Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^7: eval order of args to a sub

by ikegami (Patriarch)
on Jun 03, 2007 at 15:00 UTC ( [id://618975]=note: print w/replies, xml ) Need Help??


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

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!

Replies are listed 'Best First'.
Re^8: eval order of args to a sub
by shmem (Chancellor) on Jun 04, 2007 at 01:15 UTC
    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.

        You are both right, mrpeabody and ikegami - it's not in the specs; but my take on this is that in the expression
        $result = 8 - 4 - 2;

        the term 8 - 4 is a subexpression and the operand for the rightmost substraction operation; from that I deduce that associativity therefore defines the operand and subexpression evaluation order.

        That might be a logical fallacy; which one, I wonder?

        Anyways, that's how perl does it, although it is not defined in Perl. Of course the implementation could change, and perl could do a breadth-first evaluation of its execution tree and roll a dice for ad-hoc definition of evaluation order. But the optimizer would set things straight again, I guess :-)

        --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}

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2024-04-25 23:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found