March 2012 Archives

Easy Data::Printer in the perl debugger

I find myself often wanting the magic of Data::Printer when I’m debugging. Sometimes ‘x’ just doesn’t cut the mustard.

I’ve finally got bored of typing:

use Data::Printer alias => dp;

in the debugger and have taken some time to make it available automatically.

If you want to try it add the following to $HOME/.perldb:

$DB::alias{dp} = 's/dp/DB::dp/';
sub DB::dp {
    eval {
        require Data::Printer;
        Data::Printer->import(colored=>1,use_prototypes=>0);
    };
    if ($@=~/Can't locate/) {
        print 'Data::Printer is not installed';
        return;
    };
    print Data::Printer::p(@_);
}

You can test it works with the following:

perl -d -e '$DB::single=1; $a'
main::(-e:1):   $DB::single=1; $a
auto(-1)  DB<1> v
1==>    $DB::single=1; $a
  DB<1> dp { foo => bar }
{
    foo   "bar"
}
  DB<2>

(Yes, you can use ‘-e 1’, but I have NonStop=1 in my parse_options() setting)

About Chisel

user-pic