SPUG: The string name of a variable (or any identifier)

Michael R. Wolf MichaelRWolf at att.net
Wed Jan 28 03:46:15 CST 2004

Anyone ever wanted to do something like this? I do it all the time in
debugging, but I can't put it in a loop like this

my ($x, $y, $z) = (1..3);
@stuff_to_debug = ($x, $y, $z);
foreach (@stuff_to_debug) {
    printf "%s %d\n", $_.printable_name, $_;

To output this?

$x 1
$y 2
$z 3

Is there a way to find the current identifier name? That is, given $x
or @y (or \$x or \@y), can you get back "$x" or "@y"?

I'd even resort to a Dumper-type interface, requireing that I pass
references, ala

    @stuff_to_debug = (\$x, \$y, \$z);

It's easy (if somewhat awkward syntax) to get the name of the
enclosing function

sub xxx {
    my $fn_name = caller(0)[3]
    my $x = 42;

    warn "problem in $fn_name: $x.printable_name is strangly equal to $x" if $some_condition;

Ideas on how to generalize 'caller' to identifiers (and thingys) other
than enclosing functions?

Michael R. Wolf
    All mammals learn by playing!
        MichaelRWolf at att.net

More information about the spug-list mailing list