roho has asked for the wisdom of the Perl Monks concerning the following question:
I have a Moose class with an attribute that is not required. When I try to
access this attribute in an object where it is not present, the warning message
"Use of uninitialized value in concatenation (.) or string"
is generated. How can I check for the presence of an optional attribute
without generating the warning message?
The sample code below is a simple Moose class with 1 required attribute (name) and 1 optional attribute (age). The 'show_person' method generates the warning message because $self->age() is not present in the current object.
Just referencing $self->age() in 'show_person' to test it generates the warning, as in:
if ($self->age() ne '') {...};
#!/usr/bin/perl package Person; use Moose; has 'name' => ( is => 'ro', isa => 'Str', required => 1); has 'age' => ( is => 'ro', isa => 'Str'); sub show_person { my $self = shift; return $self->name() . " " . $self->age(); } __PACKAGE__->meta->make_immutable; no Moose; 1; my $person = Person->new(name => "Bob"); print "\n", $person->show_person(), "\n";
"Its not how hard you work, its how much you get done."
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Checking for Presence of Optional Attribute in Moose Object
by FalseVinylShrub (Chaplain) on Jul 04, 2010 at 04:41 UTC | |
Re: Checking for Presence of Optional Attribute in Moose Object
by Your Mother (Archbishop) on Jul 04, 2010 at 04:47 UTC | |
Re: Checking for Presence of Optional Attribute in Moose Object
by Khen1950fx (Canon) on Jul 04, 2010 at 04:23 UTC | |
by roho (Bishop) on Jul 04, 2010 at 07:02 UTC |
Back to
Seekers of Perl Wisdom