Item 7: Use $_ for elegance ."Dollar underscore ," or $_ : you may love it, or you may hate it. But either way, if you're going to be a proficient Perl programmer, you've got to understand it. $_ is a default argument for many operators, and also for some control structures. Here are some examples: $_ as a default argument
The last example illustrates the special case in which using the line input operator < filehandle > alone as the condition of a while loop is a shortcut for reading a line from the file into $_ until the end of file is reached. This is by no means an exhaustive list. Consult the on-line documentation or your favorite comprehensive Perl reference for more information. $_ is a normal scalar variablemostly. You can use it, print it, change its value, and so on, just as if it were an ordinary scalar. There are a couple of things to watch out for, though. $_ and the main package$_ is always in the package main . This applies even, or especially , if you are in some other package:
In fact, all special variables ( $ -punctuation) have this property. You can use a variable like $foo::_ if you like, but it has no special properties and isn't "the" $_ . Localizing $_$_ can only be localized using local . You cannot use my . The following won't work, no matter where or how you try it:
Programming style and $_Oddly enough, when you use $_ , you may not see much of it:
Some Perl programmers may feel that $_ is more an aid to obfuscation than elegance. I have a book on my shelf that opines: "Many Perl programmers write programs that have references to $_ running like an invisible thread through their programs. Programs that overuse $_ are hard to read and are easier to break than programs that explicitly reference scalar variables you have named youself." I find this hard to accept. Is: while (defined ($line = <STDIN>)) { print $line if $line =~ /Perl/ } really superior to: while (<STDIN>) { print if /Perl/ } You'll have to decide. |