Perl One-Liners

Author: Peteris Krumins
Publisher: No Starch Press
Pages: 168
ISBN: 978-1593275204
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.

Recommended

 

Banner


Managing Humans 2nd Ed

Author: Michael Lopp
Publisher: Apress
Pages: 281
ISBN: 978-1430243144
Audience: Aspiring managers
Rating: 3.5
Reviewed by: Sue Gee

Billed as "Biting and Humorous Tales of a Software Engineering Manager". Does it live up to expectations?



Fundamentals of Game Design (2nd ed)

Author: Ernest Adams
Publisher: New Riders, 2009
Pages: 700
ISBN: 978-0321643377
Aimed at: Hands-off designers
Rating: 1
Pros: Not actually misleading, comprehensive
Cons: Boring, unmotivating, too keen on being comprehensive
Reviewed by: Mike James

The back jacket praise for this book says it provides "enc [ ... ]


More Reviews

Last Updated ( Monday, 27 January 2014 )
 
 

   
RSS feed of book reviews only
I Programmer Book Reviews
RSS feed of all content
I Programmer Book Reviews
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.