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.)

[This entry was originally posted on my perl blog]

Print Friendly
This entry was posted in Perl by Chisel. Bookmark the permalink.

About Chisel

I've been interested in technology since my childhood (my Acorn Electron era). Commercially I've worked with C and VBA (Access) [but we don't talk about that]. In 2000 I secured my first role as a Perl Programmer and that's been my primary language since then. I dabble in bash, puppet, and a few other skills that help me dip my toe in the DevOps water, or provide a conduit beween the dev and ops worlds. I joined Net-A-Porter in November 2006 and have been happily evolving with the business since then.

Leave a Reply