Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How to map function name to coderef?

by choroba (Abbot)
on Jan 07, 2012 at 00:04 UTC ( #946685=note: print w/ replies, xml ) Need Help??


in reply to How to map function name to coderef?

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


Comment on Re: How to map function name to coderef?
Select or Download Code
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?

      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.

      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->();

Log In?
Username:
Password:

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

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

    The best computer themed movie is:











    Results (134 votes), past polls