Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Using Data::Printer from the Perl debugger

by ibm1620 (Hermit)
on Feb 25, 2023 at 16:06 UTC ( [id://11150599]=perlquestion: print w/replies, xml ) Need Help??

ibm1620 has asked for the wisdom of the Perl Monks concerning the following question:

I set up to use Data::Printer from the debugger using these instructions. It successfully rebound the debugger's p() command, except that it prints the data twice:

File foo.pl

#!/usr/bin/env perl use v5.36; say "Here is ~/.perldb:\n"; say qx(cat ~/.perldb); my @xx = (qw/a b c d e/); push @xx, [ 1,2,3 ]; my $foo = 1; # set breakpoint here and issue p @xx say 'Show result of p @xx when issued from within program'; use Data::Printer; p @xx;
Debug session:
$ perl -d ./foo.pl Loading DB routines from perl5db.pl version 1.73 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(./foo.pl:4): say "Here is ~/.perldb:\n"; DB<1> b 10 DB<2> c Here is ~/.perldb: use DB::Pluggable; DB::Pluggable->run_with_config( \'[DataPrinter]' ); main::(./foo.pl:10): my $foo = 1; # use as breakpoint DB<2> p @xx [ [0] "a", [1] "b", [2] "c", [3] "d", [4] "e", [5] [ [0] 1, [1] 2, [2] 3 ] ] [ [0] "a", [1] "b", [2] "c", [3] "d", [4] "e", [5] [ [0] 1, [1] 2, [2] 3 ] ] DB<3> c Show result of p @xx when issued from within program [ [0] "a", [1] "b", [2] "c", [3] "d", [4] "e", [5] [ [0] 1, [1] 2, [2] 3 ] ] Debugged program terminated. Use q to quit or R to restart,
(I get the same result when I remove the last 3 lines from foo.pl).

Any idea how to fix this?

Replies are listed 'Best First'.
Re: Using Data::Printer from the Perl debugger
by LanX (Saint) on Feb 25, 2023 at 17:55 UTC
    Well I'd say you have to file a big report with those two modules involved.

    DB::Pluggable and Data::Printer

    I have a hunch that your problem here is that the debugger prints the result of your command while you trigger an internal print too.

    If I were you I'd go with the approach to define an alias px

    FWIW I successfully patched perl5db in the past to use Data::Dump you can find the discussions in the archives here.

    HTH :)

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

      Thanks! Interestingly, the documentation specifies putting this into .perldb:
      $DB::alias{px} = 's/px/DB::px/'; sub px { my $expr = shift; require Data::Printer; print Data::Printer::p($expr); }
      Which causes $expr to be printed twice, once colorized, and once uncolorized, as one might expect. Changing that last line to
      Data::Printer::p($expr);
      fixes it.
        IMHO someone should file a bug report with both modules.

        I think one of the authors had the behavior of Data::Dump::pp() in mind which doesn't print by itself when called in a non void context.

        And the other one just copied the doc without testing.

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

      Using pure guesswork, I made one change to DB::Pluggable::Plugin::DataPrinter.pm:
      sub initialize { no warnings 'once'; # $DB::alias{p} = 's/^/use Data::Printer; /; eval $cmd'; $DB::alias{p} = 's/^/use Data::Printer; /'; }
      Now the p command prints its argument only once.

      I inserted print $cmd; before eval $cmd, entered p $my_var in the debugger, and it printed "p $my_var". Apparently the eval was unnecessary.

      FWIW.

        You are mangling internal p command and aliases, here are dragons.

        If I were you I'd stick with px

        Otherwise you need to look into the code of perl5db.pl

        Cheers Rolf
        (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
        Wikisyntax for the Monastery

Re: Using Data::Printer from the Perl debugger
by kcott (Archbishop) on Feb 25, 2023 at 18:21 UTC

    G'day ibm1620,

    I can replicate your results.

    I made a verbatim copy of foo.pl (including shebang and v5.36). I have the latest versions of DB::Pluggable (1.112001) & Data::Printer (1.001000). I get identical output to what you show for 'perl -d ./foo.pl'.

    I also tried with .perldb code formatted as shown in DB::Pluggable - SYNOPSIS. The result was the same ("prints twice") except, of course, for:

    DB<2> c Here is ~/.perldb: use DB::Pluggable; DB::Pluggable->run_with_config(\<<EOINI); [DataPrinter] EOINI main::(./foo.pl:10): my $foo = 1; # set breakpoint here and issue p + @xx
    "I get the same result when I remove the last 3 lines from foo.pl"

    My result starts the same but ends with:

    DB<3> c Debugged program terminated. Use q to quit or R to restart,

    I don't see this as being related to "prints twice"; however, it might be worth checking that the code you posted is the same as what you're running. After removing 3 lines, my last statement is 'my $foo = 1;'.

    — Ken

Re: Using Data::Printer from the Perl debugger [troubleshooting suggestion - IGNORE]
by kcott (Archbishop) on Feb 25, 2023 at 19:02 UTC

    Oops! Sorry! Egg on face :-(

    foo.pl contains use Data::Printer. The output format, in the examples below, has nothing to do with .perldb's contents or existence. I've stricken it all and, because it was quite long, put it in a spoiler.

    — Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11150599]
Approved by kcott
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 03:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found