When you make the test
if ( exists $test_hash{$key1}{$key2} ) { print "The double key exists\n" ; }
you're not testing for whether a value exists for the two-part key $key1, $key2. Rather, you're testing for the existence of the key $key2 within the hash %test_hash{$key1}. The existence test implicitly creates the empty hash.

Should that be the way exists works? Good question. "Does this key exists in this hash?" is a subtley different question than "Does this hash exist, and does this key exist within it"? I'm not sure that extending exists to answer the latter is a good thing, given that means exist to answer the questions independently.


In reply to Re: exists() unexpected behavior by dws
in thread exists() unexpected behavior by Richard

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.