in reply to Order of operations, mutators, aliasing and whammies

As a Perl rule in general, assignments and operations take precedence over non-assignments.

In the first case (with out the +0), the precedence for printf "%4d : %3d\n",$ofs,(($ofs+=4)-4)*4 is:

1 -- (($ofs+=4)-4)*4 2 -- $ofs

Where the expression contained the assignment of $ofs+=4 has a higher precedence over the expression of $ofs by itself.

In the second case, both the $ofs+0 and $ofs+=4 has the same precedence, so the $ofs+0 will be evaluated and pushed onto the stack first, before the $ofs+=4 gets evaluated.

Replies are listed 'Best First'.
Re: Order of operations, mutators, aliasing and whammies
by Abigail-II (Bishop) on Sep 05, 2003 at 07:59 UTC
    As a Perl rule in general, assignments and operations take precedence over non-assignments.

    Bullocks. Assignment has a low precedence. Perl has 24 levels of precedence for operators, and assignment only has precedence level 19. Furthermore, it's the comma that separates the expressions - what might be relevant is the associativity of the comma operator.

    However, in the shown code, the problem lies neither with precedence or associativity. It lies with order of evaluation. And Perl defines order of evaluation only for a few operators: &&, ||, and, or, and the comma in scalar context.

    Precedence has nothing to do with order of evaluation. Precedence is a compile time thing, it determines how the parse tree looks like. Order of evaluation is a run time thing, and the compiler/interpreter is free to determine the most efficient order of evaluation.

    Abigail

      Thanks for the explanation, I have filled in the hole in my understanding of the order of evaluations. :-D