http://www.perlmonks.org?node_id=175320


in reply to post-increment/pre-increment blues

my $i = 0;
  1. "print ++$i + 1;" -- increments and prints 2.
  2. "print $i++ + 1;" -- prints 2 and increments.
  3. "print $i + $i++;" -- isn't documented to do anything particularly useful in Perl. "perl" might have some particular behaviour, but it is in no way guaranteed in any way or form. Other languages have multiple implementations; that makes separating implementation peculiarities from the language definition easier.

    In fact, the equivalent C code "i+(i++)" is undefined behaviour in ISO C: the compiler is allowed to return (assuming the variable starts off with 2) any value, or write code to format your computer's disks, or write code to format everybody else's computers' disks, or cause daemons to fly out of your nose.

  4. "print ++$i + ++$i;" -- You guessed it! Same as above!
  5. "print ++$i + $i++ + 1;" -- Yup. Same again.

For related fun, try the various "$i=$i++" (also undefined).