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


The Art of WebAssembly

Author: Rick Battagline
Publisher: No Starch
Date: May 2021
Pages: 304
ISBN: 978-1718501447
Print: 1718501447
Kindle: B08TSYXJTS
Audience: WebAssembly developers
Rating: 5
Reviewer: Ian Elliot
WebAssembly is the coming thing - or so we are told.



Microsoft Azure For Dummies, 2nd Edition

Author: Jack A. Hyman
Publisher: For Dummies
Pages: 416
ISBN: 978-1119898061
Print:1119898064
Kindle: B0BNWG1HYK
Audience: Azure novices?!
Rating: 1 or 4.5 (see review)
Reviewer: Ian Stirk

This book aims to provide a gentle yet thorough introduction to Microsoft Azure, how does it fare? 


More Reviews

Last Updated ( Friday, 10 November 2017 )