http://www.perlmonks.org?node_id=847938

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."