<?xml version="1.0" encoding="windows-1252"?>
<node id="847938" title="Checking for Presence of Optional Attribute in Moose Object" created="2010-07-03 17:28:46" updated="2010-07-03 17:28:46">
<type id="115">
perlquestion</type>
<author id="140424">
roho</author>
<data>
<field name="doctext">
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 
&lt;i&gt;"Use of uninitialized value in concatenation (.) or string"&lt;/i&gt;
is generated. How can I check for the presence of an optional attribute 
without generating the warning message?
&lt;p&gt;
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-&gt;age() is not present in the current object.
&lt;/p&gt;
&lt;p&gt;
Just referencing $self-&gt;age() in 'show_person' to test it generates the warning, as in:
&lt;br&gt;
&lt;code&gt;if ($self-&gt;age() ne '') {...};&lt;/code&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;code&gt;
#!/usr/bin/perl

package Person;
use Moose;

has 'name' =&gt; ( is  =&gt; 'ro', isa =&gt; 'Str', required =&gt; 1);
has 'age'  =&gt; ( is  =&gt; 'ro', isa =&gt; 'Str');

sub show_person {
   my $self = shift;
   return $self-&gt;name() . " " . $self-&gt;age();
}

__PACKAGE__-&gt;meta-&gt;make_immutable;
no Moose;
1;

my $person = Person-&gt;new(name =&gt; "Bob");
print "\n", $person-&gt;show_person(), "\n";
&lt;/code&gt;

&lt;div class="pmsig"&gt;&lt;div class="pmsig-140424"&gt;
&lt;p&gt;
"Its not how hard you work, its how much you get done."
&lt;/div&gt;&lt;/div&gt;</field>
</data>
</node>
