http://www.perlmonks.org?node_id=946685


in reply to How to map function name to coderef?

You can use $s->()or &$sor &{$s}. This will not work under strict, though.

Replies are listed 'Best First'.
Re^2: How to map function name to coderef?
by Anonymous Monk on Jan 07, 2012 at 00:15 UTC
    Is there a way to do this with strict enabled?

      Yes, there are ways of fooling strict. It's simpler and clearer to just turn it off in the code in question.

      my $func_name = 'f'; my $cr = do { no strict 'refs'; \&$func_name }; say $cr->();

      You can use can to return a coderef:

      use strict; use warnings; my $doFoo = main->can ('foo'); print $doFoo ? $doFoo->() : "Can't foo\n"; sub foo { return 1; }

      Prints:

      1
      True laziness is hard work
        can searches the inheritance tree. It should be used for methods, not functions.