$_ is set by very few things:
- An explicit assignment ($_ = 35).
- The implicit setting for a while() loop reading from a filehandle:
while (<HANDLE>) { ... } # same as ...
while (defined($_ = <HANDLE>)) { ... }
- The implicit variable of a foreach loop:
foreach (@list) { ... } # same as foreach $_ (@list) { ... }
- The implicit variable of a grep/map block (added after prodding, sorry):
$count = grep { BLOCK } LIST;
@result = grep { BLOCK } LIST;
@result = map { BLOCK } LIST;
map { BLOCK } LIST; # invalidates your warranty
I think that's it. I don't recall anything else that can set the value. Lots
of operations can alter the value, like chomp or s///.