| [reply] |
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.
| [reply] [d/l] [select] |
I may be mistaken, but I'm think that $c+=1 is actually defined to run the two steps one right after the other, while what is ambiguous in $c++ is the fact that $c will be incremented later.
I do agree on the fact that undefined behaviour does not mean undefined value, that's why it's hard to track.
| [reply] [d/l] [select] |
Sorry, you are right, I originally made a mistake when copying the values, my third example gives (3, 2), not (2, 3) as I originally typed by mistake. I almost immediately corrected it, but it appears you had time to see it before I corrected my error, even though I corrected within 2 minutes of the original post.
| [reply] |