Yes, you are right. In C, if you write:
c = c++;
the behavior is not defined by the C standard. Which does not mean that the compiler will return an undefined value. Just the implementer of the compiler is free to do whatever she or he wants. And I remember very well having tried it about 16 years ago with GCC under Linux and another compiler under Windows and having obtained the original value of c for one of them and the incremented value of c for the other (although I do not remember which one gave what). For this type of things, the Perl syntax is rooted in C, but, in a certain way, the behavior is not imposed by a standard which may be silent about certain edge cases, but is sort of defined by the Perl compiler itself. The Perl compiler gives the logical result that I would expect (although what the logical result should be might be considered debatable):
my $c = 2; $c = $c ++ ; # $c is now 2 $c = ++$c ; # $c is now 3
And it seems to me that using $c and $c+=1 in the same expression falls into the same category as using $c and $c++ (or ++c) in the same expression, something not very well defined, which is why I also brought those additional examples. Your explanation using operator precedence does make sense (I had not seen it when I posted my examples), but it might simply just be one of those not well-defined behavior cases.

In reply to Re^4: Why does the first $c evaluate to the incremented value in [$c, $c += $_] ? by Laurent_R
in thread Why does the first $c evaluate to the incremented value in [$c, $c += $_] ? by smls

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.