in reply to Re: Re: Stumped with $_ being modified
in thread Stumped with $_ being modified

There's a difference between lexically scoping with my and localising using local, although the terminology is one that seems rather contentious. Like the difference between arrays and lists.

There are only a few things you still need to localise with local, such as the $" variable. Ever since 5.0 came out, a lot of effort has been put into moving people away from local and to my, for example, the way you can use lexically scoped filehandles. Where possible, it's good to use filehandles like that since they can be passed around easily from subroutine to subroutine, or stored in an object's hash easily. In this example, though, I'm using Old-School handles because it's not really an issue.

As for loops, I won't hesitate to use $_ as long as it doesn't get too complicated. Where it's obvious what's being iterated, that is. For example:
$_->iron() foreach (@shirts);
You could say my $shirt, but it would be redundant. On the other hand, where there's no hint as to what you're using, a simple my declaration acts as documentation.
foreach my $shirt ($laundry->contents()) ...
You have to be careful with $_, just like with $1 and its relatives. Where there's risk of contamination, I use alternate names or copies. If there's no risk, then it's a matter of preference.

I think my point is really this: Don't use local $_, instead, use a named lexical variable such as my $line.