Wednesday 10 February 2010

Determining the Function Name of a CODE Ref in Perl

One of the great things about Perl that it inherits from functional programming is the ability to treat functions as data. You can create an function and assign it to a scalar reference. You can take a reference to an existing, named function and do the same. You're then free to pass that reference around, to be invoked later (the latter case is similar to function pointers in C, but the former is very different as the created sub can make use of variables defined in its lexical scope as it forms a closure).

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.