Don't ask to ask, just ask | |
PerlMonks |
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:
use strict; use warnings; use diagnostics;
warn "$var\n"; print "@things\n"; # array with spaces between elements
chomp $var; print ">>>$var<<<\n";
my $copy = $str; $copy =~ s/([^\x20-\x7E])/sprintf '\x{%02x}', ord $1/eg; print ":$copy:\n";
use Data::Dumper; print Dumper(\%hash); print Dumper($ref);
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 my $fh, '<', 'foo.txt' or die "can not open foo.txt: $!";
if (system $cmd) { print "Error: $? for command $cmd" } else { print "Command $cmd is OK" } $out = `$cmd`; print $? if $?;
eval { do_something() }; warn $@ if $@;
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();
use WWW::Mechanize; if ($mech->success()) { ... }
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 (Curate) 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 |