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
$VAR1which 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
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 STDERRBut 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)