That's a lot of questions! Here goes...

Answer to Question 1

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) }

Answer to Question 2

Your compiler probably does something like:

load x inc push load x push call printf

C doesn't guarantee the order either.

Answer to Question 3

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.

Answer to Question 4

# $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; }

Answer to Question 5

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.