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)
- Display the contents of variables using print or warn
- 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?
- If you were expecting a reference, make sure it is the right kind (ARRAY, HASH, etc.)
-
Check to see if your code is what you thought it was: B::Deparse
-
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.
- Demystify regular expressions by installing and using the CPAN module YAPE::Regex::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:
- Check the Bug List by following the module's "View Bugs" link.
- 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.
-
If a module provides status methods, check them in your code as you would check return status of built-in functions:
use WWW::Mechanize; if ($mech->success()) { ... }
use strict; use warnings; use diagnostics;
warn "$var\n"; print "@things\n"; # array with spaces between elements
use Data::Dumper; print Dumper(\%hash); print Dumper($ref);
print ref $ref, "\n";
$ perl -MO=Deparse -p program.pl
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
# what the heck does /^\s+$/ mean? use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('/^\s+$/')->explain();
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Basic debugging checklist
by Bloodnok (Vicar) on Feb 23, 2009 at 01:19 UTC | |
Re: Basic debugging checklist
by hexcoder (Deacon) on Jun 21, 2014 at 11:07 UTC | |
by LanX (Saint) on Jun 21, 2014 at 12:30 UTC | |
Re: Basic debugging checklist
by LanX (Saint) on Dec 03, 2017 at 13:54 UTC | |
by Athanasius (Archbishop) on Dec 03, 2017 at 15:08 UTC | |
by davies (Prior) on Mar 01, 2020 at 22:14 UTC | |
by LanX (Saint) on Mar 01, 2020 at 23:57 UTC | |
by hippo (Bishop) on Mar 02, 2020 at 09:22 UTC | |
by soonix (Canon) on Dec 03, 2017 at 14:44 UTC | |
by QM (Parson) on Dec 05, 2017 at 12:02 UTC | |
Re: Basic debugging checklist
by choroba (Cardinal) on Feb 28, 2020 at 17:33 UTC | |
Re: Basic debugging checklist
by Anonymous Monk on Mar 14, 2013 at 00:17 UTC | |
Re: Basic debugging checklist
by Anonymous Monk on Oct 03, 2014 at 02:58 UTC | |
by Anonymous Monk on Oct 31, 2014 at 07:38 UTC | |
Re: Basic debugging checklist
by umasuresh (Hermit) on Jan 28, 2010 at 16:33 UTC |