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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.