The operations that default on $_ will use the lexically-scoped version of $_ when it exists, instead of the global $_.
In a map or a grep block, if $_ was previously my'ed, then the $_ inside the block is lexical as well (and scoped to the block).
In a scope where $_ has been lexicalized, you can still have access to the global version of $_ by using $::_, or, more simply, by overriding the lexical declaration with our $_.
can break because of a bug with Perl tied variables that may bite you in the neck first or later:
#!/usr/bin/perl -l
use strict;
use warnings;
use Tie::Array;
my @q = my @q0 = qw/foo bar/;
sub foo {
local $_ = 'baz';
print "@q $_";
}
foo for @q;
tie @q, 'Tie::StdArray';
@q=@q0;
print '-' x 11;
foo for @q;
__END__
Snippets of code should be wrapped in
<code> tags not<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).