Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: eval order of args to a sub

by dewey (Pilgrim)
on May 30, 2007 at 18:48 UTC ( #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.


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?

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://618264]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (9)
As of 2018-07-17 10:06 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (363 votes). Check out past polls.