perltutorial
toolic
Are you new to Perl? Is your program misbehaving? Not sure where or how to begin debugging? Well, here is a concise <b>checklist</b> of tips and techniques to get you started.
<p>This list is meant for debugging some of the most common Perl programming problems; it assumes no prior working experience with the Perl debugger ([doc://perldebtut]). Think of it as a First Aid kit, rather than a fully-staffed state-of-the-art operating room.
<p>These tips are meant to act as a guide to help you answer the following questions:
<ul>
<li>Are you sure your data is what you think it is?
<li>Are you sure your <i>code</i> is what you think it is?
<li>Are you inadvertently ignoring error and warning messages?
</ul>
<readmore>
<ol>
<li>Add the "stricture" pragmas ([id://111088])</li>
<c>
use strict;
use warnings;
use diagnostics;
</c>
<li>Display the contents of variables using [doc://print] or [doc://warn]</li>
<c>
warn "$var\n";
print "@things\n"; # array with spaces between elements
</c>
<li>Check for unexpected whitespace</li>
<ul>
<li>a) [doc://chomp|<c>chomp</c>], then [doc://print|<c>print</c>] with delimiters of your choice, such as colons or balanced brackets, for visibility
<c>
chomp $var;
print ">>>$var<<<\n";
</c>
</li>
<li>
b) Check for unprintable characters by converting them into their ASCII hex codes using [doc://ord|<c>ord</c>]
<c>
my $copy = $str;
$copy =~ s/([^\x20-\x7E])/sprintf '\x{%02x}', ord $1/eg;
print ":$copy:\n";
</c>
</li>
</ul>
<li>
[doc://Data::Dumper|Dump] arrays, hashes and arbitrarily complex data structures. You can get started using the core module [doc://Data::Dumper]. Should the output prove to be unsuitable to you, other alternatives can be downloaded from CPAN, such as [mod://Data::Dump], [mod://YAML], or [mod://JSON]. See also [id://481745]
</li>
<c>
use Data::Dumper;
print Dumper(\%hash);
print Dumper($ref);
</c>
<li>
If you were expecting a [doc://ref|<c>ref</c>]erence, make sure it is the right kind (ARRAY, HASH, etc.)
</li>
<c>
print ref $ref, "\n";
</c>
<li>
<p>Check to see if your code is what you thought it was: [doc://B::Deparse|<c>B::Deparse</c>]
</li>
<c>
$ perl -MO=Deparse -p program.pl
</c>
<li>
<p>
Check the return ([doc://perlvar#Error-Variables|error]) status of your commands
</li>
<ul>
<li>
[doc://open|<c>open</c>] with [doc://perlvar#$!|<c>$!</c>]
<c>
open my $fh, '<', 'foo.txt' or die "can not open foo.txt: $!";
</c>
</li>
<li>
[doc://system|<c>system</c>] and backticks ([doc://qx|<c>qx</c>]) with [doc://perlvar#$?|<c>$?</c>]
<c>
if (system $cmd) {
print "Error: $? for command $cmd"
}
else {
print "Command $cmd is OK"
}
$out = `$cmd`; print $? if $?;
</c>
</li>
<li>
[doc://eval|<c>eval</c>] with [doc://perlvar#$@|<c>$@</c>]
<c>
eval { do_something() }; warn $@ if $@;
</c>
</li>
</ul>
<li>
Use [doc://Carp] to display variables with a stack trace of module names and function calls.
</li>
<c>
use Carp qw(cluck);
cluck("var is ($var)");
</c>
<p>Better yet, install and use the [mod://Carp::Always|<c>Carp::Always</c>]
CPAN module to make your existing [doc://warn|<c>warn</c>]/[doc://die|<c>die</c>] complain with a stack trace:
<c>
$ perl -MCarp::Always program.pl
</c>
<li>
Demystify regular expressions by installing and using the CPAN module [mod://YAPE::Regex::Explain|<c>YAPE::Regex::Explain</c>]
</li>
<c>
# what the heck does /^\s+$/ mean?
use YAPE::Regex::Explain;
print YAPE::Regex::Explain->new('/^\s+$/')->explain();
</c>
<li>
Neaten up your code by installing and using the CPAN script [mod://perltidy]. Poor indentation can often obscure problems.
</li>
<p>
<li>
Checklist for debugging when using CPAN modules:
</li>
<ul>
<li>
Check the Bug List by following the module's "View Bugs" link.
</li>
<li>
Is your installed version the latest version? If not, check the change log by following the "Changes" link. Also follow the "Other Tools" link to "Diff" and "Grep" the release.
</li>
<li>
If a module provides status methods, check them in your code as you would check return status of built-in functions:
<c>
use WWW::Mechanize;
if ($mech->success()) { ... }
</c>
</ul>
</ol>
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 [mod://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.
<p>For more relevant discussions, refer to the initial Meditation post: [id://744845]
<p><small>
<b>Updated:</b> Sep 8, 2009:
Added CPAN Diff/Grep tip.
<br>
<b>Updated:</b> Jan 11, 2011:
Added Carp::Always.
</small>
</readmore>