Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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.


Comment on Source filter or PadWalker. Both are ugly
Select or Download Code
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: ShownameFilter.pm
    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;
    showname.pl
    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 );
    Output:
    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://588546]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2014-07-22 16:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (119 votes), past polls