At 10:10 AM 4/18/05 -0700, Jan Dubois wrote:
>> > $x=10;
>> > $x=$x++;
>> > print "$x\n";
>> > #Results: $x=10, $x=11.
>> The first result ($x=10) puzzles me. Are not the $x on the LHS and RHS
>> refering to the same scalar? I would think that $x would be assigned the
>> value of $x (10), and then be post-incremented, resulting in 11.

I think the best way to understand this is that the evaluation of the =
operator comes later than the evaluation of the ++ operator.  First $x is
interpreted as a right hand value of 10.  The = operator now has a 10 in
it's queue of things to shove into the left hand side.  Once $x is
interpreted into a 10, the ++ operator is evaluated and $x becomes 11.  But
wait, the = operator still has to shove it's queue of things into $x.  It
does this and the preincrement value of $x is shoved back into $x. 

$x = 10;
$x = ++$x;
print $x;
#->> 11
Here the = operator sees the postincrement value of $x and shoves that into $x.


>Think about how a more complex expression would need to be compiled:
>
>  $x = 10;
>  $x = $x++ + $x++;

Same thing here, the + operator has a 10 and an 11 to work on.  The 2nd $x
is eleven because one ++ operator has been evaluated.  Basically the ++
operator leaves the old value as the argument to whatever operator or
context it's used in.







--
REMEMBER THE WORLD TRADE CENTER         ---=< WTC 911 >=--
"...ne cede males"

00000100

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to