Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Hacking Debugger bugs connected to $^D

by LanX (Canon)
on Apr 24, 2013 at 15:52 UTC ( #1030454=perlquestion: print w/ replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I hacked my perldebugger in a way to act as a REPL, i.e. instantly printing the returnvalue from every executed line.

from time to time I have weird output, like

DB<100> sub tst {} => 0

which is wrong, since sub returns nothing (i.e. empty list in list context)

$ perl -e 'use Data::Dumper; print Dumper eval "sub tst {}"' $

digging into the code of perl5db.pl helped me identifying the source of the problem:

The debugger automatically prepends code to the command-line before evaling it.

Essentially the directive '$^D=$^D;' causes the problem

$ perl -e 'use Data::Dumper; print Dumper eval "\$^D=\$^D;sub tst {}"' $VAR1 = '0';

I'm not sure why this is happening, IMHO it's a bug in the implementation of eval and/or debug mode.

The original line in perl5db.pl is

$evalarg = "\$^D = \$^D | \$DB::db_stop;\n$cmd";

my workaround now is using an explicit do { ...}

$evalarg = "\$^D = \$^D | \$DB::db_stop;\ndo {$cmd}";

:~$ perl -e 'use Data::Dumper; print Dumper eval "\$^D=\$^D;do {sub ts +t {}}"' :~$

This should hopefully not cause any new side effects... but is still a weird phenomenon.

Anyone here who can enlighten me, or give me a better workaround?

Cheers Rolf

( addicted to the Perl Programming Language)

) see also YAPC talk

Comment on Hacking Debugger bugs connected to $^D
Select or Download Code
Re: Hacking Debugger bugs connected to $^D
by Tommy (Chaplain) on Apr 24, 2013 at 18:53 UTC

    Okay Rolf. pdb repl?! You have just officially attained a new level of awesomeness! I can't help you with this problem but let me be the first to say, you are a-ma-zing. That is all.

    Tommy
    A mistake can be valuable or costly, depending on how faithfully you pursue correction

        I looked at the Perl debugger code some months ago with the idea that I could get ideas for a vague project of mine: I had the idea of possibly trying to use Perl and the Perl debugger to try to write a basic shell script debugger (at first, this did not seem completely crazy, I was able to use Perl to write a basic code profiler for another proprietary programming language), but I quickly gave up, it is just awful and way above and beyond my level of comprehension.

Re: Hacking Debugger bugs connected to $^D (())
by tye (Cardinal) on Apr 25, 2013 at 01:41 UTC

    It isn't a bug. It is the feature of Perl where the last statement executed defines the return value. "sub foo {}" doesn't count as a run-time statment.

    If you want "sub foo {}" to give back an empty list, then the solution is simple:

    $ say '$^D=$^D; sub foo {}' 0 $ say '$^D=$^D; (); sub foo {}' $

    - tye        

      > "sub foo {}" doesn't count as a run-time statment.

      Ah I see! Now this explains why wrapping with do {... } helps.

      > $ say '$^D=$^D; (); sub foo {}'

      Good idea! I'll do that, thanks!

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030454]
Approved by marto
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2014-09-17 01:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (55 votes), past polls