in reply to *a=*b;@a=@b problem
is the equivalent of:@b = @a;
And because the code defined *a = *b earlier, the assignment @b = () affected @a on the right hand side before the assignment, and hence the assignment @b = @a is really doing @b = () and @b = () again. Therefore the value of @a and @b after the assignment are both undef. But why does the resulting list contain two undef's? Ummm... this reminds me of another post by BrowserUk a few days ago on the side effect of print join '|', @a = %b... Another weired behaviour of Perl@b = (); # temporary internal step @b = @a;
And I am getting the correct output:use Data::Dumper; *a = *b; @b = (1,2); @b = @b; # perl detects this is assigning into same variable print Dumper(\@b); @b = @a ? @a : (); # new assignment print Dumper(\@a); print Dumper(\@b);
From which an obersation is made: the right hand-side @a ? @a : () is evaluated first into a temporary copy of @a, before assigning into @b, thus preserving the original values of @a.$VAR1 = [ 1, 2 ]; $VAR1 = [ 1, 2 ]; $VAR1 = [ 1, 2 ];
|
|---|