On 29/04/2011 13:17, C.DeRykus wrote:
This happened in 5.10 ... at least after 5.8.
From: http://rt.perl.org/rt3/Ticket/Display.html?id=89024
Just like the argument list of sub calls, The list over which
foreach
iterates is evaluated in lvalue context as required by foreach's
aliasing property.
Ex:
perl -MData::Dumper -le '
for ( @y{qw(a b)} ) {$_ = "foo" unless defined $_} ;
print Dumper \%y'
$VAR1 = {
'a' => 'foo',
'b' => 'foo'
};
grep's aliasing creates the same lvalue context:
perl -MData::Dumper -le '
@x = grep {$_ = "foo" unless defined $_} @y{ qw( a b )};
print Dumper \%y'
$VAR1 = {
'a' => 'foo',
'b' => 'foo'
};
I think that is intuitive for a for loop. I would expect
for (@hash{qw/a b c/}) {
:
}
to execute three times, regardless of the state of the hash. But for
my @list = @hash{qw/a b c/};
to perform so very differently from
my @list = grep 1, @hash{qw/a b c/};
is a surprise.
I can anticipate reasons for leaving things this way, one of which is
that there must be code out there that modifies $_ in the grep block,
but it is certainly a shame and a major pitfall.
Rob
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/