[prev in list] [next in list] [prev in thread] [next in thread] 

List:       apache-modperl
Subject:    Re: Determine which subroutine to call at runtime
From:       Matthew Byng-Maddick <mbm () colondot ! net>
Date:       2000-09-29 22:40:37
[Download RAW message or body]

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.

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic