Sunday, May 31, 2009

CPAN Picks: Variable Dump

This is a series in which I will present CPAN modules I often use or deem to be noteworthy otherwise.
While the first post of the series, Quick'n'Dirty SQL, was about SQL access, this post deals with a debugging module: Data::Dump
I'm not a big fan of the Perl debugger and although it has its uses, I prefer to debug by logging debug information from my applications. Now, debug information comes mostly in two flavors: trace info, which indicates that execution has reached a stage (usually the point before or after a certain interesting source code line) and dump info, which allows the inspection of runtime values held by application variables.
The most venerable and ancient (i.e. primitive) dump info provider is the Data::Dumper module (well, apart from Perl Cookbook recipes). The main drawback is that its output usually starts with $VAR1 which is meant to denote the variable being dumped. You can supply a name to it, but it's cumbersome and non-intuitive.
Data::Dump on the other hand, dispenses with variable names altogether. It just prints the value supplied to it, no matter what the variable was called. It also allows you to import a handy short-named dump function (pp) instead of the name-clashing dump.
So what makes Data::Dump stand out from other dumping modules?
The first thing I've mentioned already, it chooses to ignore variable names. While this might seem a drawback at first, I usually have enough trace info between the variable dumps to allow me to distinguish between dumped variables. In the rare cases when that's not enough, I add some debug info describing the variable.
The second feature is that it's context sensitive. Called in void context (i.e. as a command on its own) it will output the dump to STDERR:
use Data::Dump qw/pp/;
pp $my_complex_data; # this gets printed to STDERR
But in any other context (i.e. when it's return value is used) it will just return the dump as a string and skip the output:
use Data::Dump qw/pp/;
$c->log->debug("Stash: " . pp $c->stash );
The final advantage (mostly over Data::Dumper) is that is just seems to work whatever I throw at it, no need to wrap stuff in anonymous array or hash refs. It doesn't dump subroutine refs, but don't need to inspect them anyway (the code I have is not that tricky).
So that's Data-Dump for me, but there are more output functions and tracing wrappers for you to use (if you need them, of course)


  1. Thanks for this. I have always used Data::Dumper, and had recently read that Data::Dump was better, but I didn't have time to investigate why.

  2. It also exports a 'dump' function which returns the string representation. IMHO it would be good to replace the builtin CORE::dump() (which is hardly used these days) with that in some future perl5 version.

  3. Hi, I have a couple jobs that I would like to advertise on your site or via an email list to inform your readers about Perl programming jobs. Please get back to me as soon as you get a chance.

    Look forward to hearing from you.