Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to map function name to coderef?

by TJPride (Pilgrim)
on Jan 07, 2012 at 03:55 UTC ( #946711=note: print w/ replies, xml ) Need Help??


in reply to How to map function name to coderef?

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


Comment on Re: How to map function name to coderef?
Download Code
Re^2: How to map function name to coderef?
by GrandFather (Cardinal) on Jan 07, 2012 at 05:50 UTC
    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.

        Actually Ikegami's 'solution' ("shut up and do what I tell you") almost certainly isn't what the OP is looking for, although it is one way of solving the problem that the OP asked about.

        eyepopslikeamosquito really gave the best answer (in terms of likely best outcome for the OP) suggesting that the OP was asking the wrong question. Using symbolic references in Perl is almost never involved in a "best" solution.

        And even in the narrow context of the question asked by the OP it's not at all clear that Ikegami's 'solution' solution is best. Consider:

        use strict; use warnings; for my $fName ('foo', 'wibble') { my $doFoo = main->can($fName); print "Strictly: ", $doFoo ? $doFoo->() . "\n" : "Can't $fName\n"; my $cr = do {no strict; \&$fName}; print "Laxly: ", $cr ? $cr->() . "\n" : "Can't $fName\n"; } sub foo { return 1; }

        which prints:

        Strictly: 1 Laxly: 1 Strictly: Can't wibble Undefined subroutine &main::wibble called at noname2.pl line 13.

        If the OP wants the code to die on a bad sub name then turning a blind eye to the symbolic reference is fine. If the OP wants to detect and handle a missing sub then using the can trick is a better solution. If the OP always knows what the name of the sub will be then there is no need for using a symbolic reference at all. But the OP doesn't give us enough information to make that determination.

        There are few (no?) hard and fast rules about what constitutes a 'best' anything. You would do yourself a service by disabusing yourself of the notion that there is a one true best solution to almost anything.

        True laziness is hard work

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2014-12-27 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls