In perl 5 all order of evaluation is undefinedNot true. There are a few cases for which order of execution is very well defined. One is your example, assignment: the assignment always happens after the expression, the RHS is calculated. That makes sense, as it's only at that point that the value is known.
And two is the shortcircuit operators, and/or and &&/||. The right hand side isn't even calculated if the left hand side is all one needs to know to get the final result of the whole expression.
So this combination is very safe:
Order of evaluation is as follows:sub foo { return 3+$a }; $a = 1; $a = foo($a) and print $a;
Note that this is different from the following case:
Note that in the relevant statement, $x in the left hand side and in the right hand side are actually two different variables, both named $x. The RHS uses the outer scoped variable, the LHS the inner scoped variable. And even though here:$\ = "\n"; my $x = 12; { my $x = $x + 1; # <--- point of interest print $x; } print $x;
the assignment does happen before the print, it prints the outer scoped variable, because that's the variable the parameter refers to.my $x = 123; { my $x = 45 and print $x; }
In reply to Re: Matching and order of evaluation
by bart
in thread Matching and order of evaluation
by diotalevi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |