Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: eval order of args to a sub

by dewey (Pilgrim)
on May 30, 2007 at 18:48 UTC ( [id://618264]=note: print w/replies, xml ) Need Help??


in reply to eval order of args to a sub

From perlsub:
"...all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars."
In other words, all subs have only one argument, a list value. Since the constructor for list values is the comma-separated list of values, the question becomes 'in what order are the arguments to the list-context comma operator evaluated?' Frustratingly, the documentation doesn't seem to say...

My guess is that it's right to left in most implementations (due to the overloading of comma in scalar context) but that may not be dependable.

Later: It is in fact defined as left to right! Thanks, ikegami.

Later later: ummm, if this is crucial, please see discussion below. There is some disagreement.

~dewey

Replies are listed 'Best First'.
Re^2: eval order of args to a sub
by Joost (Canon) on May 30, 2007 at 19:15 UTC

      I think the moral of the story is "understand what you are passing to your functions".

      It appears that most ways that one might modify a variable in a function call will result in an alias to the variable being passed. Post-increment seems to be unique in that it creates an anonymous value.

      C:\>perl -e "sub foo{ print join qq/\n/, map { \$_ .' - ' . $_ } @_ } +foo($i=1,++$i,$i++,$i+1,$i+=1,$i); SCALAR(0x183059c) - 4 SCALAR(0x183059c) - 4 SCALAR(0x225f7c) - 2 SCALAR(0x18305fc) - 4 SCALAR(0x183059c) - 4 SCALAR(0x183059c) - 4


      TGI says moo

        Post-increment seems to be unique in that it creates an anonymous value.
        That doesn't suprise me. post-increment and post-decrement return (a copy of) the old value, while changing the original value. What I mean is, since post-*crement creates 2 values from one variable, you need a copy somewhere, and it makes sense (for ease of coding and performance - perl variables are passed by reference internally) to increment the original value and create a copy as the return value of the *crementation.

      Hmmm... I can't see where in Terms and List Operators (Leftward) the comma operator's order of evaluation is given... it mostly seems to concern term/parenthetical precedence. Am I missing something obvious?

      Also, I don't understand your `update' remark. Your code produces 1 1, as I would have expected... could you clarify?

      ~dewey
Re^2: eval order of args to a sub
by chromatic (Archbishop) on May 31, 2007 at 18:03 UTC
    In other words, all subs have only one argument, a list value.

    I think this is misleading. There is a list of arguments, pushed onto the stack and popped off (or accessed directly). There may be a single value in the list of arguments, but to talk about a list as if it is a single argument is... misleading.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-20 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found