FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Web Development
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
August 12, 2001
TPJ One Liners

(Page 1 of 6)
Short bits of code which will amaze, awaken, and amuse.
TPJ One-Liners - The Perl Journal


#1

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


#2

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;
DynaLoader::dl_install_xsub("main::hangme",
         unpack("I", pack("P4", "\xF0\x0F\xC7\xC8")));
hangme();
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, abigail@fnx.com
appeared in Issue 8


#7    An Absurd Way To Convert From Decimal To Binary

#!/usr/bin/perl

($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;
$r=$a->xlinvals(-1.5,0.5);$i=$a->ylinvals(-1,1);
$t=$r;$u=$i;for(1..30){$q=$r**2-$i**2+$t;$h=2*$r*$i+
$u;$d=$r**2+$i**2;$a=lclip($a,$_*($d>2.0)*($a==0));($r,
$i)=map{$_->clip(-5,5)}($q,$h);}$a->wpic("mandel.gif");

Courtesy of Tuomas J.Lukka
appeared in Issue 9




1 | 2 | 3 | 4 | 5 | 6 Next Page
RELATED ARTICLES
No Related Articles
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK