I have discovered some interesting behavior with the exists() command when testing multi-keyed hashes. Given the following hash:
my %List; $List{"a"}{"1"} = "yes"; $List{"a"}{"2"} = "no"; $List{"b"}{"1"} = "possibly"; $List{"b"}{"2"} = "never";
If I use exists() to test for a subkey, it creates a new instance. To demonstrate, use the above hash with this code:
print "Before:\n"; foreach my $priKey (sort keys %List) { print "$priKey\n"; } if (exists $List{"c"}{"1"} ) { print "found a bad one\n"; } print "After:\n"; foreach my $priKey (sort keys %List) { print "$priKey\n"; }
This will give the output:
Before: a b After: a b c
Is this correct behavior that I need to code around or is it a bug? It seems to me that exists should never create a key, being a test-only operation, but I wanted to see if the Monks agree. The perl version used for the above is 5.6.1 on Solaris, but it appears to be acting the same way on other versions.

In reply to Unexpected exists() behavior by tsmoyers

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.