clueless newbie has asked for the wisdom of the Perl Monks concerning the following question:
Here this clueless newbie asked "Can the fully qualified name of a global variable be determined?". Making use of Anonymous Monk's suggestion, this clueless newbie took a look at Devel::FindRef and jumped perhaps to an erronous conclusion.
Given that Globals.pm, Globals_A.pm, Globals_H.pm and Global_S are
package Globals; use Exporter; our @ISA=qw(Exporter); our @EXPORT=qw($Var $var @var %var); use Globals_S; use Globals_A; use Globals_H; use strict; use warnings; use vars qw($Var); $Var='$Var'; $var='scalar'; @var=qw(array array); %var=(hash=>1); 1;
package Globals_A; use Exporter; our @ISA=qw(Exporter); our @EXPORT=qw(@var); use strict; use warnings; use vars qw(@var); 1 __END__
package Globals_H; use Exporter; our @ISA=qw(Exporter); our @EXPORT=qw(%var); use strict; use warnings; use vars qw(%var); 1 __END__
package Globals_S; use Exporter; our @ISA=qw(Exporter); our @EXPORT=qw($var); use strict; use warnings; use vars qw($var); 1 __END__
This
#!/usr/bin/perl use strict; use warnings; #use Globals_S; use Globals_A; use Globals_H; use Globals; use vars qw($myvar @myvar); use Smart::Comments; use Devel::FindRef; use Xyzzy qw(FullyQualify); print "passing refs:\n"; print "\&FullyQualify is @{[FullyQualify(\&FullyQualify)]}.\n"; print "\$Var is ".join (' ',FullyQualify(\$Var)).".\n"; print "\%var is ".join (' ',FullyQualify(\%var)).".\n"; print "\@var is ".join (' ',FullyQualify(\@var)).".\n"; print "\$var is ".join (' ',FullyQualify(\$var)).".\n"; print "\nThe following is less reliable see the Devel::Symdump documen +ation\n"; print "passing strings:\n"; for (qw(FullyQualify Var var Globals::var)) { print "'$_' is @{[FullyQualify($_)]}\n"; } ; exit;
together with Xyzzy.pm
package Xyzzy; use Exporter; our @ISA=qw(Exporter); our @EXPORT_OK=qw(FullyQualify); use B; use B::Deparse; use Carp; use Devel::FindRef; use Devel::Symdump; use strict; use warnings; #use Smart::Comments; # Given a string or a ref # return its fully qualified name(s) sub FullyQualify { local *coderef=sub { eval { my $obj = B::svref_2object(shift); $obj->GV->STASH->NAME . "::" . $obj->GV->NAME; } || undef; }; #coderef: local *variableref=sub { (my $tmp=(grep {$_->[0] =~ m{^the global}} Devel::Find +Ref::find($_[0]))[0]->[0]) =~ s{^the global }{}; ### $tmp return $tmp; }; # variableref: unless (ref $_[0]) { # scalar my $name=shift(); my $module; if ($name =~ m{^(.+)::}) { # already qualified $module=Devel::Symdump->new($1); } else { # in this caller $module=Devel::Symdump->new(my $caller=(caller)[0]); $name=$caller.'::'.$name; }; my @return; if (grep {m{^$name$}} $module->functions()) { push(@return,coderef(eval '\&'.$name)) }; if (grep {m{^$name$}} $module->hashes()) { push(@return,variableref(eval '\%'.$name)) }; if (grep {m{^$name$}} $module->arrays()) { push(@return,variableref(eval '\@'.$name)) }; if (grep {m{^$name$}} $module->scalars()) { if (@return) { push(@return,variableref(eval '\$'.$name).'?'); } else { push(@return,variableref(eval '\$'.$name)); }; }; return @return; } else { my $ref=shift; if (ref $ref eq 'CODE') { unless (B::Deparse->new->coderef2text($ref) ne ';') { +# $ref is a ref to an autovivified sub Carp::cluck "FullyQualifySubName was passed a code +ref to a sub that has been autovivified!"; return undef; }; return coderef($ref); } elsif ((ref $ref) =~ m{^(?:ARRAY|HASH|SCALAR)$}) { return variableref($ref); } else { return "Your ref is a '@{[ref $ref]} ref'!"; }; }; }; __END__
gives
passing refs: &FullyQualify is Xyzzy::FullyQualify. $Var is $Globals::Var. %var is %Globals_H::var. @var is @Globals_A::var. $var is $Globals_S::var. The following is less reliable see the Devel::Symdump documenation passing strings: 'FullyQualify' is Xyzzy::FullyQualify $main::FullyQualify? 'Var' is $Globals::Var 'var' is %Globals_H::var @Globals_A::var $Globals_S::var? 'Globals::var' is %Globals_H::var @Globals_A::var $Globals_S::var?
This clueless newbie seeks coments on/improvements to Xyzzy.pm.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Determining the fully qualified name of a global variable.
by Athanasius (Archbishop) on Jan 27, 2013 at 03:44 UTC | |
by LanX (Saint) on Jan 27, 2013 at 09:28 UTC | |
by Anonymous Monk on Jan 27, 2013 at 09:01 UTC |