LPM: Strict variable checking in classes?

Steve Lane sml at zfx.com
Thu Mar 29 09:53:00 CST 2001


Matt Cashner wrote:
> 
> On Thu, 29 Mar 2001, Steve Lane wrote:
> 
> >       if (exists $self->{$method}) {
> 
> exists will always return true on a hash thanks to
> autovivification. 

i don't think this is correct:

  perl -le 'print 1 if exists $self->{key}'
  [nothing]

autovivification doesn't extend to the final subscript.
in my example, the only autovivication that happens is
that $self becomes an anonymous hashref.

  perl -le 'print 1 if exists $self->{key}; print $self'
  HASH(0x804ac1c)

of course, if there is more than 1 dimension of subscripting,
the subscripts before the final one get autovivified:

  perl -le 'print 1 if exists $self->{key}{key2}{key3}; 
            print $self;
            print "EXISTS key"  if exists $self->{key};
            print "EXISTS key2" if exists $self->{key}{key2}; 
            print "EXISTS key3" if exists $self->{key}{key2}{key3}'
  HASH(0x804ac1c)
  EXISTS KEY
  EXISTS KEY2

> defined is a much better option.

$self->{$method} having a value of undef() should not mean
that $method is an invalid method.
--
Steve Lane <sml at zfx.com>



More information about the Lexington-pm mailing list