There is no general automatism to set $_ like your example implies, that's just magic behavior of the file iterator <> in scalar context.
Um, not quite, thats part of the magic of while not readline
$ perl -MO=Deparse,-p -le "while(<>){print}"
BEGIN { $/ = "\n"; $\ = "\n"; }
while (defined(($_ = <ARGV>))) {
print($_);
}
-e syntax OK
$ perl -MO=Deparse,-p -le "while($f=<>){print}"
BEGIN { $/ = "\n"; $\ = "\n"; }
while (defined(($f = <ARGV>))) {
print($_);
}
-e syntax OK
$ echo hi |perl -le "while($f=<>){print}"
You see
<> in scalar context didn't set $_