in reply to exists on hash autovivifies right?

Yes, it will autovivify intermediate structures (tested with 5.8.8 and 5.10) but not the final element being tested:
use Data::Dumper; my $x; if (exists $x->{a}->{c}) { print "c exists\n"; } else { print "c doesn't exists\n"; } print Dumper($x); __END__ c doesn't exist $VAR1 = { 'a' => {} };
Update: added confirmation about 5.10

Replies are listed 'Best First'.
Re^2: exists on hash autovivifies right?
by ikegami (Patriarch) on Jun 29, 2008 at 07:11 UTC

    No, not really. exists never autovivifies, but it doesn't prevent anything else (such as ->) from causing autovivification. In the given example ($x->{a}->{c}), the first -> autovivifies $x and the second -> autovivifies $x->{a}. This occurs independently of exists.

    >perl -le"$x->{a}->{c}; print $x; print $x->{a}" HASH(0x225f18) HASH(0x226020)
      this is the best answer I read so far. thanks.
Re^2: exists on hash autovivifies right?
by pc88mxer (Vicar) on Jun 28, 2008 at 23:24 UTC
    If you need to test for existence without autovivification, you'll have to code up a test that goes something like this:
    sub my_exists { my $struct = shift; while (@_) { my $key = shift(@_); return false unless exists $hash->{$key}; $hash = $hash->{$key}; } 1; } my $x = {}; if (my_exists($x, 'a', 'c')) { print "c exists\n"; } else { print "c doesn't exist\n"; } print Dumper($x);
        nice modu;es. thanks.
      nice solution. thanks.
Re^2: exists on hash autovivifies right?
by Anonymous Monk on Jun 28, 2008 at 23:11 UTC
    cool!
      well, cool that you answered. btu I dislike the behavior a lot. who does not?
        A reply falls below the community's threshold of quality. You may see it by logging in.