in reply to Matching and order of evaluation

In perl 5 all order of evaluation is undefined
Not 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:

sub foo { return 3+$a }; $a = 1; $a = foo($a) and print $a;
Order of evaluation is as follows:

Note that this is different from the following case:

$\ = "\n"; my $x = 12; { my $x = $x + 1; # <--- point of interest print $x; } print $x;
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:
my $x = 123; { my $x = 45 and print $x; }
the assignment does happen before the print, it prints the outer scoped variable, because that's the variable the parameter refers to.

Replies are listed 'Best First'.
Re: Re: Matching and order of evaluation
by diotalevi (Canon) on Oct 28, 2003 at 05:23 UTC

    Oh ok, right. I didn't intend to be that general. There are a few select places where execution order is defined. Your example of the RHS computing before the LHS isn't a good one because that part isn't well defined. That only matters where computing the LHS has some sort of external effect on a dependancy of the RHS. Of course, the RHS has to be executed before the assignment but the LHS also has to execute to find the lvalue for the assignment and its that part that can move around.

    Anyhow, I think I'm clear on when I care about execution order - only when the sub-expressions in a statement depend on each other - say multiple shift statements: sub { ${ shift() } = shift }->( a, b ). In this case sassign wants the LHS as its last argument so the second shift runs first then the first shift and scalar dereference. Execution order matters here because a common element is being altered. Ah well.

    perl -MO=Concise -e '@ARGV=qw(a b); ${shift()} = shift;' | Idealized optrees from B::Concise

    leave enter nextstate aassign pushmark const a const b pushmark rv2av gv nextstate sassign shift rv2av gv rv2sv scope shift rv2av gv