QM has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to (re)name an anonymous sub, at least for the purposes of svref_2object and it's brethren. It's cleaner to declare the sub anonymous, since it doesn't do much, is really a method call, and I'm using the anonymous "wrap up the lexicals" feature that named subs complain about.

I've tried the local *__ANON__ = "name"; solution, but it doesn't affect svref_2object. Below is the outline of the code I want to use this in.

use B qw(svref_2object); use Foo::Bar; my $self = Foo::Bar->new; # wrap a method call in a sub my $anon = sub { $self->mumble("blah\n") }; # pass the coderef to $self->grumble($anon, qw(one two three)); # ...meanwhile in Foo::Bar... sub grumble { my $self = shift; my $anon = shift; my @args = @_; # Get the name of the sub my $anon_cv = svref_2object ( $anon ); my $anon_gv = $anon_cv->GV; my $anon_name = $anon_gv->NAME; print "About to call $anon_name...\n"; $anon->(@args); }

Update: Added a catch for $self at the beginning of grumble.

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re: Naming an anonymous sub for svref_2object
by rjt (Curate) on Aug 06, 2013 at 16:27 UTC

    Good question. If I understand your requirement correctly, you want to be able to name a CODE ref such that you can retrieve its name from outside the sub by reference (and possibly inside the sub as well or via Carp/caller?), but you do not appear to want to install the sub into the package's namespace. Is that about right?

    First, local *__ANON__ is just that... local. It only affects the enclosing block (i.e., the sub body in its current execution), and reverts as soon as it goes out of scope when the sub exits. And of course modifying global *__ANON__ (i.e., without local) is not what you want, either.

    Fortunately, there's an easy way around all of this:

    use 5.012; use Sub::Name 'subname'; use B; sub get_sub_name { B::svref_2object(shift)->GV->NAME } my $ref = sub { say 'Our name is: ' . (caller(0))[3] }; $ref->(); say 'Before subname: ' . get_sub_name($ref); subname 'the_pad_test', $ref; $ref->(); say 'After subname: ' . get_sub_name($ref); print "the_pad_test(): "; eval { the_pad_test() }; print $@ =~ /^Undefined sub/ ? "fails (correct)\n" : "ERROR: $@";

    Output:

    Our name is: main::__ANON__ Before subname: __ANON__ Our name is: main::the_pad_test After subname: the_pad_test the_pad_test(): fails (correct)
    use strict; use warnings; omitted for brevity.
      First, local *__ANON__ is just that... local. It only affects the enclosing block (i.e., the sub body in its current execution), and reverts as soon as it goes out of scope when the sub exits.
      Yes, of course. Somehow the local aspect didn't register in my head.

      I had seen the Sub::Name, but there's a lot of resistance to install another module on multiple systems. It will be a debate about whether __ANON__ is easier to take than adding an XS module to the installation distribution.

      Since we do control the grumble code, another option is to pass in an optional name to use in case the coderef is an anonymous sub.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Re: Naming an anonymous sub for svref_2object
by kbrannen (Beadle) on Aug 06, 2013 at 20:56 UTC
    In addition to rjt's solution, you might want to look at http://www.effectiveperlprogramming.com/2011/09/enchant-closures-for-better-debugging-output/
      That's very nice, thanks!

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of