Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: calling subroutines from variables

by mr_mischief (Monsignor)
on Aug 21, 2008 at 18:26 UTC ( [id://705929]=note: print w/replies, xml ) Need Help??


in reply to Re^2: calling subroutines from variables
in thread calling subroutines from variables

I'm not sure I want an external source telling me which subs in my package to run without some control over which ones it's allowed to run. A hash of the allowed subrefs with a default error sub makes sense in that context.
use strict; use warnings; sub one { print "one\n"; } sub two { print "two\n"; } sub tres { # some private sub they don't need to access } my %dispatch = ( 'one' => \&one, 'uno' => \&one, 'two' => \&two, 'dos' => \&two, 'default' => sub { print "sorry, $_[0] not found.\n"; }, ); my @do_these = qw( one two uno dos tres ); foreach my $do_this ( @do_these ) { if ( exists $dispatch{ $do_this } ) { $dispatch{ $do_this }->(); } else { $dispatch{ 'default' }->( $do_this ); } }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-03-28 23:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found