http://www.perlmonks.org?node_id=11110442


in reply to Re: unexpected modify hash in a distance with grep { $_ }
in thread unexpected modify hash in a distance with grep { $_ }

> Not a problem with grep. $$h{x} is autovivified before grep happens.

Nope, it happens because of the aliasing of the hash value inside grep, but I'm not sure how.

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $h = { 'a' => 1, 'b' => 2, 'c' => 3, }; warn Dumper [ $$h{a}, $$h{x}, $$h{b}, $$h{c} ]; warn Dumper [ keys %$h ]; warn Dumper [ grep {$_} $$h{a}, $$h{x}, $$h{b}, $$h{c} ]; warn Dumper [ keys %$h ]; warn !$$h{y}; warn Dumper [ keys %$h ];

C:/Perl_524/bin\perl.exe d:/tmp/pm/grep_autovivify.pl $VAR1 = [ 1, undef, 2, 3 ]; $VAR1 = [ 'b', 'a', 'c' ]; $VAR1 = [ 1, 2, 3 ]; $VAR1 = [ 'a', 'x', 'c', 'b' ]; 1 at d:/tmp/pm/grep_autovivify.pl line 19. $VAR1 = [ 'a', 'x', 'c', 'b' ];

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice