The issue of access to lexical coverage using B

For debugging purposes, I would like to access the lexical domain of various routines with a specific set of attributes. It works great. I get the problem when the first variable stores the string, then I get the empty string. I am doing something like this:

$pad = $cv->PADLIST; # $cv is the coderef to the sub
@scatchpad = $pad->ARRAY; # getting the scratchpad
@varnames = $scratchpad[0]->ARRAY; # getting the variablenames
@varcontents = $scratchpad[1]->ARRAY; # getting the Content from the vars

for (0 .. $#varnames) {
    eval {
        my $name = $varnames[$_]->PV;
        my $content;
        # following line  matches numbers, works so far
        $content = $varcontent[$_]->IVX if (scalar($varcontent[$_]) =~ /PVIV=/);
        # should match strings, but does give me undef
        $content = B::perlstring($varcontent[$_]->PV) if (scalar($varcontent[$_]) =~ /PV=/);
        print "DEBUGGER> Local variable: ", $name, " = ", $content, "\n";
    }; # there are Special vars that throw a error, but i don't care about them
}

As I said in a comment, eval is to prevent errors from B :: Special objects in notepad. Output:

Local variable: $test = 42
Local variable: $text = 0

The first output is OK, the second should print "TEXT" instead of 0.

What am I doing wrong?

EDIT: with a bit of coding, I got all the values ​​of the variables, but was not stored in the same @varnames and @varcontents indexes. So, now the question is how (in what order) the values ​​are stored in @varcontents.

use strict;
use warnings;
use B;

sub testsub {
    my $testvar1 = 42;
    my $testvar2 = 21;
    my $testvar3 = "testval3";
    print "printtest1";
    my $testvar4 = "testval4";
    print "printtest2";
    return "returnval";
}

no warnings "uninitialized";

my $coderef = \&testsub;
my $cv = B::svref_2object ( $coderef );
my $pad = $cv->PADLIST; # get scratchpad object
my @scratchpad = $pad->ARRAY;
my @varnames = $scratchpad[0]->ARRAY; # get varnames out of scratchpad
my @varcontents = $scratchpad[1]->ARRAY; # get content array out of scratchpad

my @vars; # array to store variable names adn "undef" for special objects (print-values, return-values, etc.)

for (0 .. $#varnames) {
    eval { push @vars, $varnames[$_]->PV; };
    if ($@) { push @vars, "undef"; }
}

my @cont; # array to store the content of the variables and special objects

for (0 .. $#varcontents) {
    eval { push @cont, $varcontents[$_]->IV; };
    eval { push @cont, $varcontents[$_]->PV; };
}

print $vars[$_], "\t\t\t", $cont[$_], "\n" for (0 .. $#cont);

EDIT2: runnable script, : Variablenames variablevalues ​​ (@varnames @varcontents).

+4

All Articles