in reply to Learning to use "each"
It shows that the loop is really using $_ for the iterator.$ perl -MO=Deparse 901847.pl use warnings; use strict 'refs'; { my(%hash) = ('one', 1, 'two', 2); foreach $_ (my($key, $value) = each %hash) { print $key, "\n"; } }
Update: Now I have changed the OP code a little, just to print more info:
use warnings; use strict; my %hash = ("one" => 1, "two" => 2, ); foreach ((my $key, my $value) = each %hash) { print "\$key=$key \$value=$value \$_=$_\n"; } __END__ $key=one $value=1 $_=one $key=one $value=1 $_=1
It seems like each is evaluated once in list context. It happens to choose the 'one' key and stores that in $key and stores the value '1' in $value. It never evaluates each again. Then each time through the loop, each item of the list ('one', '1') is assigned to $_.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Learning to use "each"
by JavaFan (Canon) on Apr 28, 2011 at 19:14 UTC | |
by toolic (Bishop) on Apr 28, 2011 at 19:19 UTC | |
by JavaFan (Canon) on Apr 28, 2011 at 19:29 UTC |