Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Checking for Presence of Optional Attribute in Moose Object

by Your Mother (Canon)
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.)


Comment on Re: Checking for Presence of Optional Attribute in Moose Object
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://847955]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-08-23 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (173 votes), past polls