$a = $b = $c = 1; print $a++, " ", $a++, "\n"; print $b++, " ", $b, "\n"; print $c, " ", $c++, "\n";

Isn't order of operations fun? I'm not sure I completely understand this either. As far as I can tell, although addition is generally left-associative, all operations in the equation (++ operators) are run first. The auto-increment operators have higher precedence than the addition operator. The first two equations in your example are easy to understand. The first $a++ returns 1, which is added to the second $a++ which returns 2, since the inital ++ has already incremented the variable. The $b equation is much the same, since the post-increment operator is superfluous. The final equation is tricky. Because of precedence, the auto-increment runs first, setting the value of the rhs of the equation to 1, and setting $c to 2, which makes that the lhs. Keep in mind, that the addition operator operates on the values returned by the auto-increment operators at the time that they ran.

Edit: As Abigail-II pointed out, I have no idea what I'm talking about in terms of standards. I am curious, though, why the standards-maintainers chose to keep this behavior undefined.


In reply to Re: Auto-increment frenzy by dbp
in thread Auto-increment frenzy by Dist

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.