Channels ▼

Web Development

TPJ One Liners

TPJ One-Liners - The Perl Journal

#11    The Game Of Life

The Game Of Life
use PDL; use PDL::Image2D;
use PDL::Graphics::TriD;nokeeptwiddling3d;
$k=byte [[1,1,1],[1,0,1],[1,1,1]];
do{ imagrgb [$d]; $s=conv2d($d,$k);
while (!twiddle3d);

Courtesy of Robin Williams
appeared in Issue 10

#12    DeMorgan's Rule

if (!$a || $b != $c) { ... }
is equivalent to
unless ( $a && $b == $c ) { ... }

appeared in Issue 10


Little-known facts about qr// (new with Perl 5.005): it has a magic print value, and it's an object of type Regexp.

% perl -le 'print "My regex: ", qr/^watch/i'
                               My regex: (?i-xsm:^watch this)
                            $rob = qr/red/i;
                            if ($rob->match("Fred Flintstone")) {
                                print "Got obj fred!\n";

                            sub Regexp::match {
                                my $self = shift;
                                my $arg = @_ ? shift : $_;
                                return $arg =~ /$arg/;

Courtesy of Tom Christiansen
appeared in Issue 11


Transpose a two-dimensional array:
@matrix_t = map{my$x=$_;[map {$matrix[$_][$x]}

Courtesy of Tuomas J. Lukka
appeared in Issue 11

#15    Primality

use PDL; use PDL::Graphics::TriD; $s=40;$a=zeroes

Courtesy of Tuomas J. Lukka
appeared in Issue 11


This code converts any GIF to an HTML table--each cell of the table corresponds to a pixel of the image. Use this to make your web advertisements seem like important content and circumvent Lincoln's Apache::AdBlocker. :

This code is online at
use GD;$f='#ffffff';$T=table;sub p{print @_}
p"<body bgcolor=$f>";for(@ARGV){open*G,$_ or(warn("$_:
": GD error"and next);@c=map{$_!=$g->transparent
$g->colorsTotal;p"<$T border=0 cellpadding=0
<td bgcolor=",$c[$g->getPixel($i,$j)],"
colspan=$s> "}}p"</$T>"}

Courtesy of Mike Fletcher
appeared in Issue 11


Ever wish backquotes didn't interpolate variables? qx() is a synonym for backquotes, but if you use single quotes as a delimiter, it won't interpolate: qx'echo $HOME' works.

Courtesy of Tom Christiansen
appeared in Issue 11


"Use m//g when you know what you want to keep, and split() when you know what you want to throw away."

Courtesy of Randal L. Schwartz
appeared in Issue 11


Count the lines of pod and code in a Perl program:

@a=(0,0);while(<>){++$a[not m/
^=\w+/s .. m/^=cut/s]} printf"%d
pod lines, %d code lines\n",@a;

Courtesy Sean M. Burke
appeared in Issue 11

#20    Results of the SunWorld reader survey (4,106 respondents)

Which of the following open source products do you have installed for WORK use?

Perl            83%
Sendmail        74%
Apache          72%
Linux           64%
Tcl             52%
Python          24%

Which of the following open source products do you have installed for PERSONAL use?

Perl            79%
Linux           77%
Apache          63%
Sendmail        61%
Tcl             55%
Python          34%

appeared in Issue 11

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.