in reply to Re: Best Hash Practices?
in thread Best Hash Practices?

> that autovivication doesn't happen when you simply check wether a hash key exists or is true.

unfortunately not that simple!

use strict; # or die use warnings; # or die my %hash = ( cogito => "ergo sum" # I think, so I exist ); if (exists $hash{absum}{absum}) { print "Absum exists."; } else { print "Absum doesn't exist.\n"; # which would explain it's name... } print "Keys in the hash: ", join(" :: ", keys %hash), "\n";
output
/usr/bin/perl -w /home/lanx/tst.pl Absum doesn't exist. Keys in the hash: cogito :: absum

Cheers Rolf

Replies are listed 'Best First'.
Re^3: Best Hash Practices?
by muba (Priest) on Oct 10, 2009 at 02:16 UTC

    unfortunately not that simple!

    if (exists $hash{absum}{absum})

    Ah, yes. But note how that syntax implies a -> operator, as ikegami described elsewhere in this thread, and it is that operator which makes $hash{absum} come into existance. Of course it is funny to see how exists still thinks $hash{absum} doesn't exist, however, not so funny anymore when you realize that exists really tries to check the existance of $hash{absum}->{absum} here. So exists is right and the information in that print statement is factually wrong. It should state print "Absum->absum doesn't exist\n" or something similar meaning the same.