Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Accessing lexically scoped variables from a subref

by /dev/urandom (Beadle)
on Dec 28, 2007 at 01:26 UTC ( #659281=note: print w/replies, xml ) Need Help??


in reply to Re: Accessing lexically scoped variables from a subref
in thread Accessing lexically scoped variables from a subref

This is indeed an excellent solution. The only problem is that it's not a pure perl module. However, after looking at the xs, the actual code for the closed_over function is rather small. For a simple subref:
$i = 12; $a = sub { $i };
All it took to get $i was this:
B::svref_2object($a)->PADLIST->ARRAYelt(1)->ARRAYelt(1)->SV->object_2s +vref
The only problem with using this is that I have no idea how it actually works. I just assumed that PADLIST contains all the local variables for that reference, but why the two-dimensional AV after that? Or for that matter, why is $i located on the second position of the second array? The others just return a B::SPECIAL, which I have no idea what its purpose is.

Replies are listed 'Best First'.
Re: Accessing lexically scoped variables from a subref
by benizi (Hermit) on Dec 31, 2007 at 04:41 UTC
    The only problem with using this is that I have no idea how it actually works. I just assumed that PADLIST contains all the local variables for that reference, but why the two-dimensional AV after that?

    Short answer: the first array holds the names, the second holds the values.

    Longer answer:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2022-09-27 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (124 votes). Check out past polls.

    Notices?