peek_sub#
Return the my variables declared inside a given subroutine, whether or not the sub is currently on the call stack.
Synopsis#
use PadWalker qw(peek_sub);
my $h = peek_sub(\&some_sub);
What you get back#
A hash reference keyed on sigiled variable names. The references
point at whatever values the pad currently holds. If the sub is not
active, those values will typically be undef for scalars or empty
for aggregates — the sub has not yet run to populate them. If the
sub is active, the references point at the live values of the
most recent invocation.
Examples#
my $x = "Hello!";
my $r = peek_sub(sub { $x })->{'$x'};
print $$r; # Hello!
Inspect the pad of a named sub:
sub counter { my $n = 0; sub { $n++ } }
my $h = peek_sub(\&counter);
print join ",", sort keys %$h; # $n
Edge cases#
If the sub declares several
myvariables with the same name, only the last one is returned. There is no way to distinguish shadowed entries; this is a known upstream limitation.XS subs and subs without a padlist croak with
PadWalker: cv has no padlist.Passing something that is not a code reference croaks with
PadWalker: cv is not a code reference.
Differences from upstream#
Fully compatible with upstream PadWalker 2.5.
See also#
closed_over— the strict subset ofpeek_subcovering only variables the sub closes over.peek_my— walk the live call stack instead of a named sub.var_name— name-from-reference lookup against the same pad.