I looked at the current source of B::Deparse, and so far I've found two functions that are responsible for looking up variables. Hooking into these is a hackish way to get the variable names :-) There may be more things that need to be hooked (Update: and the final example shows it's not perfect), but perhaps this is a starting point:
use warnings;
use strict;
use B::Deparse;
use Data::Dump;
use Hook::LexWrap;
my $deparse = B::Deparse->new();
$deparse->ambient_pragmas(strict=>'all', warnings=>'all');
wrap 'B::Deparse::stash_variable', post => sub {
my $rv = pop @_;
my ($self, $prefix, $name, $cx) = @_;
dd 'stash_variable', $prefix, $name, $rv;
};
wrap 'B::Deparse::stash_variable_name', post => sub {
my $rv = pop @_;
my ($self, $prefix, $gv) = @_;
dd 'stash_variable_name', $prefix, $rv;
};
our $foo = "World";
my $bar = "Hello";
our @foo = 1..3;
my @bar = 4..5;
our %foo = ( abc => 444 );
my %bar = ( def => 222 );
our $ref = { xyz => 123 };
$deparse->coderef2text(sub {
print "$bar, $foo\n";
print "@foo @bar\n";
print "$foo[1] $bar[1]\n";
my %dummy = %foo = %bar;
print "$foo{abc} $bar{def}\n";
print $ref->{xyz};
});
__END__
("stash_variable", "\$", "foo", ["\$foo"])
("stash_variable", "\@", "foo", ["\@foo"])
("stash_variable_name", "\@", ["foo", 0])
("stash_variable", "%", "foo", "%foo")
("stash_variable_name", "%", ["foo", 0])
("stash_variable_name", "%", ["main::ref", 0])
I showed an example of PadWalker here.