Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: eval order of args to a sub

by ikegami (Patriarch)
on May 30, 2007 at 20:28 UTC ( [id://618314]=note: print w/replies, xml ) Need Help??


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

You were looking at the wrong section of the document. If you check the Operator Precedence and Associativity table, you'll notice the list seperator is left-associative, and that operator associativity is defined as follows:

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.

I can confirm that is how it works on my system (ActivePerl 5.8.8 on WinXP).

Replies are listed 'Best First'.
Re^6: eval order of args to a sub
by mrpeabody (Friar) on Jun 03, 2007 at 11:15 UTC
    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.

      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}
Re^6: eval order of args to a sub
by blazar (Canon) on May 30, 2007 at 21:34 UTC
    I can confirm that is how it works on my system (ActivePerl 5.8.8 on WinXP).

    /me too. (Actually, that was in answer to otto.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://618314]
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 01:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found