Chris Wagner wrote on 04/18/2005 01:16:05 PM: > 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. > That's a clear and "computer scientistish" explanation. Boils down to operator precedence, where assignment is included as an operator.
It's been fun tossing this around. _______________________________________________ Perl-Win32-Users mailing list [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
