Perl One-Liners |
Author: Peteris Krumins This book's subtitle "130 Programs That Get Things Done" tells us exactly what to expect. Is it a good addition to your bookshelf? You could replace the subtitle with "How to use Perl as a shell tool that replaces awk,sed,grep, and others". This ability to do so many tasks is the reason why Perl is nicknamed the "Swiss-Army chain saw" of UNIX tools. This aspect of Perl's greatness is easily forgotten and left in the shadows. So the only fate One Liners would normally aspire to would be to be relegated to a lonely corner of a book. But here they are put under the spotlight and have a book wholly dedicated to them. The great thing about this collection is that the tips it provides can be immediately applied to daily activities. This does not mean, however, that the book is merely reduced into a listing of commands to be blindly executed in a copy and paste fashion. On the contrary, Peteris Krumins goes to great lengths explaining not only what each command does, but also how it does it. Therefore, you get to comprehend the underlying mechanisms that will aid you in creating your own commands in the future
Peteris Krumins kicks off by presenting short samples with impressive results, which pave the way for what's to follow - seven chapters broken down into distinct categories according to what they focus on :
So, the chapter on Spacing contains recipes for changing line and word spacing. Among the cleverest examples there's a script to find all lines in a file that appear more than once:
and another for importing Perl modules for more powerful processing:
Here the most effort required is in getting your combination of command line switches right, something covered throughout the book.
The chapter on Calculations contains more involved examples, like counting and calculating dates, for example checking if a number is a prime:
which is nicely deconstructed and looked at in detail. Other examples include finding the minimum element on each line, converting an IP address to an unsigned integer and printing the date 14 months, 9 days, and 7 seconds ago.
Chapters 6,7 and 8 contain useful tips for manipulating text and for Web-related tasks as well as for Word-processing. Examples include base64-encode a string; url-escape a string; html-decode; converting all text to uppercase; and stripping leading whitespace from the beginning of each line:
Another example, that of substituting “foo” with “bar” on lines that match “baz”:
is expanded and shown to be equivalent to:
This is a perfect demonstration of how one liners lead to optimized and efficient coding Chapter 7 and 8 offer solutions to frequent tasks undertaken by shell warriors - extracting, printing and matching relevant data.
Summing up, this is a useful book for admins, shell users and coders alike who need their tools concentrated into one toolbox and not spread here and there. Having the book to hand will allow you to call on Perl's full power in an instant when the need arises.
|
|||
Last Updated ( Friday, 10 November 2017 ) |