Pete_I has asked for the wisdom of the Perl Monks concerning the following question:

whilst toying with some code, a friend and I came upon this:
use Data::Dumper qw/Dumper/; my %a = map { $a{$_} => 1 unless undef, 0..5 } 0..11; print Dumper \%a;
output:
$VAR = { '11' => 12, '1' => 2, '3' => 4, '7' => 8, '9' => 10, '5' => 6 };
same results if we change the 0..5 to 0..333 or 0..1, but doesn't work the same if we remove the range operator. We're confused here. we know it's screwy we're checking for it being undef in the assignment/map. the question is, where's that 12 coming from?

A second question, possibly related:
my %a = map { $a{ $a{ $a{ $a{ $a{ $_ } } } } } => 1, 0..5 } 0..9; print Dumper sort(\%a);
also has the same output as:
my %a = map { $a{ $_ } => 1, 0..5 } 0..9;
why? these clearly aren't the same thing are they? am I missing something vital here?
-- Pete_I

Replies are listed 'Best First'.
Re: something funny with hashes?
by dirving (Friar) on Mar 06, 2009 at 08:59 UTC

    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

      Brilliantly explained and reverse engineered. ++ for that!

      --
      seek $her, $from, $everywhere if exists $true{love};
      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
Re: something funny with hashes?
by cdarke (Prior) on Mar 06, 2009 at 09:18 UTC
    I'm wondering if you are mis-understanding =>. It is not an assignment, but a "fat comma". It allows a bareword on its left-hand side, but other than that is just a comma. This means you are invoking the comma operator in list context, which results in the rightmost value. So:
    my %a = map { $a{$_} => 1 unless undef, 0..5 } 0..11;
    Is the same as
    my %a = map { 1 unless undef, 0..5 } 0..11;
    In fact, if you had run with use strict; and use warnings; things may have been clearer because that code does not actually compile. It requires the %a to be declared first (since it is used in the map on the rhs). When I run this:
    use strict; use warnings; use Data::Dumper qw/Dumper/; my %a; #%a = map { $a{$_} => 1 unless undef, 0..5 } 0..11; #%a = map { $a{$_} , 1 unless undef, 0..5 } 0..11; %a = map { 1 unless undef, 0..5 } 0..11; print Dumper \%a;
    I get (5.10):
    Use of uninitialized value in range (or flip) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. Use of uninitialized value in range (or flop) at C:\gash.pl line 10. $VAR1 = { '11' => 12, '1' => 2, '3' => 4, '7' => 8, '9' => 10, '5' => 6 };
Re: something funny with hashes?
by moritz (Cardinal) on Mar 06, 2009 at 08:35 UTC
    why? these clearly aren't the same thing are they?

    They are clearly the same thing.

    Since the right hand side of the assignment is evaluated before the left hand side, %a is always empty in the map, and thus $a{ anything } is always undef.