Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^4: eval order of args to a sub

by Joost (Canon)
on May 30, 2007 at 19:42 UTC ( [id://618289]=note: print w/replies, xml ) Need Help??


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

Hmm.. now that I re-read that, it doesn't mean what I thought it meant.

As for the update: If the arguments were passed by value, and evaluated right-to-left, you'd expect a($i--,++$i) to pass (2,2)

Replies are listed 'Best First'.
Re^5: eval order of args to a sub
by ikegami (Patriarch) on May 30, 2007 at 20:28 UTC

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

      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!

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

Re^5: eval order of args to a sub
by dewey (Pilgrim) on May 30, 2007 at 20:00 UTC
    I see. Thanks.

    ~dewey

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 02:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found