This clueless newbie seeks com[m]ents on/improvements to Xyzzy.pm.

Well, OK, since you asked... ;-)

Here is my version of module Xyzzy.pm:

package Xyzzy; use strict; use warnings; use B; use B::Deparse; use Carp; use Devel::FindRef; use Devel::Symdump; use Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( FullyQualify ); sub FullyQualify { my ($global) = @_; my @return; if (ref $global) { if (ref $global eq 'CODE') { # $ref is a ref to an autovivified sub if (B::Deparse->new->coderef2text($global) eq ';') { Carp::cluck 'FullyQualifySubName was passed a coderef +' . 'to a sub that has been autovivified!'; } else { push @return, code_ref($global); } } elsif ((ref $global) =~ m[ ^ (?: ARRAY | HASH | SCALAR ) $ ]x) { push @return, var_ref($global); } else { push @return, 'Your ref is a \'' . join(' ', @{ [ ref $global ] }) . ' ref\'!' +; } } else # ! ref $global { my $module; if ($global =~ m[^(.+)::]) # already qualified { $module = Devel::Symdump->new($1); } else # in this caller { my $caller = (caller)[0]; $module = Devel::Symdump->new($caller); $global = $caller . '::' . $global; } push @return, code_ref(eval '\&' . $global) if grep { m[ ^ $global $ ]x } $module->functions(); push @return, var_ref (eval '\%' . $global) if grep { m[ ^ $global $ ]x } $module->hashes(); push @return, var_ref (eval '\@' . $global) if grep { m[ ^ $global $ ]x } $module->arrays(); push @return, var_ref (eval '\$' . $global) . (@return ? '?' : + '') if grep { m[ ^ $global $ ]x } $module->scalars(); } return @return; } sub code_ref { my ($ref) = @_; return eval { my $obj = B::svref_2object($ref); $obj->GV->STASH->NAME . '::' . $obj->GV->NAME; }; } sub var_ref { my ($ref) = @_; my @globals = grep { $_->[0] =~ m[^the global] } Devel::FindRef:: +find($ref); return $globals[0]->[0] =~ s[^the global ][]r; } 1;

Note that I have consolidated the 5 return statements into a single return at the end of sub FullyQualify. Not necessarily an improvement (although I am a big fan of structured programming), but in this case I think it makes the code easier to follow.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Determining the fully qualified name of a global variable. by Athanasius
in thread Determining the fully qualified name of a global variable. by clueless newbie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.