http://www.perlmonks.org?node_id=643446


in reply to Re^3: Puzzling $| behavior
in thread Puzzling $| behavior

i think it's matter of the compiler optimizer:
if i have something like f($x+=2, $x+=3); (and comma between arguments is not a sync point, like in C) then the optimizer could collapse $x+=2 and $x+=3 to $x+=5. It's not only matter of order.

$_ = 1; f($_+=2, $_+=3); sub f { print shift, ", ", shift, "\n"; } print `perl -v`; __________ 6, 6 This is perl, v5.6.0 built for darwin ....
Oha

Replies are listed 'Best First'.
Re^5: Puzzling $| behavior
by ikegami (Patriarch) on Oct 08, 2007 at 16:58 UTC

    It's not because of an optimization.

    # $_ $_[0] $_[1] $_[0] $_[1] # ----- ----- ----- ----- ----- $_ = 1; # 1 do { # local @_; # alias $_[0] = $_+=2; # 3 $_ 3 alias $_[1] = $_+=3; # 6 $_ $_ 6 6 &f; };

    Add $_++; to f and you'll see it print 7, 7 because $_[0] and $_[1] are aliased to $_.

    It works that way because += returns its LHS as an lvalue.

    >perl -le "$_=1; ($_+=2)+=3; print $_" 6