You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by sa...@sammy.portal2.com on 2000/09/29 23:27:11 UTC

Determine which subroutine to call at runtime

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: sammy@outblaze.com

= 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 



Re: Determine which subroutine to call at runtime

Posted by Matthew Byng-Maddick <mb...@colondot.net>.
On Sat, 30 Sep 2000 sammy@sammy.portal2.com wrote:
> 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.

Yes. And unmaintainable.

> 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)?

It depends on the spec of your server, mod_perl servers can take up a lot
of memory. All of your code will be loaded all of the time, so you want
the loading of the hash to occur at compile time, so it will be shared as
much as possible across all the apache children.

> 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.

This is a good way of doing things.

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

You might want to think about the security of your system. For example, is
there any verification that something the user has just typed in is a
sensible thing to run code with? Is there any kind of verification that
this function exists first? can they get stuff into the hash by some
spurious quoting?

MBM

-- 
The  Universe  shipped by  weight, not by  volume.  Some  expansion of the
contents may have occurred during shipment.