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:

if (exists($hash{keyA}{keyB}{keyE})) {
then everything is fine and dandy and behaves as expected (keyE doesn't get created).

Is exists() just a dangerous function to use for nested hashes? Or did I stumble on a bug?


In reply to exists() for a hash child-key creates the parent key? Bug? by AnonymousPerl1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.