Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: RFC: Basic debugging checklist (updated)

by toolic (Bishop)
on Feb 19, 2009 at 18:19 UTC ( #745150=note: print w/replies, xml ) Need Help??

in reply to RFC: Basic debugging checklist

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?
  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
  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)";
  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 perltidy (see also the CPAN module Perl::Tidy). 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.
    • 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://745150]
[ambrus]: So that, or if you really want something agnostic to the loop, then use curl multi. Its interface is quite reasonable (unless you use an old version),
[ambrus]: though it's very C-like (serious use of varargs so easy to pass the wrong type), and there are some minor problems (busy loop because it doesn't use the same timer as your event loop, so you have to artificially delay timer callbacks by a small amount).

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2016-12-07 16:23 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (130 votes). Check out past polls.