sandra_henrystocker
Unix Dweeb

Unix: Book review — Perl one-liners

Analysis
Dec 15, 20137 mins

If you get a secret thrill out of a well crafted Perl one-liner, but only have a few that you can pull out of your mental pocket and use, this book may be just what Santa should be leaving under your tree!

Perl One-Liners: 130 Programs that Get Things Done by Peteris Krumins, no starch press, 2014 When I first saw the title of this book, I asked myself “An entire book of one-liners? Is that even possible?”. But, yes, it’s not only possible; it’s actually a good idea. Unlike most books that aim to teach you about programming/scripting by starting with the basics and then slowly adding detail until you’re building complicated programs, this one starts with crystallizations of cleverness — precise, single purpose, no wasted characters code — and then helps you understand what each one-liner does for you and how/why it works. This is an interesting, effective approach and one that is likely to provide you not only with immediately useful time-saving Perl commands, but also with some long lasting insights into how you can make use of Perl’s many features. What is a one-liner and why are one-liners such a big deal? Some people describe one-liners as a “dirty little secret”. That special thrill that some of us get when we can do in one line of text what many others require numerous lines in a script to accomplish is one of the pinnacles of Unix vanity. A one-liner is slick and demonstrates skill with the language in which it is crafted. But it also can serve as the right tool for the job when you want to get a lot of work done without a lot of effort. You type a single line of text on the command line and maybe avoid an hour of potentially boring work or half an hour of trying to do the same thing with a simple script. A one-liner that I’ve probably used hundreds of times or more is one that I generally refer to as the “perl pie” command. It allows me to make changes to a file without opening it or having to make use of a temporary file. It’s extremely useful on the command line or as line in a script that needs to change the content of a file along with many other things. For those of you unfamiliar with perl “pie” commands, they look like this:

$ perl -p -i -e 's/this/that/g' myfile

The “-p -i -e” part of this command could also be expressed as “-pi -e” and this is, in fact, like the example that the author uses to start his journey through 130 really useful Perl one-liners. He shows us the command shown below and asks us to imagine that we have to make repeated word substitutions in a file and to compare this one liner with the process of editing the file and making the changes in a more manual way.

perl -pi -e s/you/me/g' file

The -p -i and -e options don’t have to be provided in the order shown, but I find them easier to remember that way, especially for anyone who likes pie as much as I do. 😉 These three options:

-p	place a printing loop around your command so that it acts on each line of
        input
-i	modify command input in-place
-e	provide the program as an argument (i.e., on the command line)

A similar Perl pie command might change Windows (DOS) style line endings (carriage return, linefeed) to Unix style line endings (linefeed only) or to replace other bytes in a file whether the file is a text file or a binary file. This particular command strips out ^M (carriage return) characters by changing carriage return linefeed sequences to simple linefeeds. This has the same effect as the dos2unix command.

$ perl -p -i -e 's/
sandra_henrystocker

Sandra Henry-Stocker was a programmer, Linux systems administrator, security engineer and Linux journalist for most of her 30-year career. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders. Tune into her 2-Minute Linux video tutorials and take command of your command line.

More from this author