Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.)


In reply to Re: Accessing lexically scoped variables from a subref by benizi
in thread Accessing lexically scoped variables from a subref by /dev/urandom

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 sharing their wisdom with the Monastery: (6)
As of 2024-03-28 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found