Some men build model trains. Some men fish. I collect Perl one-liners, error messages, and hard-earned debugging laws.
# 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}'
| Error | Translation |
|---|---|
| Can't use string ("0") as a HASH ref while "strict refs" in use | You thought it was a hashref. It was not. It was never a hashref. |
| Useless use of a constant ("42") in void context | Perl saw you write 42; on a line by itself and is judging your life choices. |
| Not a CODE reference at script.pl line 47 | You tried to call it like a function. It's a string. It was always a string. |
| Use of uninitialized value $thing in concatenation | The ghost of variables past haunts your concatenation. |
| Can't locate Acme/Magic.pm in @INC | CPAN has over 5,000 modules. This isn't one of them. Or you forgot use lib. |
| Modification of a read-only value attempted | You 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 use | Perl doesn't know what foo is. Honestly, neither do you at this point. |
| 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. |
+--------------+
| 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() |
+--------------+