david nicol
Thu, 10 Oct 2002 13:24:28 -0700
Oh I get it -- it's because $_ within for(){} is supposed to be
an alias not a copy. Which is easy to forget. In fact the
capitalization of the names in my example would be an unexpected
side effect, since I forgot about it when I wrote the example.
This warning message they're introducing will keep the forgetful
from making quite so many unintended side effects. Good.
Andrew Moore wrote:
> On Thu, Oct 10, 2002 at 02:42:59PM -0500, david nicol wrote:
>
>>I don't want to bother perl5-porters with my questions...
>>
>>
>>>=head2 Modifying $_ Inside for(..)
>>>
>>> for (1..5) { $_++ }
>>>
>>>works without complaint. It shouldn't. (You should be able to
>>>modify only lvalue elements inside the loops.) You can see the
>>>correct behaviour by replacing the 1..5 with 1, 2, 3, 4, 5.
>>
>>Is there a bug? There a lots of places where you want to modify $_
>>within loops. Consider:
>>
>> for (@names){
>> s/\b([a-z])/toupper($1)/g;
>> print;
>> }
>>for one example.
>>
>>
>>Who says that modifying $_ here causes a problem? It doesn't
>>break the looping. Do they want it break the looping, as in
>>a for(;;) construction?
>
>
> I believe the point is that @names is an lvalue. If you want
> to loop over the integers 1 through 5 _and_change_them_, use an lvalue.
>
> my @fiver = (1..5);
> foreach ( @fiver ) {
> $_++;
> }
>
> or somesuch. Know what I mean?
>
> -Andy
>
>
>