PREVIOUS  TABLE OF CONTENTS 

The Perl Journal One-Liners

TPJ One Liner #21

Picking random elements from an array:

srand;
$item = $array[rand @array];

TPJ One-liner #22

If you're trying to get Windows to generate a PostScript file,
but it wraps the file with PCL junk, you can remove it with this:

perl -ni -e "!$g&&s/^.*(%!.*)/$1/&&$g or print;last if /^%%
EOF/"

TPJ One-liner #23

In the movie Sphere, the commands that Harry typed to translate the message were taken from Tom Christiansen's FAQ:

$BSD = -f '/vmunix'; if ($BSD) {system "BIN
cbreak </dev/tty >/dev/tty 2>&1
sub set_break { # &setset_cbreak(1) or &set_cbreak(0)
      local($on) = $_[0];
	  local($sgttyb,@ary);
	  require 'sys/ioctl.ph';

Courtesy of Brendan O'Dea

TPJ One-liner #24

Seperate the header and body of a mail message into strings:

while (<>)  {
    $in_header =   1  ../^$/;
	$in_body   = /^$/ ..eof();

Courtesy of the Perl Cookbook

TPJ One-Liner #25

Simple numeric tests:
warn "has nondigits" if /\D/;
warn "not a natural number" unless /^\d+$/;
warn "not an integer" unless /^'?\d+$/;
warn "not an integer" unless /^[+']?\d+$/;
warn "not a decimal number" unless
             /^'?\d+\.?\d*$/; # rejects .2
warn "not a decimal number" unless
             /^'?(?:\d+(?:\.\d*)?|\.\d+)$/;
warn "not a C float" unless
            /^([+']?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;

Courtesy of The Perl Cookbook.

TPJ One-Liner #26

Launching xterms with random colors. You might have to replace xterm with whatever command you use to launch a terminal window:

perl -e '$fg=rand 2**24; do { $bg = rand
2**24 } while (unpack("%32b*", pack "N",
($bg^$fg)&0xe0e0e0) < 8); ($fg, $bg) =
map { sprintf "#%06x", $_ } $fg, $bg;
exec("xterm", "-fg", $fg, "-bg", $bg);'
 

Courtesy of Tkil

TPJ One-Liner #27

Lop off the latter half of an array:

$#array /= 2;

Courtesy of The Perl Cookbook.


PREVIOUS  TABLE OF CONTENTS