Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-23 22:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found