Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Accessing lexically scoped variables from a subref

by benizi (Hermit)
on Dec 31, 2007 at 04:41 UTC ( #659701=note: print w/replies, xml ) Need Help??


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

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:

#!/usr/bin/perl use 5.010; # sorry, use feature ':5.10'; # testing out 5.10 features use strict; use warnings; use List::Util 'max'; use B; sub dumpem; { my $closed = 3; # sub1 closes around this var sub sub1 { state $state_in = 5; # a state var in sub1 my $my_in = 6; # a my var in sub1 my %hash = (a=>2); # a hash in sub1 dumpem "sub1"; $my_in + $closed; # use it so it's not optimized away } } sub B2str { my $o = shift; return $o unless ref $o; my $r = "$o"; given(B::class($o)) { when ('SPECIAL') { $r = $B::specialsv_name[$$o] } when (['PVIV','IV']) { $r = $o->IVX } when (['PVNV','PV']) { continue if !defined($r = $o->PV); $r = + "'$r'" } when (['PVNV','NV']) { $r = $o->NVX } when (['HV','AV']) { my ($open, $close, @sep) = split /\|/, /AV/ ? '(|)|,' : '{ +|}| =>|,'; my @in = map B2str($_), $o->ARRAY; $in[$_] .= ($_!=$#in?$sep[$_%@sep]:'') for 0..$#in; $r = join ' ', $open, @in, $close; } when ('NULL') { $r = ref $o } default { $r = "$o" } } $r; } sub dumpem { my $in = shift; my ($nam, $val) = B::svref_2object(\&sub1)->PADLIST->ARRAY; s/^'//, s/'$// for my @names = map B2str($_), $nam->ARRAY; my @vals = map B2str($_), $val->ARRAY; my $l = max map length, @names; $_ = sprintf "%-*s", $l, $_ for @names; say for "", "In $in", map " $names[$_] = $vals[$_];", 0..$#names; } dumpem "main before sub1"; sub1 7, 11, 13; # 7,11,13 just demonstrates where @_ is stored dumpem "main after sub1"; __END__ Output: In main before sub1 &PL_sv_undef = ( ); $state_in = B::NULL; $my_in = B::NULL; %hash = { }; &PL_sv_undef = B::NULL; &PL_sv_undef = B::NULL; $closed = 3; &PL_sv_undef = B::NULL; In sub1 &PL_sv_undef = ( 7, 11, 13 ); $state_in = 5; $my_in = 6; %hash = { a => 2 }; &PL_sv_undef = B::NULL; &PL_sv_undef = B::NULL; $closed = 3; &PL_sv_undef = B::NULL; In main after sub1 &PL_sv_undef = ( ); $state_in = 5; $my_in = 6; %hash = { }; &PL_sv_undef = B::NULL; &PL_sv_undef = B::NULL; $closed = 3; &PL_sv_undef = 9;

Some interesting things: '@_' is stored in the first unnamed slot. The last unnamed slot is a temp var for the return result ($my_in + $closed) == 9.

Also see: Re^7: private recursive subroutines. (Wrote this node before just now finding that -- important note is that PADLIST has a stack of values, but just one set of names.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2022-12-08 19:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?