[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Drop-in modules



I was designing a program that would present the user with the same set of
functions for a set of user services (in this case, an interface for
changing their password for that user service), and wanted it to be trivial
to add or remove such a service; if anything went wrong with one of them,
for instance, I wanted the operations people to know that all they had to
do was delete or rename a file.

So I made a module out of the functionality for each service, creating them
from a template.  The main program loads them in with a loop something like
this:

  opendir DIR, $mod_dir || die "Can't open $mod_dir: $!\n";
  foreach my $mod (map { /(.*)\.pm$/; $1 } grep /\.pm$/, sort readdir DIR)
  {
    my $file = "$mod_dir/$mod.pm";
    eval "require \"$file\"";
    die "Couldn't load $file: $@\n" if $@;
    $mod->import;
    push @Modules, $mod;
    }
  }             
  closedir DIR;

In the actual code there's a lot more going on in the loop as it calls
initialization functions in each module, but you get the idea.

I know there's not much to it, but it made for really clean code; parts of
the interface that present an HTML table with one row per user service
later on are straightforward map statements on @Modules.

Peter Scott



==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org