[Kc] dynamically loading and unloading perl packages

Garrett Goebel garrett at scriptpro.com
Mon Sep 15 10:04:18 CDT 2003

John Reinke wrote:
> I've got a challenge that I just haven't been able to
> find a solution for. I'll describe the feature I need
> to add to the application, in case I'm headed in the
> wrong direction.
> I have a script which runs constantly, watching another 
> application. It has certain tasks it performs, and
> occasionally needs to add and/or subtract from the list
> of tasks it performs through checking to see if a
> config file has changed.
> Here's the tricky part: the tasks are performed by
> custom perl packages that are not known until the
> config file is read. This means that the main program
> needs to be able to dynamically load/initialize or unload
> packages according to changes made to the config file
> since the last time it was read.

Are you sure? I would think giving each package intialize and/or cleanup
routines and a little code organization would be sufficient.


Reloading packages is as simple as:

  do '/path/to/package';

But what's already in memory remains. So you'd have to make sure you have
explicit default value assignments for all variables. And follow the same
type of advice given to apache mod_perl programmers to otherwise make sure
you don't have any package state from a previous request carry over to the
next one.

Removing a package from memory is considerably more difficult... you might

  sub _pkg_clear ($) {
    no strict 'refs';
    my ($package) = shift;
    my $stash = *{$package . '::'}{HASH};
    foreach my $name (keys %$stash) {
      $name = join('::', $package, $name);
  #    print "undef $name\n";
      undef $$name;
      undef @$name;
      undef %$name;
      undef &$name;
      undef *$name;
    undef %{$package . '::'};

But that's more of a shotgun than a recipe for reliable behaviour. It
doesn't do much for detecting and removing circular references, or handling
object destruction ordering issues and class clean-up code. And it'll any
slots in the package namespace typeglobs I've failed to account for: like
IO, FORMAT, etc.

If all the code you're using is stuff you wrote yourself... then perhaps
through a little discipline you might be able to use the above. But the more
complex your perl and reliance on tricky CPAN modules particularly OO-Perl,
the less confident I'd be about taking the shotgun approach.

> I've not been able to figure out how to dynamically
> load or unload packages within a global scope. The
> closest I've been able to find is DynaLoader, which
> allows dynamically loading C libs into a perl program.
> This seems like something that should be easy in perl,
> but I've not been able to figure it out.

If you want some well thought out ideas on building a system that has
various components which must support being initiated, suspended, resumed,
terminated, and reconfigured at runtime... I'd recommend doing a little
reading on the component configurator pattern.


Garrett Goebel
IS Development Specialist

ScriptPro                   Direct: 913.403.5261
5828 Reeds Road               Main: 913.384.1008
Mission, KS 66202              Fax: 913.384.2180
www.scriptpro.com          garrett at scriptpro dot com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.pm.org/pipermail/kc/attachments/20030915/d5b78756/attachment.htm

More information about the kc mailing list