While defining an API to a service I've been writing, I suddenly encountered the need to be able to differentiate between references to named functions and references to anonymous subroutines. Quite simply, once I'd established a set of objects to operate on, a named routine should be invoked once for each, whereas an anonymous routine should be given the entire set to work with (this allowed a great deal of reuse within the existing codebase without limiting flexibility).
While I suspected that Perl might be able to do this, it seemed like it would be some serious magic.
Data::Dumper
displays sub { DUMMY }
when printing CODE
references, so it's largely opaque.However, Perl comes with the B module, providing access to Perl's internals as it's running.
use B;
sub named_routine {}
my $named_ref = \&named_routine;
my $unnamed_ref = sub {};
printf "%s\n", B::svref_2object($named_ref)->GV->NAME;
printf "%s\n", B::svref_2object($unnamed_ref)->GV->NAME;
this yields:
named_routine
__ANON__
So, provided that you're willing to avoid calling your subroutines __ANON__, you've got a good indication of what your
CODE
reference actually points to.