note
Roy Johnson
Thanks!
<P>
Update 2:
Because of the explanations given, I understand the phenomenon, but I think it can be explained more explicitly than has been done, so I'm going to give it a shot:
<P>
First of all, all the expressions will be evaluated when the list is built. That is, before any iteration happens, all of those assignments have been executed.
<P>
The next thing to understand is that lists may include ordinary values and lvalues; an assignment yields an lvalue, though an expression of which an assignment is just part (e.g., <code>0+($x=3)</code>) is an ordinary value. What is stored in an lvalue can change even after the lvalue is inserted into the list, so its value depends on when you look at it.
<P>
The aliasing of a for loop is not fundamental to this. Simply assigning the same list to an array demonstrates the same phenomenon:
<code>
my $x;
my @a = ($x=1, $x+=1, $x-=5);
print "@a\n";
#yields:
#-3 -3 -3
</code>
One final interesting note: postincrement does not yield an lvalue, but preincrement does.
<div class="pmsig">
<div class="pmsig-300037">
<hr>
<small>The PerlMonk <tt>tr///</tt> Advocate</small>
</div></div>
347092
347098