Well, that piece of code is pathological for more than a few reasons, but the biggest problem is that the 0..5 isn't a range -- it's a flip-flop. To see why this happens, first look at the output of perl -MO=Deparse,-p on your code:

use Data::Dumper ('Dumper'); (my(%a) = map({((undef, (0 .. 5)) or ($a{$_}, 1));} (0..11))); print(Dumper((\%a)));

Notice that the statement-modifier form of unless gets turned into an or with the two sides reversed. This probably isn't what you expect, but according to perlsyn the unless must go immediately before the terminating semi-colon for the statement, so you're dealing with undefined behavior. The left-hand side of the or will always return true, so the right-hand side will never even be evaluated, and the expression as a whole will return the value of the left-hand side. So let's look at the left side and see what's going on.

or forces scalar context on its operands, so we get (undef, (0 ..5)) in scalar context. A comma in scalar context just returns the value of the expression on its right, so we get the value of 0..5. This expression is also in scalar context, so .. is not the range operator, it's the flip-flop operator. And since both of its arguments are constants, they get compared to the current line number to decide whether the flip-flop returns true or false. Since your script takes no input, 0 will get compared to the current line number, 0. The line number will never change from 0, so it is never equal to 5, the right-hand side of the flip-flop. Consequently, the flip-flop will return true each time through the map. (If you're confused about the flip-flop operator you can read up on it in perlop.)

The exact value returned by the flip-flop operator is a sequence number starting from 1 and increasing by 1 each time it is evaluated. So the first time through, you just get 1, the second time 2, and so on up until 12. So the map returns the list (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12). When this is converted into a hash, the elements are just taken pairwise as keys and values, giving the output you got.

edit: $., the line number, isn't actually 0 like I said above -- it's undef, which turns into 0 in numeric context. This explains the "Use of uninitialized value in range (or flip)" that cdarke got below.

-- David Irving

In reply to Re: something funny with hashes? by dirving
in thread something funny with hashes? by Pete_I

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.