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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.