in reply to Auto-increment frenzy

$a = $b = $c = 1; print $a++, " ", $a++, "\n"; print $b++, " ", $b, "\n"; print $c, " ", $c++, "\n";

Isn't order of operations fun? I'm not sure I completely understand this either. As far as I can tell, although addition is generally left-associative, all operations in the equation (++ operators) are run first. The auto-increment operators have higher precedence than the addition operator. The first two equations in your example are easy to understand. The first $a++ returns 1, which is added to the second $a++ which returns 2, since the inital ++ has already incremented the variable. The $b equation is much the same, since the post-increment operator is superfluous. The final equation is tricky. Because of precedence, the auto-increment runs first, setting the value of the rhs of the equation to 1, and setting $c to 2, which makes that the lhs. Keep in mind, that the addition operator operates on the values returned by the auto-increment operators at the time that they ran.

Edit: As Abigail-II pointed out, I have no idea what I'm talking about in terms of standards. I am curious, though, why the standards-maintainers chose to keep this behavior undefined.

Replies are listed 'Best First'.
Re: Auto-increment frenzy
by Abigail-II (Bishop) on Aug 26, 2003 at 23:15 UTC
    Because of precedence, the auto-increment runs first,

    Uhm, no. Now you are confusing operator precedence with evaluation order. Perl does NOT define an evaluation order (for those who want to say "yes, it does", don't do so unless you can cite the documentation saying that Perl does).

    Except for the first line, all lines of code posted by the OP have an undefined behaviour.

    Abigail