JadeNB has asked for the wisdom of the Perl Monks concerning the following question:
It seems to me that the first and second code paragraphs below should behave identically, and the third should behave nearly identically (except that it increments only once). As shown, the behaviour is quite different (on Perl 5.10.1 and, with the obvious modification, Perl 5.8.9). Am I missing something obvious?
$_ = 'a'; $_++ and say; # => 'b' $_++ and say; # => 'c' $_ = 'a'; $_++ and say for 1..2; # => '2', '3' $_ = 'a'; $_++ and say for 1; # => Modification of a read-only value attempted
UPDATE: Oops, for aliases $_, so I'm not aliasing incrementing the $_ that I think I'm aliasing incrementing. Sigh, sorry ….
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Auto-incrementing strings in for loops
by colwellj (Monk) on Nov 20, 2009 at 04:12 UTC | |
by ikegami (Patriarch) on Nov 20, 2009 at 04:47 UTC | |
by JadeNB (Chaplain) on Nov 20, 2009 at 04:19 UTC |