my $i = 0;
in reply to post-increment/pre-increment blues
- "print ++$i + 1;" -- increments and prints 2.
- "print $i++ + 1;" -- prints 2 and increments.
- "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.
- "print ++$i + ++$i;" -- You guessed it! Same as above!
- "print ++$i + $i++ + 1;" -- Yup. Same again.
For related fun, try the various "$i=$i++" (also undefined).