Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Order of execution of functions in list

by ikegami (Pope)
on Sep 13, 2013 at 13:26 UTC ( #1053932=note: print w/ replies, xml ) Need Help??


in reply to Re: Order of execution of functions in list
in thread Order of execution of functions in list

has no side-effects

$ perl -E'@a=qw( a b c ); say 0+@a; shift(@a); say 0+@a;' 3 2

returns an rvalue

$ perl -E'$_=123; say; sub { sub { ++$_[0] }->(shift); }->($_); say;' 123 124


Comment on Re^2: Order of execution of functions in list
Select or Download Code
Re^3: Order of execution of functions in list
by LanX (Abbot) on Sep 13, 2013 at 14:05 UTC
    The problem with mobiles is that I can't check my code (at least ATM)

    And I don't understand the sideeffect code you posted.

    I'll reply this evening...

    Edit

    At second glance it seems that shift really returns lvalues... Strange

    Mea culpa!!! :(

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re^3: Order of execution of functions in list
by LanX (Abbot) on Sep 13, 2013 at 17:02 UTC
    > > returns an rvalue

    FWIW: I think this code is easier to understand and demonstrates your point

    DB<194> sub tst { my $x=\(shift()); ++$$x } DB<195> $a=10 => 10 DB<196> tst $a => 11 DB<197> tst $a => 12 DB<198> $a => 12
    I get the same results using the equivalent splice(@_,0,1)

    That's not documented in the perldocs for shift or splice and I think it's due to the way Perl holds and replaces variables in @_, they just don't loose their aliasing magic when shifted.

    Maybe Perl avoids copying of the values for performance reasons.

    Interesting...

    But since it's not documented, I doubt that this is reliable behavior.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      But since it's not documented, I doubt that this is reliable behavior.

      It's an emergent behaviour; it's not by design.

        It's an emergent behaviour; it's not by design.

        What's that mean (important why)?

        It goes back far enough (5.8 at least) and its in t/op/sub_lval.t update: since 2001

        so it emerged? long ago :)

        OTOH, a reference to an alias takes a reference to the original and incrementing tat referenced value increments the original -- can't see any aliasing magic being preserves, references reference

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-04-18 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (462 votes), past polls