PERL 5.004_04 INSIDE - OVER 5,000 MODULES ON CPAN

The Perl Shrine

Some men build model trains. Some men fish. I collect Perl one-liners, error messages, and hard-earned debugging laws.

My Favorite Perl One-Liners

# Reverse a string
perl -e 'print scalar reverse "Hello World\n"'

# Find duplicate lines in a file
perl -ne 'print if $seen{$_}++' file.txt

# In-place find and replace
perl -pi -e 's/foo/bar/g' *.txt

# Print lines matching a pattern (better grep)
perl -ne 'print if /^\d{3}-\d{4}/' contacts.txt

# One-liner web server (the original)
perl -MIO::Socket::INET -e '$s=IO::Socket::INET->new(LocalPort=>8080,Listen=>5,Reuse=>1);while($c=$s->accept){print $c "HTTP/1.0 200 OK\r\n\r\nIt works!\n";close $c}'

Perl Error Message Hall of Fame

ErrorTranslation
Can't use string ("0") as a HASH ref while "strict refs" in useYou thought it was a hashref. It was not. It was never a hashref.
Useless use of a constant ("42") in void contextPerl saw you write 42; on a line by itself and is judging your life choices.
Not a CODE reference at script.pl line 47You tried to call it like a function. It's a string. It was always a string.
Use of uninitialized value $thing in concatenationThe ghost of variables past haunts your concatenation.
Can't locate Acme/Magic.pm in @INCCPAN has over 5,000 modules. This isn't one of them. Or you forgot use lib.
Modification of a read-only value attemptedYou tried to change the unchangeable. This is a metaphor for debugging legacy code.
Deep recursion on subroutine "main::oops"Congratulations, you've invented infinite regress. Philosophers are jealous.
Bareword "foo" not allowed while "strict subs" in usePerl doesn't know what foo is. Honestly, neither do you at this point.

Lepercum's Laws of Perl Debugging

1.Any regex that works on the first try is wrong. You just don't know how yet.
2.The bug is always in the module you didn't write, but somehow it's still your fault.
3.If Data::Dumper can't explain it, nothing can. Except maybe alcohol.
4.There's more than one way to do it, and all of them are in production simultaneously.
5.A Perl script that runs without warnings is either trivial or lying.
6.perl -d is not just a debugger. It's a lifestyle.
7.The correct number of backslashes in a regex is always one more than you think.
8.Documentation is a letter to your future self. I have not written to myself in years.
9.If it runs on the first try, add use strict. Then watch it burn.
10.CGI.pm did nothing wrong. Debate this on the links page.

Debug or Die()

        +--------------+
        | Script fails |
        +------+-------+
               |
        +------v-------+
        |  Add print   |
        |  statements  |
        +------+-------+
               |
        +------v-------+     YES    +-------------+
        | Does it work +----------->| Remove half |
        |    now?      |            | the prints, |
        +------+-------+            | ship it     |
               | NO                 +-------------+
        +------v-------+
        | Try          |
        | Data::Dumper |
        +------+-------+
               |
        +------v-------+     YES    +-------------+
        | Understand   +----------->|    Liar.    |
        | the output?  |            +-------------+
        +------+-------+
               | NO
        +------v-------+
        |   perl -d    |
        +------+-------+
               |
        +------v-------+     YES    +-------------+
        | Fixed it?    +----------->| Mass email  |
        |              |            | the team    |
        +------+-------+            +-------------+
               | NO
        +------v-------+
        | Ask on       |
        | PerlMonks    |
        +------+-------+
               |
        +------v-------+     YES    +-------------+
        | Was it a     +----------->| It's always |
        | missing      |            | a missing   |
        | semicolon?   |            | semicolon.  |
        +------+-------+            +-------------+
               | NO
        +------v-------+
        |    die()     |
        +--------------+

Larry Wall Quotes

Top 10 CPAN Modules I Trust With My Life

  1. DBI - Talks to databases without making me write C every time. ★★★★★
  2. CGI.pm - The gateway drug that let half the web accept form submissions. ★★★★★
  3. LWP::UserAgent - Fetches web pages so my scripts can pretend to be productive. ★★★★★
  4. Data::Dumper - When the variables lie, this one tells the truth. ★★★★★
  5. Storable - Freezes Perl data structures for later regret and retrieval. ★★★★☆
  6. File::Find - Crawls a directory tree like a bloodhound with shell access. ★★★★☆
  7. Text::CSV - Because spreadsheets will always find a way into production. ★★★★☆
  8. Template::Toolkit - Keeps HTML mostly separate from Perl, which everyone appreciates. ★★★★☆
  9. Net::FTP - The reason deployment can happen from a motel room at 1 AM. ★★★☆☆
  10. Getopt::Long - Makes command-line scripts look like they were planned. ★★★★☆
You are visitor number
<- Prev | The Perl Webring | Next ->
MADE WITH
NOTEPAD
PERL
POWERED
CGI-BIN
PRIDE
BOBBY
APPROVED