Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Subroutines reference from data

by chip (Curate)
on Mar 22, 2004 at 23:02 UTC ( #338815=note: print w/ replies, xml ) Need Help??


in reply to Subroutines reference from data

For most purposes, the UNIVERSAL::can method will serve:

if (my $code = main->can($key)) { &$code() };

If you don't mind turning off strict 'refs', there's the ever-popular defined:

&$key() if defined &$key;

edited: to make the first example strict-safe. Thanks, tinita.

    -- Chip Salzenberg, Free-Floating Agent of Chaos


Comment on Re: Subroutines reference from data
Select or Download Code
Re: Re: Subroutines reference from data
by Syrkres (Sexton) on Mar 22, 2004 at 23:24 UTC
    First, Thanks for the quick reply. Gotta love it! Second, Worked excellent. Thanks.
Re: Re: Subroutines reference from data
by tinita (Parson) on Mar 22, 2004 at 23:29 UTC
    &$key() if main->can($key);
    is not strict safe, but:
    if (my $sub = main->can($key)) { $sub->(); }
    also nice (but maybe too obfuscated?) would be (main->can($key)||sub{})->();

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2015-07-04 03:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls