Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: PadWalker's closed_over - but for our variables?

by haukex (Archbishop)
on Apr 04, 2021 at 08:55 UTC ( [id://11130797]=note: print w/replies, xml ) Need Help??


in reply to PadWalker's closed_over - but for our variables?

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.

Replies are listed 'Best First'.
Re^2: PadWalker's closed_over - but for our variables?
by LanX (Saint) on Apr 04, 2021 at 09:37 UTC
    Thanks, coincidentally I already went the way of monkey patching Deparse in the meantime and have good results yet. :)

    Edit:

    Tho Hook::LexWrap might be a saner way to do it, need to get acquainted with this module...

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11130797]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found