Calling functions by name and load modules on demand
- From: "ska" <skg@xxxxxxxxxxxxxxxxxx>
- Date: 19 May 2006 01:20:24 -0700
Hello,
I'm developing a program that gets the name of an object, looks up what
groups (e.g. web, psoix) the object belongs to in a DB, and finally
spawns one function per group. (My implementation is not
object-orientated.)
There are plenty of groups, of which only a minimal subset is needed
for each run, however, the program is invoked rather often, hence, I'd
like to load the required modules on demand only.
All actions of a particular group are locatated in its own module, say:
Groups::Web.pm
Groups::Posix.pm
etc.
The interface of all of them is equal.
When I want to perform an action (aka function) on an object, the
function call shall be delegated to the equally named function of all
the Groups:: modules the object belongs to.
This is my test vehicle for the principle:
sub AUTOLOAD {
my $fnam = $AUTOLOAD;
print "AUTOLOADing $fnam\n";
$fnam =~ s/^.*://;
my $g = shift;
foreach my $grp (@{$g}) {
my $module = "Groups::" . ucfirst($grp);
my $fct = "${module}::$fnam";
unless(defined(&{$fct})) {
print "Loading $module\n";
eval "require $module";
$@ and die "Failed with: $@\n";
}
&{$fct} (@_);
}
}
fct ([qw/web posix/], 1,2,3);
fct1 ([qw/web mail NotExistant/], 1);
Which is working.
However, I'm wondering whether there is some way in order to have perl
autoload the modules itself or pre-compile the modules or whatever.
Would some Module, like AutoLoad help me?
Thanks,
.
- Follow-Ups:
- Re: Calling functions by name and load modules on demand
- From: Charles DeRykus
- Re: Calling functions by name and load modules on demand
- From: Brian McCauley
- Re: Calling functions by name and load modules on demand
- Prev by Date: Re: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.4 $)
- Next by Date: Re: Bytecode filter
- Previous by thread: Posting Guidelines for comp.lang.perl.misc ($Revision: 1.4 $)
- Next by thread: Re: Calling functions by name and load modules on demand
- Index(es):
Relevant Pages
|