Table of Contents 

TPJ One Liners

TPJ One-Liner #28

perl -0nal012e '@a{@F}++; print for sort keys %a'

Extracts, sorts, and prints the words from a file.

--Peter J. Kernan

TPJ One-Liner #29

This subroutine accepts a string and returns a true value if all of the parentheses, brackets, and braces in the string are balanced.

sub is_balanced {
   my $it = $_[0];
   $it =~ tr/()[]{}//cd;
   while ($it =~ s/\(\)|\[\]|\{\}//g) { 1 }
   return !length($it);

­Sean M. Burke

TPJ One-Liner #30

"Regular expressions are to strings what math is to numbers."

--Andrew Clinick, discussing what Microsoft thinks of Perl


Short answer: They like it.

TPJ One Liner #31

perl -e 'print "Internet Time @",
int (((time + 3600) % 86400)/86.4), "\n";'

Swatch's Internet Time, heralded as a revolutionary way of measuring time independent of geography. See for details.


TPJ One Liner #32

A trick for indenting here strings:

($definition = <<'FINIS') =~ s/^\s+//gm;

The five varieties of camelids are the familiar camel, his friends the llama and the alpaca, and the rather less well-known guanaco and vicuña.


--The Perl Cookbook

TPJ One Liner #33

Efficiently finding the position of the first and last occurrences of a substring in a string:

$first = index($string, $substring);
$last = rindex($string, $substring);

TPJ One Liner #34

Some scalars that Perl defines for you:

$^O contains the name of your operating system.

$^T contains the time at which your program began.

$0 contains the name of your program.


Table of Contents