var_name#

Given a reference to a variable, return the name that variable has in a specified subroutine’s pad - or undef if it is not there.

Σύνοψη#

use PadWalker qw(var_name);
my $name = var_name($level, \$var);
my $name = var_name(\&sub,  \$var);

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

A string - the sigiled pad name, e.g. '$foo' or '@items'. If the reference does not correspond to any pad slot in the target sub, returns undef.

The first argument selects the sub to search: a non-negative integer is interpreted the same as the argument to peek_my (walk the call stack), a code reference is searched directly.

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

Look up the name of a local variable by reference:

my $foo;
print var_name(0, \$foo);        # $foo

Wrap it for use from helper subs:

sub my_name {
    return var_name(1, shift);
}
my $bar;
print my_name(\$bar);            # $bar

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

  • References to globals, package variables, or anonymous SVs return undef - only pad entries are searched.

  • If several pad slots point at the same SV, the last one wins (same caveat as peek_sub).

  • The second argument must be a reference; passing a plain value croaks with Usage: PadWalker::var_name(sub, var_ref).

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

Fully compatible with upstream PadWalker 2.5.

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

  • peek_my - the inverse direction, names to references.

  • peek_sub - all names in a sub’s pad regardless of value.

  • closed_over - captured lexicals only.