What a terrific response! I'm glad I have enough votes today: ++ to all.
Every suggestion and opinion is valuable. When I post this as a Tutorial, I will link back to this Meditation so that all these valid discussions are available. Since I think this checklist will be most effective if I keep it as terse as possible, I have not captured all the reasons for using certain techniques in the updated meditation below. Nor have I included some of the more advanced methods mentioned.
I was most surprised at the amount of discussions surrounding Data::Dumper. I use it all the time because it always Does What I Want, probably because my programs are much simpler than those of more advanced coders. One advantage of Data::Dumper over its CPAN counterparts is that it is a core module, and therefore, no installation is required. Since this checklist will be geared for beginners, I will mention Data::Dumper and acknowledge the more advanced alternatives. And I will stop being so lazy and try some of them myself to see what I'm missing!
Rather than cluttering the Monastery with individual replies to all your replies, I will toss bouquets of "Thank You"'s to all who spent their time to remind me of and teach me new techniques:
my @righteous_monks= qw(
GrandFather tye YourMother kyle Tanktalus
JavaFan jplindstrom rcaputo roho
bart ELISHEVA tilly gwadej # from the CB
);
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;
- Display the contents of variables using print or warn
warn "$var\n";
print "@things\n"; # array with spaces between elements
- Check for unexpected whitespace
- chomp, then print with delimiters of your choice, such as colons or balanced brackets, for visibility
chomp $var;
print ">>>$var<<<\n";
-
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)";
-
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 perltidy (see also the CPAN module Perl::Tidy). 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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.