This is a quick sift through the main package symbol table to find subroutines, so that if you have a coderef that actually refers to a named subroutine, you can get its name.
If your sub might belong to another package, you'll need to have the search recurse through the other namespaces.
#!perl -l
use strict;
use warnings;
sub routine1 {
print "Who am I?";
}
sub routine2 {
print "Where am I?";
}
my %all_subs =
map { (\&{$main::{$_}} => $_) }
grep { defined &{$main::{$_}} }
keys %{main::}
;
my $some_sub = \&routine1;
if ($all_subs{$some_sub}) {
print "Found $all_subs{$some_sub}"
}
else {
print "Sub not found";
}