Channels ▼

Web Development

TPJ One Liners

TPJ One-Liners - The Perl Journal


Adding a long list of numbers on the command line:
perl -e 'print eval join("+", @ARGV)' 6 10 20 11
9 16 17 28 100 33333 14 -7

appeared in Issue 7


A cheap alarm clock:
perl -e 'sleep(120); while (1) { print "\a" }'

appeared in Issue 7

#3    Using Perl from Emacs

To apply a Perl expression EXPR to a region:
C-u M-| perl -pe 'EXPR'
To apply EXPR to the entire buffer:
C-x h C-u M-| perl -pe 'EXPR'

Courtesy of Mark-Jason Dominus.
appeared in Issue 8

#4    Preserving case in a substitution

To replace substring $x with an equal length substring $y, but preserving the case of $x:Z

$string =~ s/($x)/"\L$y"^"\L$1"^$1/ie;

Courtesy of Dean Inada
appeared in Issue 8

#5    Exploiting the F00F Pentium bug

require DynaLoader;
         unpack("I", pack("P4", "\xF0\x0F\xC7\xC8")));
Do NOT execute this. It will crash your computer.

Courtesy of Gisle Aas
appeared in Issue 8

#6    Primality

perl -le 'print "PRIME" if (1 x shift) !~ /^(11+)\1+$/' 19
Type this from your command line to test whether 19 (or any other integer of your choosing) is prime.

Courtesy of Abigail,
appeared in Issue 8

#7    An Absurd Way To Convert From Decimal To Binary


($decimal, $binary) = (shift, '');
$SIG(USR1) = sub { $binary .= "0"};
$SIG(USR2) = sub { $binary .= "1"};

do { kill $decimal & 1 ? 'USR2' : 'USR1' , $$;
     $decimal >>= 1;
} while ($decimal);

print scalar reverse $binary;

Courtesy of Nathan Torkington
appeared in Issue 9

#8    How To Patch Your Netscape Binary To Enable Strong Encryption

#!/usr/bin/perl -0777pi
s/TS:.*?\0/$_=$&;y,a-z, ,;s,    $,true,gm;s, 512,2048,;$_/es

Courtesy of Ian Goldberg
appeared in Issue 9

#9    How To Use The Perl Debugger as a Command-Line Interpreter

perl -de 0

appeared in Issue 9

#10    Using PDL to Generate Fractals

Using PDL to Generate Fractals
use PDL; use PDL::IO::Pic;$a=zeroes 300,300;

Courtesy of Tuomas J.Lukka
appeared in Issue 9

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.