Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Why is the execution order of subexpressions undefined?

by ikegami (Patriarch)
on Apr 11, 2005 at 23:53 UTC ( [id://446800]=note: print w/replies, xml ) Need Help??


in reply to Why is the execution order of subexpressions undefined?

Arguments are evaluated from left to right. (For me. I don't know if this is defined or undefined behaviour.) However, everything is passed by reference (or would that be "passed by alias" in Perl terminology). That explains why ++$i affected the $i. It must be this way to allow $_[0] = 4 to work.

The following is the C++ equivalent (ignoring the memory leaks). Try it and you will see similar results.

int func(int &at0, int &at1, int &at2); func(i, *(new int(++i)), *(new int(i+2)));

The following is a good example of the execution order in Perl:

my $i = 5; func($i+0, $i, ++$i, $i+0); # 5 6 6 6 for me. # ^^^^ ^^ # rvalue lvalue

From the above, you can also see the solution to your problem. You can work around the problem by converting lvalues to rvalues:

my $i = 5; func($i, ++i, $i+2); # 6 6 8 for me. ^^ lvalue my $i = 5; func($i+0, ++i, $i+2); # 5 6 8 for me. ^^^^ rvalue

Of course, you can no longer do $_[0] = 4; from within func if you apply this fix.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-25 15:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found