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

Hello Monks,

In the process of working on a module for persistent coderefs, I have been intrigued as to the wherefores of the debugger when executing inside eval code. Usually it gets it right, and displays the correct line of code it is evaluating.

My question is: how does the debugger know anything about lines of dynamic code under the circumstances?

Secondly, if the code being run is a coderef, does the debugger have a clue about this?

I am interested in some general infomation about how the debugger obtains information about what it is running, in particular, code inside evals and passed coderefs.

Here is the code I have been developing. I am trying to make the debug mode (as set by $foo->debug(1)) able to give a better hint as to what is happening at expense of execution speed.

use strict; package Persistent::Code; sub new { my ($class,$src,$permflag) = @_; my %flags; $flags{'perm'} = 1 if $permflag; my $cr = eval "sub { $src }"; return undef if $@; my $self = [$cr,$src,\%flags]; bless $self, $class; } sub src { my $self = shift; $self->[1]; } sub coderef { my $self = shift; $self->[0]; } sub debug { my $self = shift; @_ ? ($self->[2]{debug} = shift) : $self->[2]{debug}; } sub perm { my $self = shift; @_ ? ($self->[2]{perm} = shift) : $self->[2]{perm}; } sub run { my $self = shift; my $pkg = ref $self; if ($self->[2]{debug}) { my $src = $self->[1]; print "eval: ",$src; wantarray ? eval "(&sub {$src}(@_))" : eval "&sub {$src}(@_)" +; } else { goto &{$self->[0]}; } } sub STORABLE_freeze { my ($self,$cloning) = @_; $self->[2]{perm} ? $self->[1] : ''; } sub STORABLE_thaw { my ($obj,$cloning,$code) = @_; return if !$code; my $class = ref $obj; my $newobj = $class->new($code,'perm'); @$obj = @$newobj; } 1;

Replies are listed 'Best First'.
Re: On debugging coderefs and inside evals
by erikharrison (Deacon) on Mar 16, 2002 at 03:25 UTC

    I know thatthe debugger store lots of information in special variables, including an array containing the lines being eval'ed. It may seem like an asinine answer, but perldebguts in the docs discusses how the debugger works and how to write your own debugger.

    Cheers,
    Erik
      Thanks for the tip Erik, perldebguts has some clues as to what is happening.

      I have made some progress in that when the debugger is in a normal module, variables _<$filename contain what the documentation says to expect.

      When I'm inside one of my coderefs, I get the string __ANON__ or something like eval 6 quoted as a module name. I tried stuffing an array called _<__ANON with my lines of code, but this made no difference. Also, I'm presuming these debug variables are package not lexical, as they appear to be lumped together in a typeglob. Evidence suggests that all these debug variables go into package main::.

      I want a way of priming the debugger with my lines of code before it drops into the coderef.

      Update: Looking at perl5db.pl and perlfunc on caller, I see that what I need to do is fake a call, supplying the correct source and line information so that caller can see it. How to do this? I've re-read the chapters on XS programming in Advanced Perl programming , but this does not even mention the supplementary information passed into a call for the debugger. Maybe this book pre-dates perl5db. Any help would be appreciated.

      --rW