Review of Perl One-Liners

Hello everyone, as a bit of a departure from my “normal” postings I will be doing a review of a programming book that I recently acquired.

A bit of history before I begin my review; my first foray into the world of Perl programming was a course I took in college entitled Perl for Sys Admins. It was a wonderful course taught by a professor that was very knowledgeable on the subject. For this course we used “the Llama” book (Learning Perl; a standard when it comes to learning Perl). This book has taught me a great deal and is a must-have for anyone starting out in the language.

I should mention (in an effort to be as transparent as possible) that I received a free copy of “Perl One-Liners” thanks to Mr. Krumins when he was doing some promotions on the Perl subreddit (an online community based on Perl). Truth be told had I not received a free copy of this book (thanks to the reddit Perl community) I would probably not have purchased it myself (I don’t generally buy books on programming anymore I tend to rely on internet resources and the like to learn new tricks and techniques). With that in mind there is something to be said for having a physical book in your hand as you are going through the exercises and reading the examples. I’m not the sort of person who can pick up an e-reader and focus my attention on it long enough to garner any real knowledge from it so for me physical books are the way to go.

Enough minutia let’s talk One-liners! The introduction thrusts you head first into the world of one-liners giving you some fairly complex examples that demonstrate the breadth and power of Perl. I am using a windows machine so I had to do a bit of finagling in order to get my programs to work correctly (the author does provide an appendix for windows users). Unfortunately due to constraints beyond the control of the author windows does some “interesting” things with quoting in Perl. I began my first program a simple on that adds double spaces to a file:

perl -pe '$\ = "\n"' file

This works in UNIX but in windows you have do to a bit of annoying tinkering, so my script ended up looking like this:

perl -pe "$\ = \"\n\"" file

…which frankly looks like crap; you have to escape all of those double quotes because if you try to use single quotes windows starts acting like a bit of a dick. I returned to the appendix to find that the author had addressed this problem (after the description of how to download Perl and which bash windows versions he prefers; a section I largely glossed over). I had completely forgotten about the qq/../ operator in perl (an operator that puts the things in between the forward slashes in quotes). Suddenly we’ve done away with those annoying escaped double quotes:

perl -pe "$\ = qq/\n/" file

Much nicer isn’t it? This is one very simple example of the little tricks that the author helps to explain and illuminate throughout his book. It’s nice that he gave some thought to this problem (a problem likely to be encountered by a large portion of his audience).

The first section of the book after the introduction deals a lot with spacing. I ran through most of these one-liners and although I found the one-liners to be pretty interesting, I can’t really think of a time when I would use them. The other sections I found to be more useful than the first one. In the numbering section I found one “one-liner” that I was able to use right out of the box on my chess database created from ICC. This one-liner numbers the amount of times that it finds a matching pattern (in the example I used “QGD: exchange” (which is cool if you don’t have a database program).

perl -ne "print ++$x.qq| $_| if /QGD: exchange/" file

As you can see this becomes fairly handy pretty quickly. Another one-liner I quite enjoyed was this little ditty that creates a random eight-character password:

perl -le "print map{(qq|a|..qq|z|, 0..9)[rand 36]}1..8"

The book has a variety of one-liners that run the gamete of Perl tricks that every Perl developer should have handy. Some of the one liners were a bit rudimentary (like using qq// to create an array; although if you didn’t have the luxury of taking a class this might not be so obvious). I think ultimately where the book lacks is that there seems to be no order (or markers) as far as complexity of the one liner (aside from the size of the explanation). I would think it would make sense to have the easy one liners in the beginning of the chapter and then build up towards the more difficult ones (which seems to be done in some chapters and not others). For example the first one-liner in chapter 4 is titled “Check if a number is prime”. It then shows a one-liner that conceptually quite complex (albeit quite ingenious). Towards the middle of this section it shows you how to print out some familiar math constants, such as Pi or e… these I would think are fairly simple and don’t require a lot of technical finesse or know how (he basically has you call a function that Perl has built-in and prints out the desired accuracy).

Additionally it would be cool if there were more examples provided for when some of these one-liners might be useful (or case studies). Some of the one-liners the author has provided examples for when you might use them; but others we are left in the dark. The author does a good job of explaining some fairly complicated things in layman’s terms (such as the default variable and $. which is a variable I hadn’t used before but turned out to be quite handy). Although the prose is not as amusing as the llama book overall I thought the book was quite interesting and had useful tools and code snippets for the reader to peruse. As we are all aware there are many ways to do things in Perl and what’s cool in this particular book is that the author provides numerous examples of slightly different ways to reach the same goal. I am reminded of a quote by Larry Wall:

Although the Perl Slogan is There’s More Than One Way to Do It, I hesitate to make 10 ways to do something.

I think the book has a little something for everyone; if you’re interested in learning more (about the book or the author) check out his blog. Additionally the book can be purchased on amazon or via the publisher directly.

 

Leave a Reply

Your email address will not be published. Required fields are marked *