Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Checking for Presence of Optional Attribute in Moose Object

by Your Mother (Chancellor)
on Jul 04, 2010 at 04:47 UTC ( #847955=note: print w/replies, xml ) Need Help??

in reply to Checking for Presence of Optional Attribute in Moose Object

A way to do it is with a predicate.

BEGIN { package Person; use Moose; use overload '""' => sub { +shift->show_person }, fallback => 1; has 'name' => is => 'ro', isa => 'Str', required => 1; has 'age' => is => 'ro', isa => 'Str', predicate => 'has_age'; sub show_person { my $self = shift; join(", ", $self->name, $self->has_age ? $self->age : "age unknown"); } __PACKAGE__->meta->make_immutable; no Moose; 1; } my $person = Person->new({ name => "Bob" }); print $person->show_person, "\n"; print Person->new({ name => "Your Uncle", age => 93 }), "\n";

More here: Moose::Manual::Attributes.

(Update: man! I waited for hours to see if anyone would bite on this and when I finally go to post, FalseVinylShrub hit create while I was typing.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://847955]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-01-19 00:41 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (215 votes). Check out past polls.