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):c = c++;
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.my $c = 2; $c = $c ++ ; # $c is now 2 $c = ++$c ; # $c is now 3
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |