Instead the post-(in|de)crement operators seem to be squireling away their value of $x when it came by them in a left to right evaluation of the terms.
Post-increment and -decrement have to squirrel away their value of $x, because they change the value of $x in place but return the previous value.

Pre-increment and -decrement, on the other hand, change the value of $x in place and then return the new value. Returning a pointer to the location of $x, rather than to a copy of the value in $x, is an optimization.

$x = 3; print ++$x/$x--;
Coming into the function x is 3, it gets pre-incremented (now 4), it's divided by x (still 4), x is post-decremented (to 3). Print outputs 1 (4/4) and x leaving the function is 3. This is how it _should_ happen to my mind, instead it's 3/4 as stated earlier.
You appear to have the order of evaluation all wrong. The operands to /, this case ++$x and $x--, must both be evaluated before the division. What you describe sounds more like: (++$x/$x)-- except, of course, that it's a syntax error.

By the way, you'll see the same results with other operations that modify variables in place, such as +=. print $x, $x += 1; Creating a separate copy of every value in every variable, to prevent such effects, would cost a lot in execution time and memory usage. It could also get very complicated with constructions like this: print +(($x+=1)+=1) + ($x+=$x++);


In reply to Re: Re: Re: Re: Re: For loop problem by chipmunk
in thread For loop problem by srawls

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.