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