Perl One-Liners

Perl One-Liners

130 Programs That Get Things Done
by Peteris Krumins
November 2013, 168 pp.

Part of the fun of programming in Perl lies in tackling tedious tasks with short, efficient, and reusable code. Often, the perfect tool is the one-liner, a small but powerful program that fits in one line of code and does one thing really well.

In Perl One-Liners, author and impatient hacker Peteris Krumins takes you through more than 100 compelling one-liners that do all sorts of handy things, such as manipulate line spacing, tally column values in a table, and get a list of users on a system. This cookbook of useful, customizable, and fun scripts will even help hone your Perl coding skills, as Krumins dissects the code to give you a deeper understanding of the language.

You'll find one-liners that:

  • Encode, decode, and convert strings
  • Generate random passwords
  • Calculate sums, factorials, and the mathematical constants π and e
  • Add or remove spaces
  • Number lines in a file
  • Print lines that match a specific pattern
  • Check to see if a number is prime with a regular expression
  • Convert IP address to decimal form
  • Replace one string with another

And many more! Save time and sharpen your coding skills as you learn to conquer those pesky tasks in a few precisely placed keystrokes with Perl One-Liners.

Author Bio 

Peteris Krumins is a programmer, systems administrator, start-up manager, and all-around hacker. He is the cofounder of Browserling and Testling, and he runs the popular programming blog catonmat.

Table of contents 


Chapter 1: Introduction to Perl One-Liners (PDF)
Chapter 2: Spacing
Chapter 3: Numbering
Chapter 4: Calculations (PDF)
Chapter 5: Working with Arrays and Strings
Chapter 6: Text Conversion and Substitution
Chapter 7: Selectively Printing and Deleting Lines
Chapter 8: Useful Regular Expressions

Appendix A: Perl’s Special Variables
Appendix B: Using Perl One-Liners on Windows
Appendix C: perl1line.txt

View the detailed Table of Contents (PDF)
View the Index (PDF)


"A great reference for when you want to do something with Perl without spending an hour or two trying to figure out how."
Sandra Henry-Stocker, IT World (Read More)

"By reading this book and trying the recipes, you’ll master shortly all the command-line options that can turn the Perl interpreter into a sed-awk-grep-toolboox-on-steroids."
Alexis Sukrieh, creator of Perl Dancer (Read More)

"Having the book to hand will allow you to call on Perl's full power in an instant when the need arises."
Nikos Vaggalis, I Programmer (Read More)

"This is a tinkerer's dream. What's more, its a book that you can grab at any old time and play with for the fun of it, and yes, this book is fun."
Michael Larsen, TESTHEAD (Read More)

“One of the slogans used by Perl is ‘Easy things should be easy and hard things should be possible.’ This book illustrates just how easy things can be—and how much can be done with so little code.”
David Precious, contributor to the Perl Dancer project and various CPAN modules

“By reading this book you can make a step toward becoming the local computer wizard, even without learning how to program.”
Gabor Szabo, founder and editor of the Perl Weekly newsletter

“A set of exercises for deepening your understanding of Perl.”
John D. Cook, Singular Value Consulting

“The author is enthusiastic about the material and uses an easy writing style. Highly recommended.”
Thrig (Jeremy Mates), Internet plumber

“These one-liners are great. Simple. Clear. Concise.”
Jonathan Scott Duff, Perl guru

“A quick read full of useful command-line Perl programs.”
Chris Fedde, systems engineer and Perl enthusiast

“Handy for anyone who does a lot of one-off text processing: system administrators, coders, or anyone with large amounts of data they need shifted, filtered, or interpreted.”
Jim Davis, Perl developer

Check out Tips2Secure's interview with the author


Page 40:
The third text paragraph should read "In the list context, both gmtime and localtime return a nine-element list..."