in reply to Re: Mysterious for behavior
in thread Mysterious for behavior
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:
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.
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., 0+($x=3)) 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.
The aliasing of a for loop is not fundamental to this. Simply assigning the same list to an array demonstrates the same phenomenon:
One final interesting note: postincrement does not yield an lvalue, but preincrement does.my $x; my @a = ($x=1, $x+=1, $x-=5); print "@a\n"; #yields: #-3 -3 -3
The PerlMonk tr/// Advocate
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Mysterious for behavior
by Belgarion (Chaplain) on Apr 22, 2004 at 22:05 UTC |