On Thu, Apr 25, 2002 at 12:51:33PM -0500, Craig S. Cottingham wrote:
> A: craigc@samantha craigc $ perl -e '$a = 1; print +(--$a + 
> $a++) . "\t$a\n"'
>    1       1
> B: craigc@samantha craigc $ perl -e '$a = 1; print +($a + 
> $a++) . "\t$a\n"'
>    3       2
> C: craigc@samantha craigc $ perl -e '$a = 1; print +(--$a + 
> $a) . "\t$a\n"'
>    0       0
> D: craigc@samantha craigc $ perl -e '$a = 1; print +($a++ + 
> - --$a) . "\t$a\n"'
>    2       1
> E: craigc@samantha craigc $ perl -e '$a = 1; print +($a + 
> - --$a) . "\t$a\n"'
>    0       0
> F: craigc@samantha craigc $ perl -e '$a = 1; print +($a++ + 
> $a) . "\t$a\n"'
>    3       2
> 
> B and F make sense, if you assume that B is commuted to F 
> internally. C and E make sense, if you assume that E is commuted 
> to C internally. I can't figure out why A and D evaluate 
> differently, nor why A evaluates to 1 instead of 0 as I'd 
> expect. The results of C, D, and F suggest that perl, at least 
> in this instance, evaluates the operands to the '+' binary 
> operator in left-to-right order.

Post-decrement and -increment leave a copy of the variable's current value
on the stack, and then change the value of the variable.  Pre-decrement and
-increment change the value of the variable and then leave a *pointer to
that variable* on the stack.  In the latter case, the value of the variable
may change before the pointer is retrieved from the stack.

FYI, this particular behavior has been discussed extensively on various
forums.

Ronald

Reply via email to