As far as I know, we can only do it for CODE references:
# obtain CODEref name
use B;
sub coderef2name {
my ($coderef) = @_;
return unless UNIVERSAL::isa($coderef, "CODE");
my $obj = B::svref_2object($coderef);
return $obj->GV->STASH->NAME . "::" . $obj->GV->NAME;
}
Update: Changed to use GV->STASH->NAME instead.
Note that:
> perl -MDevel::Peek -e '$var = 123; Dump \$var; sub my_sub {}; Dump \
+&my_sub'
SV = RV(0x8073260) at 0x804cc28
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x8065648
SV = IV(0x8068920) at 0x8065648
REFCNT = 2
FLAGS = (IOK,pIOK)
IV = 123
SV = RV(0x8073260) at 0x804cc28
REFCNT = 1
FLAGS = (TEMP,ROK)
RV = 0x8064b34
SV = PVCV(0x8061538) at 0x8064b34
REFCNT = 2
FLAGS = ()
IV = 0
NV = 0
COMP_STASH = 0x804cb50 "main"
START = 0x806d6a8 ===> 1053
ROOT = 0x806fa68
XSUB = 0x0
XSUBANY = 0
GVGV::GV = 0x8065684 "main" :: "my_sub"
FILE = "-e"
DEPTH = 0
FLAGS = 0x0
OUTSIDE_SEQ = 91
PADLIST = 0x8064b04
PADNAME = 0x804cce8(0x8088758) PAD = 0x8065678(0x805e5b0)
OUTSIDE = 0x804cd78 (MAIN)
-
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.
|