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

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

Oh ye who hold all Perl wisdom!

My Perl foo is sufficient to understand the following:

#!/bin/env perl use strict; use warnings; my $coderef = \&f; print &$coderef, $/; sub f { return 1; }

...which simply outputs upon execution:

1

Yet, what I would really like is to map a string to the code reference of the function itself -- something similar to the following (syntax is incorrect...):

my $s = 'f'; print $s(), $/;

...which would give similar output as the preceding code.

Those that bear all ye knowledge, what is the syntactic incantation?

Thanks!

Replies are listed 'Best First'.
Re: How to map function name to coderef?
by eyepopslikeamosquito (Archbishop) on Jan 07, 2012 at 02:29 UTC

    This looks like an XY Problem. What problem are you trying to solve? What alternatives have you considered?

    Generally, symbolic references should be avoided, as convincingly demonstrated by MJD in three parts: part 1 and part 2 and part 3.

Re: How to map function name to coderef?
by choroba (Cardinal) on Jan 07, 2012 at 00:04 UTC
    You can use $s->()or &$sor &{$s}. This will not work under strict, though.
      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
Re: How to map function name to coderef?
by AnomalousMonk (Archbishop) on Jan 07, 2012 at 00:59 UTC
    ... what I would really like is to map a string to the code reference of the function itself ...

    If you mean an arbitrary string, the following is one way.

    >perl -wMstrict -le "my %f_map = ( glue => \&foo, ); ;; print $f_map{glue}->('hoo'); ;; sub foo { return 'FOO' . $_[0]; } " FOOhoo
Re: How to map function name to coderef?
by bluescreen (Friar) on Jan 07, 2012 at 00:30 UTC

    you can do it using eval to return the coderef, as follows:

    use strict; use warnings; sub some_sub { "hello\n"; } my $sub_name = 'some_sub'; my $coderef = eval("\\&$sub_name"); print &$coderef();
Re: How to map function name to coderef?
by TJPride (Pilgrim) on Jan 07, 2012 at 03:55 UTC
    Working fine for me. Am I missing something?

    use strict; use warnings; sub func1 { print "running func1\n"; } my %funcs = ( 'func1' => \&func1, 'func2' => sub { print "running func2\n"; } ); $funcs{'func1'}->(); $funcs{'func2'}->();
      Am I missing something?

      Yes: the fact that the OP wasn't using hashes for storing code references and was hoping to use a symbolic reference for the function call. Reading the previous replies may have made this clear.

      True laziness is hard work
        I did read them, but apparently it didn't sink in at the time because I couldn't think of a situation where you wouldn't know your function names in advance. The only exception might be if you're taking user input and running the function asked for, which would be inherently unsafe I would think.

        Still, valid point. ikegami's solution is obviously what the OP was looking for. I should stop posting after a hard coding session when my brain is already tired.

          A reply falls below the community's threshold of quality. You may see it by logging in.