$i = $i ++; Its behaviour is undefined.
It is defined. The ++ operator has nothing to do with this, in Perl. You're right about perlop, and with multiple increments/decrement the order in which things happen is undefined (well, not always. But it is not clear either). But that isn't really what we're dealing with. This is an assignment, just like any $i = EXPR. In this case, EXPR is $i++, which causes $i to be incremented, but returns the old value. The value of EXPR is assigned to $i, so as an end result nothing happens. If $i is tied, FETCH, STORE and STORE are called.
$i = 5;
$i = $i++;
print $i, "\n"; # 5\n
- $i is set to 5
- $i is incremented, new value is 6
- old value (5) is used as the rhs of the assignment operator
- $i is set to that value, old value was 6, new value is 5
- $i and newline are printed
$i = 5;
$i = ++$i;
print $i, "\n"; # 6\n
- $i is set to 5
- $i is incremented, new value is 6
- new value (6) is used as the rhs of the assignment operator
- $i is set to that value, old value was 6, new value is 6
- $i and newline are printed
I have tried this with Perl 5.005 and 5.6.1.
Note: I agree that $i = $i++ is bad style and should never ever be used in non-obfu.
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.