Perl One-Liners

When you write a Perl script, much of the time you'll write it as you have throughout this book—putting the script into a file, and then using the Perl interpreter to run that script. But sometimes there might be a task that's just really simple, or there might be something that you only need to do once (or very infrequently). For these kinds of tasks, it's almost a waste of time to start up an editor to write an actual script. For just this reason there are Perl one-liners.

Perl one-liners are scripts that you type directly on the Perl command line. They aren't saved anywhere; if you get them wrong you'll have to type them again.

To create a Perl one-liner, use the -e option, followed by the script inside quotes, like this:

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.