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.


In reply to Re: PadWalker's closed_over - but for our variables? by haukex
in thread PadWalker's closed_over - but for our variables? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.