LanX has asked for the wisdom of the Perl Monks concerning the following question:
is there any better way to find the "our" name of a package variable other than searching thru all possibilities?
And why can't I find $x? cause its an alias?
use feature qw(say); use PadWalker qw(var_name peek_our); use Data::Dump qw(pp); sub our_name { my $varref=\shift; my $h_our=peek_our(1); while ( my ($name,$ref) = each %$h_our ) { return $name if $ref == $varref; } return; } package test; our $bar=10; package main; for our $x (1) { say our_name($bar); # > $bar say our_name($x); # > '' }
Cheers Rolf
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Inspecting the name of a variable
by philiprbrenan (Monk) on Sep 01, 2012 at 19:49 UTC | |
by LanX (Saint) on Sep 01, 2012 at 20:18 UTC | |
by remiah (Hermit) on Sep 02, 2012 at 14:46 UTC | |
by LanX (Saint) on Sep 02, 2012 at 20:50 UTC | |
by remiah (Hermit) on Sep 03, 2012 at 01:18 UTC | |
|