Update: The updated version of this Meditation is at Re: RFC: Basic debugging checklist (updated) and the Tutorial version is now at Basic debugging checklist. Here is the untouched original:
Before I post this as a Tutorial, please help me to improve this by offering constructive feedback.
Are you new to Perl? Is your program misbehaving? Not sure where or how to begin debugging? Well, here is a concise checklist of tips and techniques to get you started.
This list is meant for debugging some of the most common Perl programming problems; it assumes no prior working experience with the Perl debugger (perldebtut). Think of it as a First Aid kit, rather than a fully-staffed state-of-the-art operating room.
These tips are meant to act as a guide to help you answer the following questions:
- Are you sure your data is what you think it is?
- Are you sure your code is what you think it is?
- Are you inadvertently ignoring error and warning messages?
- Add the "stricture" pragmas (Use strict and warnings)
use strict;
use warnings;
use diagnostics;
- Print the contents of variables
print "$var\n";
print "@things\n"; # array with spaces between elements
- Check for unexpected whitespace
- chomp, then print with colon delimiters for visibility
chomp $var;
print ":$var:\n";
-
Check for unprintable characters and identify them by their ASCII codes using ord
print ":$_:", ord($_), "\n" for (split //, $str)
-
Dump arrays, hashes and arbitrarily complex data structures
use Data::Dumper;
print Dumper(\%hash);
print Dumper($ref);
-
If you were expecting a reference, make sure it is the right kind (ARRAY, HASH, etc.)
print ref $ref, "\n";
-
Check to see if your code is what you thought it was: B::Deparse
$ perl -MO=Deparse program.pl
-
Check the return (error) status of your commands
-
open with $!
open my $fh, '<', 'foo.txt' or die "can not open foo.txt: $!";
-
system and backticks (qx) with $?
if (system $cmd) {
print "Error: $? for command $cmd"
}
else {
print "Command $cmd is OK"
}
$out = `$cmd`; print $? if $?;
-
eval with $@
eval { do_something() }; warn $@ if $@;
-
Demystify regular expressions using the CPAN module YAPE::Regex::Explain
# what the heck does /^\s+$/ mean?
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new('/^\s+$/')->explain();
-
Checklist for debugging when using CPAN modules:
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.