peek_my#

Return every my variable in scope at a given call-stack depth as a hash of name-to-reference pairs.

Synopsis#

use PadWalker qw(peek_my);
my $h = peek_my($level);

What you get back#

A hash reference. Each key is a variable name with its sigil — '$x', '@list', '%env'. Each value is a reference to the live variable, so dereferencing and assigning through it changes the original. our variables are not included; use peek_our for those.

Examples#

Read a my variable in the current scope:

my $x = 12;
my $h = peek_my(0);
print ${ $h->{'$x'} };      # 12

Modify the caller’s my variable:

sub bump_x {
    my $h = peek_my(1);
    ${ $h->{'$x'} }++;
}
my $x = 5;
bump_x();
print $x;                   # 6

A my declared deeper than the target frame is not in the returned hash — only variables visible at the call site come back.

Edge cases#

  • $level defaults to 0 if omitted.

  • $level deeper than the call stack croaks with Not nested deeply enough.

  • Negative $level is not supported and croaks.

  • Variables declared inside an eval BLOCK between the target frame and the call site are folded in at the sequence number of the call — they become visible alongside the target’s own lexicals.

Differences from upstream#

Fully compatible with upstream PadWalker 2.5.

See also#

  • peek_our — same idea for our variables.

  • peek_sub — inspect a CV’s pad directly, without walking frames.

  • var_name — given a reference, find the name it had in a pad.