in reply to exists on hash autovivifies right?

No. exists would always return true if it autovivified the element being tested. It will however autovivify any elements the element being tested depends on. Consider:

use strict; use warnings; my %hash; if (exists $hash{top}{dependent}) { print "\$hash{top}{dependent} exists\n"; } else { print "\$hash{top}{dependent} does not exist\n"; } if (exists $hash{top}) { print "\$hash{top} exists\n"; } else { print "\$hash{top} does not exist\n"; }

Prints:

$hash{top}{dependent} does not exist $hash{top} exists

Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: exists on hash autovivifies right?
by Anonymous Monk on Jun 28, 2008 at 23:32 UTC
    I noticed that from the first example, and my memory of auto-vivification did fade a little. you said "exists would always return true if it autovivified the element being tested", well not entirely true, depends on sequence of events, but that's meaningless argue any way. thanks for taking your time.

      If it tested first but autovivified anyway it'd have to be named 'existed' because having made the test you would know that it exists. ;)


      Perl is environmentally friendly - it saves trees
        any way, the best answer is the exists does not autovivify but -> does, I buy that, so this empty argument stops here.