Perl One-Liners

Author: Peteris Krumins
Publisher: No Starch Press
Pages: 168
ISBN: 978-1593275204
Print: 159327520X
Kindle: B00GS9BZLU
Audience: Perl shell script users
Rating:4
Reviewer: Nikos Vaggalis

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

 

 

Banner

 

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 :

  • Spacing (Chapter 2),
  • Numbering (Chapter 3)
  • Calculations (Chapter 4)
  • Working with Arrays and Strings (Chapter 5)
  • Text Conversion and Substitution (Chapter 6)
  • Selectively Printing and Deleting Lines (Chapter 7)
  • Useful Regular Expressions (Chapter 8)

 

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:

perl -ne 'print if $a{$_}++' file

and another for importing Perl modules for more powerful processing:

perl -MPOSIX -le '@t = localtime; $t[3] -= 1299; print scalar localtime mktime @t'

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: 

perl -lne '(1x$_) !~ /^1?$|^(11+?)\1+$/ && print "$_ is 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:

perl -ple 's/^[ \t]+//' 

Another example, that of substituting “foo” with “bar” on lines that match “baz”: 

perl -pe '/baz/ && s/foo/bar/'

is expanded and shown to be equivalent to:

 while (defined($line = <>)) {
 if ($line =~ /baz/) {

  $line =~ s/foo/bar/

 }

}

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.

 

Banner


Android Programming: The Big Nerd Ranch Guide (5e)

Authors: Bryan Sills, Brian Gardner, Brian Hardy and Kristin Marsicano
Publisher: Addison-Wesley
Pages: 688
ISBN: 978-0137645541
Print: 0137645546
Kindle: B09WLF84W7
Audience: Kotlin programmers
Rating: 4.5
Reviewer: Mike James  

The Big Nerd Ranch Guide to Android is bac [ ... ]



Core Java for the Impatient, 3rd Ed

Authors:  Cay S. Horstmann 
Publisher: Addison Wesley
Pages: 576
ISBN: 9780138052102
Print: 0138052107
Kindle: B0B8RZZBDJ
Audience: Smart programmers wanting in-depth coverage
Rating: 4.8
Reviewer: Mike James

The key to this book is the word "impatient" in the title. What does this m [ ... ]


More Reviews

Last Updated ( Friday, 10 November 2017 )