Are you new to Perl? Is your program misbehaving? Not sure where or how to begin debugging? Well, here is a concise
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:
- Add the "stricture" pragmas (Use strict and warnings)
use strict;
use warnings;
use diagnostics;
- Display the contents of variables using print or warn
warn "$var\n";
print "@things\n"; # array with spaces between elements
- Check for unexpected whitespace
- a) chomp, then print with delimiters of your choice, such as colons or balanced brackets, for visibility
chomp $var;
print ">>>$var<<<\n";
-
b) Check for unprintable characters by converting them into their ASCII hex codes using ord
my $copy = $str;
$copy =~ s/([^\x20-\x7E])/sprintf '\x{%02x}', ord $1/eg;
print ":$copy:\n";
-
Dump arrays, hashes and arbitrarily complex data structures. You can get started using the core module Data::Dumper. Should the output prove to be unsuitable to you, other alternatives can be downloaded from CPAN, such as Data::Dump, YAML, or JSON. See also How can I visualize my complex data structure?
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 -p 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 $@;
-
Use Carp to display variables with a stack trace of module names and function calls.
use Carp qw(cluck);
cluck("var is ($var)");
Better yet, install and use the Carp::Always
CPAN module to make your existing warn/die complain with a stack trace:
$ perl -MCarp::Always program.pl
-
Demystify regular expressions by installing and using the CPAN module YAPE::Regex::Explain
# what the heck does /^\s+$/ mean?
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new('/^\s+$/')->explain();
-
Neaten up your code by installing and using the CPAN script perltidy. Poor indentation can often obscure problems.
-
Checklist for debugging when using CPAN modules:
What's next? If you are not already doing so, use an editor that understands Perl syntax (such as vim or emacs), a GUI debugger (such as
Devel::ptkdb) or use a full-blown IDE. Lastly, use a version control system so that you can fearlessly make these temporary hacks to your code without trashing the real thing.
For more relevant discussions, refer to the initial Meditation post: RFC: Basic debugging checklist
Updated: Sep 8, 2009:
Added CPAN Diff/Grep tip.
Updated: Jan 11, 2011:
Added Carp::Always.