in reply to Autovivification with hash of hashes

Here's a neat (I think so anyway) little trick that I found.
1) declare a hashref. ($hashref = {})
if you check to see if the hash is defined then
if(defined $hashref)
always returns true even if you have not added to the hash, however if you check for
if(defined %$hashref)
then this will only return true if $hashref actually refers to an existing hash. so I guess that
if(defined $hashref->{KEY})
should only return true if the key exists.

Replies are listed 'Best First'.
Re^2: Autovivification with hash of hashes
by Aristotle (Chancellor) on Jan 28, 2003 at 11:46 UTC

    This is broken. It will falsely return false for keys which exist but point to a value equal to 0 or the empty string.

    Nor does it solve the problem at hand, because exists $hashref->{KEY} will not autovivify that key into a hashref either. It is only when you try something like exists $hashref->{LEVEL1}->{LEVEL2} that the level one key is automagically created. But in that case, defined $hashref->{LEVEL1}->{LEVEL2} will do the same.

    So your "solution" adds a new problem without solving the existing one.

    Makeshifts last the longest.