On Thursday, November 14, 2002, at 01:04 PM, Matthew O. Persico wrote:
Actually, in some of them it looks like it's so that $_ can be used as the implicit target of a regular expression. I do that sometimes, particularly when writing something like a string tokenizer, because it reduces clutter quite a bit:On Thu, 14 Nov 2002 11:49:56 +1100, Ken Williams wrote:Incidentally, I'd just write these aslocal $_ = $o->{lines}[0];As explained, you usually use $_ as assigned by Perl in for, map ,grep. From these snippets, it seems like $_ is being assigned and used because: 1) being too lazy (bad lazy) to use a 'my' 2) misguidedly think that not using 'my' saves a significant number of cycles 3) being too cute.
local $_ = shift();
if ( /^foo/gc ) {
....
} elsif ( /^bar/gc ) {
....
} elsif ( /^baz/gc ) {
....
} elsif ( /^quux/gc ) {
....
}
But yeah, if you're using $_ as a regular variable simply because perl lets you use it without declaring it, you're asking for trouble.
-Ken