Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Source filter or PadWalker. Both are ugly

by imp (Priest)
on Dec 08, 2006 at 09:36 UTC ( #588546=note: print w/replies, xml ) Need Help??

in reply to How to extract the name of a variable?

The easiest way to provide the variable name is with Data::Dumper::Simple, which uses a source filter. Since the variable name in question is one level higher up the call chain there aren't many options other than PadWalker, which is usually an indication that you are being a bad person.

Here is an implementation using PadWalker:

use PadWalker qw( peek_my peek_our ); use strict; use warnings; sub name(\[$@%&*]) { for my $pad ( peek_my(1), peek_our(1)) { while (my ($k,$v) = each %$pad) { if ($v == $_[0]) { return $k; } } } return; } our $x = [0]; my @a = (1,2); my %foo = (1 => 2); my $subref = sub {}; printf "Name: %s\n", name($x); printf "Name: %s\n", name(@a); printf "Name: %s\n", name(%foo); printf "Name: %s\n", name($subref);
This will work for variables declared with my or our, but not for things like $::x.

Note that this will not work on older perls that do not support the (\[$@^&*]) style prototype.

Replies are listed 'Best First'.
Re: Source filter or PadWalker. Both are ugly
by imp (Priest) on Dec 08, 2006 at 11:18 UTC
    It didn't seem fair (or fun) to provide a PadWalker example, and not a Filter::Simple one. I don't recommend using either of these approaches, but here is a basic(?) filter implementation:
    package ShownameFilter; use Filter::Simple; FILTER_ONLY code => sub { s{ showname\( ( # Capture 2 (closes second) \s* \\? # References [\$@%*]+ # Scalar, array, hash, glob ( # Capture 1 (closes first) \w* # alphanumeric. Doesn't catch a lot of perl spe +cial vars ) # End capture 1 \s* ) # End capture 2 \) } {showname('$2',$1)}xg; }; 1;
    use strict; use warnings; use ShownameFilter; sub showname { my ($name,$value) = @_; print "name: $name\n"; } my ($x, $x12, @foo, %bar); showname($x); showname( $x12 ); showname( \$x12 ); showname( @foo );
    name: x name: x12 name: x12 name: foo
    Again - I don't recommend doing this.
Re: Source filter or PadWalker. Both are ugly
by Anonymous Monk on Dec 08, 2006 at 21:32 UTC
    Thank you imp

    Your insight is truly appreciated. I tried Data::Dumper::Simple and except for pretty printing a hash (name => value where the fat comma "=>" lines up in a vertical column), it does what I was trying to achieve with my 3 subs showvar(),show(),showHASH

    Incidently, the author Curtis "Ovid" Poe explains in more detail the reasons why people like myself would want to use his module as a simple debugging aid. ie. typos and "succintness"

    Best regards

    Bernie in Montreal.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588546]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2018-04-23 06:22 GMT
Find Nodes?
    Voting Booth?