Closed p5pRT closed 20 years ago
Isn't
perl -e 'sub foo{delete $a->{x}; 2}; $a->{x}||=foo(); print $a->{x}'
and
perl -e 'sub foo{delete $a->{x}; 2}; $a->{x}=foo(); print $a->{x}'
supposed to print the same thing ? I understand that global variables are evil\, but fwiw my intuition says that they should both print 2.
Thanks\,
Balazs
This isn't strictly speaking a bug\, as you are relying on execution order of the components of the assignment / if-assignment which perl does not specify. It so happens that in the '||=' case perl creates the left hand side first\, then calls the right hand side\, which then removes the 'x' key from the hash\, it doesn't delete the bit of memory which will hold the result of the rhs\, it just happens that nothing points to it\, so later\, the print doesn't\, as it were.
With the '='\, it runs right\, then left\, thus creating somewhere for the result.
The same happens with lexicals;
perl -le 'my $m; sub foo {delete $_[0]->{x}; 2}; $m->{x} ||= foo($m); print $m->{x}'
alex@rcon.org - Status changed from 'new' to 'resolved'
Migrated from rt.perl.org#21946 (status was 'resolved')
Searchable as RT21946$