On Apr 29, 2:39 am, u...@stemsystems.com ("Uri Guttman") wrote: > >>>>> "RD" == Rob Dixon <rob.di...@gmx.com> writes: > > RD> On 29/04/2011 10:27, Uri Guttman wrote: > > RD> Good call Brian. It's not at all obvious that all the elements of a hash > RD> slice will be created if they don't exist :) > >> > >> and they won't be anyhow. you need have lvalues to autovivify hash (or > >> array) elements. > >> > >> perl -le '@x = @y{ qw( a b )}; print keys %y' > >> > >> %y is empty as you can see. > > RD> I meant in the specific case of the grep that was posted. There are no > RD> lvalues there, yet they are autovivified: > > RD> perl -le '@x = grep defined, @y{ qw( a b )}; print keys %y' > > that shouldn't happen IMO. it is only calling defined on the aliased > values of %y. i would call it a bug but some could argue otherwise. >
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' }; -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/