Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: eval order of args to a sub

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


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

Your explanation makes sense.

The order of evaluation of the comma operator is defined as right-to-left. update2: or maybe not. see discussion below

see Terms and List Operators (Leftward)

update: I just tested this, and then I remembered that function arguments are aliases. Which throws the whole thing off.

perl -e'$i=1;sub a{print "@_"}; a($i--,++$i)'

Replies are listed 'Best First'.
Re^3: eval order of args to a sub
by TGI (Parson) on May 30, 2007 at 20:48 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.

        Absolutely, the post-*crement operators MUST create an anonymous value, so they do.

        I wonder though, what the "expected" or "natural" behavior of the other constructs I posted should be.

        Construct Behavior Expected $i += 2 \$i ??? $i++ Anon. Anon. $i + 1 Anon. Anon. $i++ Anon. Anon. ++$i \$i Anon. $i=2 \$i ???

        From reading the other posts here, I feel comfortable saying that people expect ++$i to pass an anonymous value. I'm not 100% sure what I'd expect the assignment operators (=, *=, etc) to pass. Since most langauges pass by value, I guess I'd expect the assignment operators to pass anonymous values equal to the value assigned to $i. For example for foo($i=2) my faulty intuition is that foo() would get an anonymous 2 in $_[0].


        TGI says moo

Re^3: eval order of args to a sub
by dewey (Pilgrim) on May 30, 2007 at 19:30 UTC
    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

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

        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://618274]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found