in reply to something funny with hashes?

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

Replies are listed 'Best First'.
Re^2: something funny with hashes?
by puudeli (Pilgrim) on Mar 06, 2009 at 09:19 UTC

    Brilliantly explained and reverse engineered. ++ for that!

    --
    seek $her, $from, $everywhere if exists $true{love};
Re^2: something funny with hashes?
by Pete_I (Beadle) on Mar 07, 2009 at 04:04 UTC
    Thank you for the very well explained answer. I'd never heard of the flip-flop before. I'll be pouring over perlop for a few days.
    -- Pete_I