Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^6: eval order of args to a sub

by mrpeabody (Friar)
on Jun 03, 2007 at 11:15 UTC ( [id://618970]=note: print w/replies, xml ) Need Help??


in reply to Re^5: eval order of args to a sub
in thread eval order of args to a sub

Mmm, that doesn't say what you think it says.

"Operator associativity" defines what happens if a sequence of the same operators is used one after another: whether the evaluator will evaluate the left operations first or the right. For example, in "8 - 4 - 2", subtraction is left associative so Perl evaluates the expression left to right. "8 - 4" is evaluated first making the expression "4 - 2 == 2" and not "8 - 2 == 6".
This answers the question "Which minus operator will be evaluated first?" But our question is different: "Will a given operator evaluate its left-hand or right-hand argument first?"

In other words, what if the code were foo() - bar() - baz()? Will foo() execute before bar(), or the reverse? Operator associativity has nothing to say about that. This is the same issue as $i++ * $i and foo($i++, $i).

I've looked through the docs, and it's not there. Perl doesn't define the evaluation order of subexpressions.

Replies are listed 'Best First'.
Re^7: eval order of args to a sub
by ikegami (Patriarch) on Jun 03, 2007 at 15:00 UTC

    So your saying that if operation evaluation order (associativity) is specified, but operand evaluation order is not specified,

    $result = foo() - bar() - baz() - moo();

    could be evaluated as

    $anon1 = moo(); $anon2 = baz(); $anon3 = bar(); $anon4 = foo(); $result = (($anon4 - $anon3) - $anon2) - $anon1;

    I suppose you're right and it doesn't violate the definition of associativity, but how odd!

      Operation evaluation order is about precedence, operand evaluation order is about associativity, so your case won't happen.

      + - . have "left associativity", so the operands are evaluated in that order; after evaluating the operands for an operation each operation takes place in the order which satisfies said associativity.

      The left associativity leads to the following order:

      $result = ( ( foo() - bar() ) - baz() ) - moo();

      which is also the order of execution of the subexpressions. Right from perlop:

      Operator associativity defines what happens if a sequence of the same operators is used one after another: whether the evaluator will evaluate the left operations first or the right. For example, in "8 - 4 - 2", subtraction is left associative so Perl evaluates the expression left to right. "8 - 4" is evaluated first making the expression "4 - 2 == 2" and not "8 - 2 == 6".

      which IMHO pretty well defines the execution order of subexpressions.

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
        The debug output does demonstrate that, for a specific toy example run on your particular binary, foo() executes first. That's a long way from showing that Perl will behave similarly for all operators and all operands, including tied variables, slow system calls, and threaded applications.

        + - . have "left associativity", so the operands are evaluated in that order; after evaluating the operands for an operation each operation takes place in the order which satisfies said associativity.

        People might assume that, but it's just not on the page. The snippet we both quoted clearly confines itself to breaking operand-binding ties between adjacent identical operators. On the issue of when to evaluate each operand, the Camel speaketh not.

        For that matter, you don't even know for sure that foo() will always be evaluated before moo(). Perl could run those functions left-to-right, right-to-left, or all at the same time and still be within spec. It just has to apply the operators in a certain order and finish up before beginning the next statement.

        Operation evaluation order is about precedence, operand evaluation order is about associativity

        Precedence is about the order of evaluation of different operators.
        Associativity is about the order of evaluation of operators with the same precedence.
        While associativity seems to imply operand evaluation order, that's just an unfounded assumption.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://618970]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-24 19:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found