Re: Order of execution of functions in list
by ikegami (Patriarch) on Sep 13, 2013 at 13:23 UTC
|
Operand evaluation order isn't defined for all operators[1], but it is for this one.
Quote perlop,
In list context, [,] is just the list argument separator, and inserts both its arguments into the list. These arguments are also evaluated from left to right.
That's perfectly fine.
- For example, Perl doesn't define the order in which f, g and h will be called by f() + g() + h().
| [reply] [d/l] [select] |
|
|
So in function calls it's not a list argument separator ?
function($i++,++$i) ???
Cheers Rolf
( addicted to the Perl Programming Language)
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
|
|
Ok, thanks! That was exactly what I looked for!
| [reply] |
Re: Order of execution of functions in list
by ikegami (Patriarch) on Sep 13, 2013 at 13:59 UTC
|
my ($a1, $a2) = splice(@_, 0, 2);
| [reply] [d/l] |
Re: Order of execution of functions in list
by LanX (Saint) on Sep 13, 2013 at 12:51 UTC
|
Reading the linked discussions reveals that the problem is related to lvalues and composed expressions (like ++$i) with side effects.
shift is a simple expression (a builtin in this case), has no side-effects and returns an rvalue.
Cheers Rolf
( addicted to the Perl Programming Language)
| [reply] [d/l] [select] |
|
|
$ 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
| [reply] [d/l] [select] |
|
|
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)
| [reply] |
|
|
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)
| [reply] [d/l] [select] |
|
|
|
|
|
|
|
has no side-effects
shift() actually modifies @_, isn't it side-effect?
if shift()s executed in different order, result will be different.
| [reply] |
|
|
| [reply] [d/l] |
Re: Order of execution of functions in list
by Marshall (Canon) on Sep 13, 2013 at 13:20 UTC
|
Well first, there is a problem with $a and $b in Perl. These are special variables used in for example the sort function. Use $x and $y instead.
I have bench-marked the shift vs array list assignment.
my $X = shift; my $Y = shift; vs my ($X,$Y) = @_; The second version is faster with more than one variable.
my ($a1, $a2) = (shift, shift);
Better is:
my ($X,$Y) = @_;
| [reply] [d/l] [select] |
|
|
Well first, there is a problem with $a and $b in Perl
1. there is no $a and $b in my code.
2. it's not a problem, at least if you are not defining comparison function in same scope.
| [reply] |
|
|
That was just proof-of-concept example, my original code a bit more complex and I care more about readability than performance in my particular case.
| [reply] |