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)

3 Comments

Wow, I'm really glad you enjoy Data::Printer - and even more that you took the time to write and share this nice tip!

Speaking of tips and the debugger, have you seen the ones in DDP's documentation?

https://metacpan.org/module/Data::Printer#Using-Data::Printer-from-the-Perl-debugger

One uses DB::Pluggable and the other is a very similar approach to what you described here, except it assumes you have Data::Printer installed and doesn't expose np(). In fact, np() is private and shouldn't be used at all - you should stick with p() and use_prototypes => 0.

Anyway, thanks for the post and the nice words about Data::Printer. Please let me know if you find bugs or have any feature requests!

Note this may be a duplicate: I posted something like this several hours ago and I don't see it showing up.

Also regarding debuggers and Data::Printer.

A while back (git logs show Jan 15, 2012) I added Data::Printer support to a gdb-like debugger I've been writing Devel::Trepan.

By default, if the Data::Printer module is installed, that is used by default to display results. If Data::Printer is not installed, but Data::Dumper::Perltidy is available, that is the next preferred method. And finally, failing to find either of those Data::Dumper is used. The debugger command "show evaldisplay" show you what setting is in effect.

Since people may want to change this default behavior, the debugger command "set evaldisplay" can be used to change the result display method. "help set evaldisplay" gives the syntax for that command and explains more or less what I wrote above.

Finally, since I realize "set evaldisplay" may be long to type. If you are in a in interactive terminal and a ReadLine module is available (e.g. Term::ReadLine::Perl), then you can use tab completion and type "set eval<TAB> and to see the options after that enter another tab.


Leave a comment

About Chisel

user-pic