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

I think you may simply be running into a case where Perl's order of operations is undefined.

Good guess, but a simple experiment disproves it. You can put parentheses around the substr's in the original example to force the order of evaluation.

Replies are listed 'Best First'.
Re: Re: Re: An obscure side effect?
by sgifford (Prior) on Aug 04, 2003 at 06:21 UTC
    Perhaps there's some way to do this, but in general parentheses control the order of evaluation for math operators, not the order of execution for assignments or argument evaluation. For example, how would you parenthesize to make
    $a[$i++] = $i++;
    evaluate the left-hand side before the right, producing $a[0] = 1? Or to make
    $j = $i++ - $i++;
    evaluate the second $i before the first, producing 1?

    How would you suggest parenthesizing in swab to guarantee the order of execution for this?

      [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.

        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.