raybies has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to get this clear in my brain so I can explain it to a bunch of folk who have never seen Perl before...
I seemed to recall a long while ago that if you accessed a hash element that didn't exists it would autovivify the element... even if testing it with defined, and that you want to use exists... but looking through current documentation it seems that some of that warning has been curtailed. So I'm a little foggy as to what is the warning related to creating unintended hash keys and autovivification. Sorry if I'm really vague about version #'s and details... I may have misremembered things.
Are there conditions in which you can still create autovivified hash keys with undefined values that you never intended to do? And what is the way to avoid that now with current versions of Perl?
I wrote a little script to test it, because I thought this used to fail.
I don't get either "Oh No!" comments. I thought once upon a time it did... But with Nested Hashes I still get the intermediate hash autovivified.print "Oh No!(1)" if $rec{NOTE} eq "Beware!"; print "Oh No!(2)" if exists $rec{NOTE};
print "Oh No!(3)" if exists $rec{NOTE}{Nested}; print "Oh No!(4)" if exists $rec{NOTE}; OUTPUT: Oh No! (4)
So I guess if you have nested Hashes it creates an empty hash pointer for the NOTE key.
|
---|