So I have this code (sub refto), it seems to work fine, but I feel like I'm missing a utility function somewhere - core modules very much preferred - that does this without me having to fall back on B. I've checked Scalar::Util and Symbol. I'm probably just having a dense moment - does anyone happen to know off the top of their head?

use warnings; use strict; use B qw/svref_2object/; sub refto { my $ref = shift; my $gv = svref_2object($ref)->GV; return $gv->STASH->NAME . '::' . $gv->NAME; } sub foo {} sub Foo::Bar::blah { } print refto(\&foo), "\n"; # prints "main::foo" print refto(\&Foo::Bar::blah), "\n"; # prints "Foo::Bar::blah" print refto(sub{}), "\n"; # prints "main::__ANON__"

Background: I am storing code references in a table and want to give each one a sensible name. For __ANON__ code refs, I'll probably append the address to make them unique.


In reply to Name for what a coderef is pointing at by haukex

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.