That's a lot of questions! Here goes...
I'll do two examples, $x,$x,$x++:
{ local @_; alias $_[0] = $x; alias $_[1] = $x; alias $_[2] = $x++; &print; # @_ = ($x, $x, $anon) = (1, 1, 0) }
$x,$x++,$x++:
{ local @_; alias $_[0] = $x; alias $_[1] = $x++; alias $_[2] = $x++; &print; # @_ = ($x, $anon1, $anon2) = (2, 1, 0) }
$x,$x++,$x++, on a system that does the alias statments in a different order:
{ local @_; alias $_[2] = $x++; alias $_[1] = $x++; alias $_[0] = $x; &print; # @_ = ($x, $anon1, $anon2) = (2, 0, 1) }
Your compiler probably does something like:
load x inc push load x push call printf
C doesn't guarantee the order either.
The return value of $x++ is not $x. It's a new variable that contains the old value of $x. $x is incremented before the print call, but you are aren't printing $x. Check perlop.
# $x | anon returned | $_[0] | $_[1] # | by $x++ | | # ------+-----------------+---------+--------- my $x = 0; # 0 | -- | -- | -- { # 0 | -- | -- | -- local @_; # 0 | -- | undef | undef alias $_[0] = $x; # 0 | -- | 0 | undef alias $_[1] = $x++; # 1 | 0 | 1 | 0 &print; }
or in even more detail
# $x | anon returned | $_[0] | $_[1] # | by $x++ | | # ------+-----------------+---------+--------- my $x = 0; # 0 | -- | -- | -- { # 0 | -- | -- | -- local @_; # 0 | -- | undef | undef alias $_[0] = $x; # 0 | -- | 0 | undef my $anon = $x++; # 1 | 0 | 1 | undef alias $_[1] = $anon; # 1 | 0 | 1 | 0 &print; }
Guess what the following prints:
perl -le"$x=3; sub { $_[1]++; $_[2]++; print @_ }->($x+0, ++$x, $x++, +$x+0, $x);"
On my system, it prints 36556.
my $x = 3; # $anon0 | $x | $anon2 | $anon3 | $x { # $_[0] | $_[1] | $_[2] | $_[3] | $_[4] local @_; # --------+-------+--------+--------+------- # $x+0 my $anon0 = $x+0; alias $_[0] = $anon0; # 3 | -- | -- | -- | -- # ++$x $x=$x+1; # 3 | -- | -- | -- | -- alias $_[1] = $x; # 3 | 4 | -- | -- | -- # $x++ my $anon2 = $x; alias $_[2] = $anon2; # 3 | 4 | 4 | -- | -- $x=$x+1; # 3 | 5 | 4 | -- | -- # $x+0 my $anon3 = $x+0; alias $_[3] = $anon3; # 3 | 5 | 4 | 5 | -- # $x alias $_[4] = $x; # 3 | 5 | 4 | 5 | 5 &anon_sub; # $_[1]++; # 3 | 6 | 4 | 5 | 6 # $_[2]++; # 3 | 6 | 5 | 5 | 6 # print @_; }
Notice how $x and $x+0 are different.
Notice how $_[1]++ affects more than one argument.
Notice how $_[1]++ affects $x and ++$x.
Notice how $_[1]++ doesn't affect $x+0 and $x++.
In reply to Re^3: print behavior
by ikegami
in thread print behavior
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |