in reply to Re: Re: Re: An obscure side effect?
in thread An obscure side effect?

[I]n general parentheses control the order of evaluation for math operators, not the order of execution for assignments...

Unless assignment is just another way to build expressions. See perlop, and note that the assignment operator has associativity and precedence.

Postincrement/postdecrement are tricky beasts. In C (at least in K&R C), their timing is left to the whim of the implementer. The swab() example involves a side-effect (substr() as lvalue), but the timing of the side-effect is well-defined.

  • Comment on Re: Re: Re: Re: An obscure side effect?

Replies are listed 'Best First'.
Re: Re: Re: Re: Re: An obscure side effect?
by sgifford (Prior) on Aug 04, 2003 at 08:19 UTC

    Postincrement and postdecrement also have associativity and precedence (although their associativity is "nonassoc", since they're unary), but their order of execution is still undefined when used multiple times in the same statement.

    My point is that I don't believe that associativity or precedence guarantee order of execution. For example, addition and multiplication have well-known associativity and precedence, but in the expression

    $a = b()*c() + d()
    I don't believe the order in which a, b, or c will be executed is guaranteed, since, ignoring side-effects, they can be evaluated in many different orders to produce correct results. At least, I haven't been able to find such a guarantee in Perl's documentation; if you can point me to one with some degree of precision I'd be happy to change my mind.

      Off the top of my head, the boolean operators are guaranteed to evaluate their arguments left-to-right, required for short-circuit behaviour. The comma operator is also guaranteed to evaluate arguments left-to-right. And the ternary is guaranteed to evaluate the condition before the other argument it picks, because the one it doesn't pick is guaranteed not to be evaluated.

      In other words (besides the comma), all operators which exhibit some form of short circuiting operator (and in Perl, are therefor also useful for flow control).

      Makeshifts last the longest.