Channels ▼

Web Development

TPJ One Liners

TPJ One-Liners - The Perl Journal


perl -e  'print "Internet Time @",
  int (((time + 3600) % 86400)/86.4), "\n";'
Swatch's Internet Time, heralded as a revolutionary way
of measuring time independent of geography. See for details.

appeared in Issue 13

#32    A trick for indenting here strings

($definition = <<'FINIS') =~ s/^\s+//gm;
    The five varieties of camelids are the familliar
    camel, his friends the llama and the alpaca, and
    the rather less well-known guanaco and vicuna.

Courtesy of The Perl Cookbook
appeared in Issue 13


Efficiently finding the position of the first and last
occurrences of a substring in a string

$first = index($string, $substring);
$last = rindex($string, $substring);

appeared in Issue 13


Some scalars that Perl defines for you:

$^O contains the name of your operating system.
$^T contains the time at which your program began.
$O contains the name of your program

appeared in Issue 13

#35    If Dr. Seuss were a Perl programmer

# Will give errors if run with -w, so don't use -w :)
# Tested on NT with AS (5.005), GS (5.004_02), 
# and Solaris 2.6 (5.004_04)

if ("a packet hits a pocket") {
    On: a;
    socket(ON, A ,PORT,"")
           && the bus is interrupted as a very-last-resort
           && the address of the memory makes your 
              floppy disk, abort;
} else {

    "The socket packet pocket has an";
    error: to-report;

if ("your cursor finds a menu item") { 
    "followed by a dash"
            && "the double clicking icon";
    puts: your-items-in-the-trash
            && your data is corrupted cause the 
               index("doesn't", "hash");

} else {
   "Your situation is hopeless"
            && Your system's gonna crash;

if ("the label on the cable") {
    On-the-table, at-your-house;
    sub network {"is connected to the button on your mouse"};
    BUT: Your-packets, want-to; 
    {/tunnel to another protocol/};
    that's: repeatedly-rejected; 
    {/by the printer/}; "down the hall"
            && "YOUR SCREEN is all distorted";
    {/by the side effects of Gauss/};
    so: "your icons", in-the-window;
    "are as wavy as a souse";

} else {
    YOU: "may as well reboot" && "go out with a !";
    CAUSE: /Sure as Im a poet/;
    THIS: suckers-gonna-hang;

print "Seuss as a tech writer - Kevin Meltzer\n"; 

Courtesy of Kevin Meltzer
appeared in Issue 14


Neal Stephenson's latest novel, Cryptonomicon, includes a Perl cryptosystem code-named Pontifex. You can read about it at

appeared in Issue 14


Stripping the eighth bits from a string $s &= "\177" x length($s); Given a string in $s, this one-liner turns all of the "funny" characters (like or ) into regular seven-bit ASCII characters. It works by ANDing the bit representation of each character with 127, which removes the eighth bit. That turns into L and into N, for instance.

Courtesy of Tom Christiansen
appeared in Issue 14


Replacing tabs with spaces

perl -0011 -pi -e '/\011/&apm;&($_="$'")' filename

Courtesy of Abigail
appeared in Issue 14


Printing all capitalized words

perl -ne 'push@w,/(\b[A-Z]\S*?\b)/g;END{print"@w"}' file

appeared in Issue 14

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.