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.