Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re^3: Why does the first $c evaluate to the incremented value in [$c, $c += $_] ?

by Eily (Parson)
on Mar 04, 2014 at 21:34 UTC ( #1076969=note: print w/replies, xml ) Need Help??

in reply to Re^2: Why does the first $c evaluate to the incremented value in [$c, $c += $_] ?
in thread Why does the first $c evaluate to the incremented value in [$c, $c += $_] ?

I believe this falls into the undefined behaviour of auto increment operator :

Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behavior.
So because of the precedence list, you know the right part will be run first, and the left part after that, but when exactly the variable will be incremented is left to Perl's implementation.

  • Comment on Re^3: Why does the first $c evaluate to the incremented value in [$c, $c += $_] ?

Replies are listed 'Best First'.
Re^4: Why does the first $c evaluate to the incremented value in [$c, $c += $_] ?
by Laurent_R (Canon) on Mar 04, 2014 at 22:17 UTC
    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.

      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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076969]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2017-12-14 03:23 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (384 votes). Check out past polls.