AnonymousPerl1 has asked for the wisdom of the Perl Monks concerning the following question:
I can't post exact code, due to it being confidential stuff, but for the first time in a few years of mucking about in Perl, I've found a behavior that caused me to raise an eyebrow. Wisdom welcome!
use Data::Dumper; $hash{keyA}{keyB}{keyC}{keyD}=1; if (exists($hash{keyA}{keyB}{keyE}{keyF})) { print "Hi\n"; } print Dumper (\%hash); if (exists($hash{keyA}{keyB}{keyE}{keyF})) { print "Hi\n"; }
The expected behavior should be "Hi" is never printed because keyE doesn't exist in the hash (and obviously keyF doesn't exist if keyE doesn't exist). However, what I'm seeing both by using ::Dumper and in foreach hash looping further along in the code is that $hash{keyA}{keyB}{keyE} is being "created" just by checking to see if {keyA}{keyB}{keyE}{keyF} exists, however even with another exists() call, "Hi" will not be printed.
If I instead do this:
then everything is fine and dandy and behaves as expected (keyE doesn't get created).if (exists($hash{keyA}{keyB}{keyE})) {
Is exists() just a dangerous function to use for nested hashes? Or did I stumble on a bug?
|
|---|