In this:

my $i = 0; my ($j, $k) = (++$i, ++$i); print "\$j: $j\t\$k: $k\n"; $j: 2 $k: 2

It first evaluates the ++es (because it's a pre-increment, so it happens before anything else), so $i = 2. Then, it builds a list of (2, 2), and finally assigns that list to ($j, $k).

In this:

my $i = 0; my ($j, $k) = ($i++, $i++); print "\$j: $j\t\$k: $k\n"; $j: 0 $k: 1

Because it's a post-increment, the ++es get evaluated *after* $i is "evaluated" to build the list. So the first $i is evaluated, it's 0, so 0 goes into the list. Then the ++ happens, setting $i = 1. Then the next $i is evaluated, putting 1 into the list, and finally $i is incremented again. End result, a list of (0, 1) is assigned to ($j, $k).

But as others have pointed out, having multiple ++es and --es in a statement is Bad Juju. What I've explained is what happens in current perls, but I wouldn't trust it to work the same in the next point release of perl 5, let alone in perl 6. See perlop.


In reply to Re: Pre vs Post Incrementing variables by DrHyde
in thread Pre vs Post Incrementing variables by SavannahLion

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.