16.10. Coercions
In addition to the :ATTR markers for attribute hashes, Class::Std also supplies markers for subroutines that implement conversions to numbers, strings, and booleans: sub count : NUMERIFY { This provides a simpler, more convenient, and less repetitive interface than use overload: sub count { my ($self) = @_; return scalar @{ $elements_of{ident $self} }; } sub as_str { my ($self) = @_; return sprintf '(%s)', join $COMMA, @{ $elements_of{ident $self} }; } sub is_okay { my ($self) = @_; return !$self->Houston_We_Have_A_Problem( ); } use overload ( q{0+} => 'count', q{""} => 'as_str', q{bool} => 'is_okay', fallback => 1, ); |