Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

How to map function name to coderef?

by Anonymous Monk
on Jan 06, 2012 at 23:54 UTC ( #946684=perlquestion: print w/ replies, xml ) Need Help??
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!

Comment on How to map function name to coderef?
Select or Download Code
Re: How to map function name to coderef?
by choroba (Abbot) on Jan 07, 2012 at 00:04 UTC
    You can use $s->() or &$s or &{$s}. This will not work under strict, though.
      Is there a way to do this with strict enabled?

        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

        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->();
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 AnomalousMonk (Abbot) 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 eyepopslikeamosquito (Canon) 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 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946684]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2014-10-21 10:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (100 votes), past polls