This is probably completely wrong, but...

What i think is happening is this: the reference to the variable $i is being modified by the auto-(in|de)crement operators before the addition operation because of the precedence table. The reference then gets updated for the left $i for a right-side auto-increment because we're dealing with a reference to the actual variable, not a hard-coded number -- and in the case of the post-increment, because of a bug.

Examples:
Let's say we have the expression

$a + ++$a
This should leave $a at the value 1, but it should evaluate to 1. It evaluates to 2! Because the pre-increment updates the reference to $a on the left side of the addition operator as well. This is fine if it gets documented, but the following is a straight bug:
0 + $a++
We get a return value of 0, and $a is set to 1 as it should be. However if $a is on the left side:
$a + $a++
We get a return value of 1! $a is still set to 1 as it should be. If instead of $a we use some other (zeroed) variable it evaluates to 0 correctly. Somewhere Perl is messing up what happens on a right-side post-increment if the same variable is being added to itself. Maybe it's adding 1 to the left side even if it's a post-increment?

So, to explain the strange ++$a + $a++ i think it works like this:

  1. the code is parsed, the ++'s get the precedence to go first
  2. addition being a left-wise operator, ++$a gets evaluated first; $a gets ++'d to 1
  3. $a++ generates a post-decrement after the statement is over and increments the left-side (erroneously)
  4. the values (2 + 1?) are added returning 3
  5. the post-decrement code is run, and $a is now 2

i could be wrong :/

Here's what i ran with the results i obtained:

#!/usr/bin/perl -w use strict; for $b ( '$a++ ', '0 + $a++', '0 + ++$a', '++$a + $a', '$a + ++$a', '$a + $a++', '++$a + $a++', '$a++ + ++$a', '$a+=$a++' ) { $a = 0; print "$b\t=", eval($b), "\t\$a= $a\n"; } __END__ # results: $a++ =0 $a= 1 0 + $a++ =0 $a= 1 0 + ++$a =1 $a= 1 ++$a + $a =2 $a= 1 $a + ++$a =2 $a= 1 $a + $a++ =1 $a= 1 ++$a + $a++ =3 $a= 2 $a++ + ++$a =2 $a= 2 $a+=$a++ =1 $a= 1
That's a bug imho (though i could be wrong; better explenations anyone?)

hope this helps,
jynx

update: Hmmm, even stranger.

If we take

$a + $a + $a++ # = 0 (correct) $a + $a + ++$a # = 1 (correct) $a + $a++ + $a # = 2 (the trailing $a has no effect maybe?) $a + ++$a + $a # = 3 (the ++ effects previous $a as before?)
Interesting...

So the first addition is probably becoming zero before the second addition is messing it up? And to further confuse things:

$a + $a + ++$a + $a++ # = 2 (correct!) 0 + ++$a + $a++ # = 3 (d'oh!)
i think at this point i've gone too far...

In reply to How about this for $i=$i++ by jynx
in thread $i=$i++ by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.