Hi all,

        I'm going to write a perl cgi which needs to determine which subroutine to 
call at runtime. And the number of choice is huge (more than 300 and it is growing). I 
know I could do a bunch of if-then-else but it makes the code looks very unreadable.

        I've basically figured out how to do so. I'd like to seek advice from you 
gurus of whether the way I'm doing it is good. As I'm going to run it under mod_perl, 
anything I need consider (memory consumption, performance, etc)?

        The basic idea is, in the package, I export a hash which stores subroutine 
name and the corresponding anonymous sub reference. The caller could determine which 
subroutine to call by getting the sub ref from the hash with the subroutine name.

        Below is the prototype I've written. Any suggestion/comment are highly 
appreciated. Thanks a lot.

-- 
Cheers,
Sammy Lau
Outblaze Co. Ltd.
Mail: [EMAIL PROTECTED]

= myfunc.pm
package myfunc;

require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(%func_ptr);
%func_ptr = ();
$func_ptr{funcone} = sub
 {
   my $var = shift;
   print "F<x) One : $var \n";
 };

$func_ptr{functwo} = sub
 {
   my $var = shift;
   print "F(x) Two : $var \n";
 };

= caller.pl
#!/usr/bin/perl -w
use myfunc;

&{$func_ptr{$ARGV[0]}}($ARGV[1]);

EXAMPLE:
./caller.pl funcone xxx
F<x) One : xxx

./caller.pl functwo yyy
F<x) Two : yyy 


Reply via email to