in reply to Saving/recovering sub refs in a file

given a sub ref is there any way I can convert that back into a string

You can get the subroutine source from a reference using B::Deparse. For eample:
#!/usr/bin/perl use strict; use warnings; use B::Deparse; sub myfunc { print "Hello @_\n"; return 42 } my $tricky = \&myfunc; my $deparse = B::Deparse->new(); my $body = $deparse->coderef2text($tricky); print "$body\n";
Gives:
{ use warnings; use strict 'refs'; print "Hello @_\n"; return 42; }

Replies are listed 'Best First'.
Re^2: Saving/recovering sub refs in a file
by madscientist (Novice) on Jun 16, 2010 at 15:29 UTC
    You can get the subroutine source from a reference using B::Deparse.

    Hm. That's interesting. What I was really looking for was a way to find the string name of a sub, not the source for it. IOW, suppose I had:

    package my::Plugins::SomePlugin; sub myhandler { do_something(); } my::Error::register(\&myhandler);
    Then inside my::Error::register() somehow I could convert the ref I'd been passed back into the name of the sub (in this case the string "my::Plugins::SomePlugin::myhandler").

      I would do it this way:
      package My::Plugins::SomePlugin; use strict; use warnings; use Sub::Identify ':all'; sub name { my $subname = sub_name(\&myhandler); defined $subname and print "$subname\n"; } sub myhandler { do_something(); } My::Plugins::SomePlugin::name;