Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'd like to get rid of above warning

on this piece of code (in the "if" line) :

sub get_attribute { my ( $self, $attribute ) = @_; if ((exists( $self->{ATTRIBUTES} )) and ( defined( ${ $self->{ +ATTRIBUTES} }{$attribute} ) ) ) { return ${ $self->{ATTRIBUTES} }{$attribute}; } return undef; }
How can I check if value exists before actually use it ?

Thanks in advance,

regards,

Bulek.

Replies are listed 'Best First'.
Re: Use of uninitialized value in hash element at
by wind (Priest) on Apr 05, 2011 at 20:55 UTC
    sub get_attribute { my ( $self, $attribute ) = @_; return exists $self->{ATTRIBUTES} && exists $self->{ATTRIBUTES}{$a +ttribute} ? $self->{ATTRIBUTES}{$attribute} : undef; }
      sub get_attribute { my ( $self, $attribute ) = @_; return exists $self->{ATTRIBUTES} ? $self->{ATTRIBUTES}{$attribute} : undef; }
      will do the same, but if you don't mind to create $self->{ATTRIBUTES}, it maybe even shorter:
      sub get_attribute { my ( $self, $attribute ) = @_; return $self->{ATTRIBUTES}{$attribute}; }
        Hi,

        thanks for responses... I've tried both solutions and I still get warnings :

        Use of uninitialized value in hash element at ....


        line with return statement... Is there any chance to get rid of those warnings ?

        Thanks in advance, regards, Rob.