Robert's Perl Tutorial

http://www.sthomas.net/roberts-perl-tutorial.htm


The infamous $_

In fact, that gets shorter. And now I need to introduce you to $_ , which is the Default Input and Pattern Searching Variable.

foreach (@names) {
        print "$_";
}

If you don't specify a variable to put each element into, $_ is used instead as it is the default for this operation, and many, many others in Perl. Including the print function :

foreach (@names) {
        print ;
}

As we haven't supplied any arguments to print , $_ is printed as default. You'll be seeing a lot of $_ in Perl. Actually, that statement is not exactly true. You will be seeing lot of places where $_ is used, but quite often when it is used, it is not actually written. In the above example, you don't actually see $_ but you know it is there.