Using the $_ (default) Variable

Congratulations! You now know (just about) everything you need to know about loops in Perl (and anything else you're curious about, you can find out in “Going Deeper.”) Let's finish up with two general topics that we'll use quite a lot throughout the rest of this book: the special $_ variable, and the syntax for reading input from files on the Perl command line.

First, the $_ variable. This is a special variable in Perl that you can think of as being a default placeholder for scalar values. Lots of different constructs in Perl will use the $_ if you don't give them a specific scalar variable to deal with; this has the advantage of making your scripts shorter and more efficient—but sometimes it can make them more ...

Get Sams Teach Yourself Perl in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.