Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl-Sensitive Sunglasses
 
PerlMonks  

Basic debugging checklist

by toolic (Chancellor)
on Feb 23, 2009 at 00:06 UTC ( #745674=perltutorial: print w/ replies, xml ) Need Help??

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?
  1. Add the "stricture" pragmas (Use strict and warnings)
  2. use strict; use warnings; use diagnostics;
  3. Display the contents of variables using print or warn
  4. warn "$var\n"; print "@things\n"; # array with spaces between elements
  5. 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";
  6. 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?
  7. use Data::Dumper; print Dumper(\%hash); print Dumper($ref);
  8. If you were expecting a reference, make sure it is the right kind (ARRAY, HASH, etc.)
  9. print ref $ref, "\n";
  10. Check to see if your code is what you thought it was: B::Deparse

  11. $ perl -MO=Deparse -p program.pl
  12. 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 $@;
  13. Use Carp to display variables with a stack trace of module names and function calls.
  14. 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
  15. Demystify regular expressions by installing and using the CPAN module YAPE::Regex::Explain
  16. # what the heck does /^\s+$/ mean? use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('/^\s+$/')->explain();
  17. Neaten up your code by installing and using the CPAN script perltidy. Poor indentation can often obscure problems.
  18. 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()) { ... }
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.

Comment on Basic debugging checklist
Select or Download Code
Re: Basic debugging checklist
by Bloodnok (Vicar) on Feb 23, 2009 at 01:19 UTC
    Damned decent posting :D ... just a coupla suggestions tho'...
    • Step 5 - Use a stringified ref. to provide straightforward visual comparison of 2, or more, ref.s - I've recently been using this to verify that a ref. in 2 different places is actually the same object.
    • Step 7 - add use autodie; to provide default exception throwing on failure
    • Step 7 & 8 - add use CGI::Carp; for CGI/WWW scripts
    • Your final observation WRT IDEs etc. could, IMHO, suggest that the use of Eclipse, for perl dev't, isn't for the fainthearted...
    Just a thought...

    A user level that continues to overstate my experience :-))
Re: Basic debugging checklist
by umasuresh (Hermit) on Jan 28, 2010 at 16:33 UTC
    Thanks a lot toolic, very useful post!
Re: Basic debugging checklist
by Anonymous Monk on Mar 14, 2013 at 00:17 UTC

    Not sure if your perl version is too old?

    Use Perl::MinimumVersion/perlver

    $ perlver foo.pl -------------------------------------- | file | explicit | syntax | external | | -------------------------------------- | | foo.pl | ~ | v5.10.0 | n/a | | -------------------------------------- | | Minimum explicit version : ~ | | Minimum syntax version : v5.10.0 | | Minimum version of perl : v5.10.0 | -------------------------------------- $ perlver --blame foo.pl ------------------------------------------------------------ File : foo.pl Line : 3 Char : 17 Rule : _perl_5010_operators Version : 5.010 ------------------------------------------------------------ ~~ ------------------------------------------------------------ $ cat foo.pl my $foo = 6; my @bar = ( 3,6, 9 ); print 1 if $foo ~~ @bar;

    Then you can add use v5.10.0; or <c> use 5.01000; to your foo.pl, and perl will do the version check

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perltutorial [id://745674]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2014-04-16 20:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (434 votes), past polls