Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: How to map function name to coderef?

by GrandFather (Sage)
on Jan 07, 2012 at 00:36 UTC ( #946688=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to map function name to coderef?
in thread How to map function name to coderef?

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

Replies are listed 'Best First'.
Re^4: How to map function name to coderef?
by ikegami (Pope) on Jan 07, 2012 at 00:37 UTC
    can searches the inheritance tree. It should be used for methods, not functions.

      And yet can works, and as there is no @main::ISA, and you shouldn't pollute UNIVERSAL,  main->can( $foo ) is perfectly legit

        How do you know there's no @ISA?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://946688]
help
Chatterbox?
[LanX]: shmem: but only because it is taught in school
LanX remembers one curriculum about benny and the punishment whe we didn't laugh ...hill
LanX horror
[Discipulus]: in case of doubt freedom
[LanX]: consideration is a democratic process
[Discipulus]: i'm sure germans has humor: i see them laughing

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2017-04-27 20:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I'm a fool:











    Results (513 votes). Check out past polls.