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;
@scatchpad = $pad->ARRAY;
@varnames = $scratchpad[0]->ARRAY;
@varcontents = $scratchpad[1]->ARRAY;
for (0 .. $
eval {
my $name = $varnames[$_]->PV;
my $content;
$content = $varcontent[$_]->IVX if (scalar($varcontent[$_]) =~ /PVIV=/);
$content = B::perlstring($varcontent[$_]->PV) if (scalar($varcontent[$_]) =~ /PV=/);
print "DEBUGGER> Local variable: ", $name, " = ", $content, "\n";
};
}
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;
my @scratchpad = $pad->ARRAY;
my @varnames = $scratchpad[0]->ARRAY;
my @varcontents = $scratchpad[1]->ARRAY;
my @vars;
for (0 .. $#varnames) {
eval { push @vars, $varnames[$_]->PV; };
if ($@) { push @vars, "undef"; }
}
my @cont;
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).