Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^4: Puzzling $| behavior

by oha (Friar)
on Oct 08, 2007 at 12:34 UTC ( #643446=note: print w/ replies, xml ) Need Help??


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


Comment on Re^4: Puzzling $| behavior
Download Code
Re^5: Puzzling $| behavior
by ikegami (Pope) 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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2014-07-26 08:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (175 votes), past polls