Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Checking for Presence of Optional Attribute in Moose Object

by Your Mother (Bishop)
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 scrutinizing the Monastery: (4)
As of 2018-06-19 03:32 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.