I would add that a quick check is to pop into the debugger. [our is used because the debugger has issues with my variables.]
c:\perl\perl>perl -de 0 Loading DB routines from perl5db.pl version 1.19 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(-e:1): 0 DB<1> use strict DB<2> use warnings DB<3> our %hash = { 'key1' => 'val1', 'key2' => 'val2' }; DB<4> x \%hash 0 HASH(0x1cbd91c) 'HASH(0x1ca1f4c)' => undef
[Code entered this way in the debugger won't give the warning the others mentioned.]

Note here that %hash has a stringified hash as the key in it (and that key has no value). Since {...} returns a hashref here, and %hash = ... is expecting key/value pairs (or something else that looks like a hash), it grabs the hashref as the first key, stringifies it, and takes undef as the corresponding value.

Compare that to this:

DB<5> %hash = ( 'key1' => 'val1', 'key2' => 'val2' ); DB<6> x \%hash 0 HASH(0x1cbd91c) 'key1' => 'val1' 'key2' => 'val2' DB<7>
which is probably what you wanted.

For grins, try this:

print {"key"=>"value"}, "\n";
That's what a hashref looks like, stringified. Not terribly useful, but a recent Quiz of the Week might be an interesting detour.

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: exists puzzle by QM
in thread exists puzzle by bret.foreman

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.