peek_sub#

Return the my variables declared inside a given subroutine, whether or not the sub is currently on the call stack.

Σύνοψη#

use PadWalker qw(peek_sub);
my $h = peek_sub(\&some_sub);

Τι επιστρέφεται#

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.

Παραδείγματα#

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

Οριακές περιπτώσεις#

  • If the sub declares several my variables 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.

Διαφορές από το upstream#

Fully compatible with upstream PadWalker 2.5.

Δείτε επίσης#

  • closed_over - the strict subset of peek_sub covering 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.