Calling functions by name and load modules on demand



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,

.



Relevant Pages

  • Re: Calling functions by name and load modules on demand
    ... groups (e.g. web, psoix) the object belongs to in a DB, and finally ... autoload the modules itself or pre-compile the modules or whatever. ... Would some Module, like AutoLoad help me? ...
    (comp.lang.perl.misc)
  • Re: Calling functions by name and load modules on demand
    ... 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: ... I don't really see why you want an AUTOLOAD mechanism for this. ...
    (comp.lang.perl.misc)