Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Usage of the DB-module (debugger)

by Tobiwan (Beadle)
on Mar 22, 2007 at 09:26 UTC ( [id://605978]=perlquestion: print w/replies, xml ) Need Help??

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

I've to find out, how I can read all lexical variables (declared with "my") in a block of the callerstack. I've found out, that Carp::Heavy uses some of the "magic" things in package DB like the following, to access to the given parameters:
use strict; use CGI; my $c = new CGI; $c->param('test', 'this_is_my_text'); sub one { my $this_i_want_to_access = 'no_way'; return two($c) } sub two { my $i = 0; for(1..4) { resolve_caller($_) } } sub resolve_caller { my $i = shift; # Ups! Inline usage of a package. Not my style, but there is no ot +her way for do some magic. package DB; my %call_info; @call_info{qw(pack file line sub has_args wantarray evaltext is_re +quire)} = caller($i); return unless (defined $call_info{pack}); if ($call_info{has_args}) { # This is only filled, when you call "caller($i)" IN the packa +ge DB!!! Otherwise it's empty. It's magic? my @args = @DB::args; print "Args: @args"; foreach my $c (@args) { if(ref($c)) { print " > parameter test = " . $c->param('test'); } } print "\n"; return; } return; } print one(1);
Have a look at the two comments. There is some magic in this code. I can use it, but I don't understand. This prints out:
Args: CGI=HASH(0x8148d48) > parameter test = this_is_my_text Args: 1
So at this way, I can access to lexical variables given into subroutines in callerstack. But I want to look around in the subroutines and, for example, want to access to $this_i_want_to_access.

There are some special variables, that sounds good, but what magical things I've to do, to fill them? How can I find some documentation about this?

$DB::trace @DB::dbline %DB::dbline @DB::ret $DB::ret %DB::sub

Replies are listed 'Best First'.
Re: Usage of the DB-module (debugger)
by xdg (Monsignor) on Mar 22, 2007 at 11:53 UTC

    As is often the case, you may want to start with perldoc:

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Usage of the DB-module (debugger)
by Anonymous Monk on Mar 22, 2007 at 12:00 UTC
Re: Usage of the DB-module (debugger)
by Anno (Deacon) on Mar 22, 2007 at 13:14 UTC
    The code in Carp::Heavy doesn't access lexical variables but the parameter arrays @_ of calling functions up the stack. This is enabled by calling caller() from the package DB, presumably an efficiency hack.

    To access lexical variables in calling subs there's the module PadWalker available from CPAN. All I remember from playing with it once is that it wasn't quite as easy to use as I would have wished.

    Anno

      Yes, it access the @_ but in fact, thats a lexical array for every subroutine, I think. But the PadWalker is exactly, what I'm searching for. Thanks for this wisdom.
        No, @_ is a (special) package variable, not a lexical one.

        Anno

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-03-29 00:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found