At 12:08 PM 6/30/02 -0400, Michael  Turner wrote:
>I appreciate everyone who wrote, but I remain confused.
>
>It seems to me that there is a weird short-circuit in the following code:
>
>$b = 1;
>$b = $b++;
>print $b;
>
>--> 1
>I expected "2"
>
>Whereas,
>
>$b = 1
>$b = ++$b;
>print $b;
>
>--> 2
>As I expected.

It depends on the order things take place in Perl.

         $b = $b++

means, take the current value of $b (1), assign it to $b, and when 
you're done with the expression, increment the value of $b by 1.  There 
are two assignments to $b going on there: the explicit one and the 
implicit one.  Since they both take place after everything else has 
happened, it ought not to be suprising that they might take place in 
any order.  Here's what the ops executed look like:

$ perl -Dt -le '$b=$b++'

EXECUTING...

(-e:0)  enter
(-e:0)  nextstate
(-e:1)  gvsv(main::b)
(-e:1)  postinc
(-e:1)  gvsv(main::b)
(-e:1)  sassign
(-e:1)  leave

What I think this means is, "Take the current value of $b, save it as 
the result of the expression.  Increment $b.  Set $b to the saved 
result of the expression."

Whereas the other case is much easier to explain:

         $b = ++$b

means, increment the value of $b, take the resulting value as the 
result of the expression, assign it to $b.

Of course, "Don't do that."  But since you asked...



--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to