This allows a user to inspect the contents of a code reference's pads. By default it returns all the pads in use so if this is used on a currently executing recursive function then it will return each instance. Since this is a viewer I didn't go to any attempt to provide a way to modify those pads. Five bucks and I'll think about doing that too.

This is for broquaint.

UPDATE: I altered the code to use Devel::Pointer::PP instead of Devel::Pointer. Its a bit more powerful than the XS version.

# Sample code use Data::Dumper; my $k = 42; my $j = sub { $k }; sub r { my $r = shift; $r--; if ( $r and $k and $j->() ) { r( $r ); } else { print Dumper( [ map $_->hash, PadViewer->new( \&r )->pads ] ); } } r( 10 );
package PadViewer; use strict; use 5.006; use warnings FATAL => 'all'; use B; sub new { my $class = shift; my $rv = shift; bless \ B::svref_2object( $rv ), $class; } sub pads { my $self = shift; return unless $$self and $$self->can('PADLIST') and $$self->PADLIST and $$self->PADLIST->can('ARRAY'); my ($names, @values) = $$self->PADLIST->ARRAY; map PadViewer::Pad->new( $names, $_), @values; } package PadViewer::Pad; use strict; use warnings FATAL => 'all'; use Devel::Pointer::PP; sub new { my $class = shift; my $names = shift; my $values = shift; bless { names => $names, values => $values }, $class; } sub names { my $self = shift; return unless $self->{'names'}->can('ARRAY') and $self->{'values'}->can('ARRAY'); my @n = $self->{'names'}->ARRAY; my @v = $self->{'values'}->ARRAY; map +((ref $n[$_] && $n[$_]->can('PV')) ? $n[$_]->PV : undef), grep _is_lexical($v[$_]), 0 .. $#v; } sub values { my $self = shift; return unless $self->{'values'}->can('ARRAY'); my @v = $self->{'values'}->ARRAY; map deref($$_), grep _is_lexical($_), @v; } sub hash { my $self = shift; my %h; @h{ $self->names } = $self->values; \ %h; } sub _is_lexical { my $padv = shift; $padv->can('FLAGS') and $padv->FLAGS & 0x400; } 1;

In reply to PadViewer - a riff on PadWalker by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.