# TPJ One Liners

TPJ One-Liners - The Perl Journal

#11    The Game Of Life

```use PDL; use PDL::Image2D;
use PDL::Graphics::TriD;nokeeptwiddling3d;
\$d=byte(random(zeroes(40,40))>0.85);
\$k=byte [[1,1,1],[1,0,1],[1,1,1]];
do{ imagrgb [\$d]; \$s=conv2d(\$d,\$k);
\$d&=(\$s>4);\$d&=(\$s>1);\$d|=(\$s==3);}
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 ) { ... }`

#13

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

#14

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

Courtesy of Tuomas J. Lukka
appeared in Issue 11

#15    Primality

```use PDL; use PDL::Graphics::TriD; \$s=40;\$a=zeroes
2*\$s,\$s/2;\$t=\$a->xlinvals(0,6.284);\$u=\$a->ylinvals
(0,6.284);\$o=5;\$i=1;\$v=\$o-\$o/2*sin(3*\$t)+\$i*sin\$u;
imag3d([\$v*sin\$t,\$v*cos\$t,\$i*cos(\$u)+\$o*sin(3*\$t)]);
```

Courtesy of Tuomas J. Lukka
appeared in Issue 11

#16

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 http://tpj.com/one-liners.
use GD;\$f='#ffffff';\$T=table;sub p{print @_}
p"<body bgcolor=\$f>";for(@ARGV){open*G,\$_ or(warn("\$_:
\$!")&&next);\$g=GD::Image->newFromGif(G)||(warn\$_,
": GD error"and next);@c=map{\$_!=\$g->transparent
?sprintf'#'.('%.2x'x3),\$g->rgb(\$_):\$f}0..
cellspacing=0>";(\$x,\$y)=\$g->getBounds;for\$j(0..\$y)
{p"<tr>";for(\$i=0;\$i<\$x;\$i++){\$s=1;\$s++&&\$i++while(\$i+1
<\$x&&\$g->getPixel(\$i+1,\$j)==\$g->getPixel(\$i,\$j));p"
<td bgcolor=",\$c[\$g->getPixel(\$i,\$j)],"
colspan=\$s> "}}p"</\$T>"}

```

Courtesy of Mike Fletcher
appeared in Issue 11

#17

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

#18

"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

#19

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

### More Insights

 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.

# First C Compiler Now on Github

The earliest known C compiler by the legendary Dennis Ritchie has been published on the repository.

# HTML5 Mobile Development: Seven Good Ideas (and Three Bad Ones)

HTML5 Mobile Development: Seven Good Ideas (and Three Bad Ones)

# Building Bare Metal ARM Systems with GNU

All you need to know to get up and running... and programming on ARM

# Amazon's Vogels Challenges IT: Rethink App Dev

Amazon Web Services CTO says promised land of cloud computing requires a new generation of applications that follow different principles.

# How to Select a PaaS Partner

Eventually, the vast majority of Web applications will run on a platform-as-a-service, or PaaS, vendor's infrastructure. To help sort out the options, we sent out a matrix with more than 70 decision points to a variety of PaaS providers.

More "Best of the Web" >>

Dr. Dobb's TV