castaway has asked for the wisdom of the Perl Monks concerning the following question:

Hi people,
I'm writing a program in which users/developers can write their own 'plugins' as perl modules, and load them at runtime, now I'm trying to figure out how to make it possible to unload/reload changed modules without restarting the program.

After some playing around I came up with the following:
Load module using:

$modname = "Blah::Module"; eval "require $modname";
Reload module using:
delete $INC{'Blah/Module.pm'}; eval "no warnings; require $modname";
(To ignore the 'Subrouting redefined' warnings)

Anyone have any comments or better ideas?
Casty

Replies are listed 'Best First'.
Re: Reloading changed modules
by larsen (Parson) on Dec 21, 2002 at 17:19 UTC
    Apache::Reload module seems to apply the same technique. Here a snippet:
    if ($mtime > $Stat{$file}) { delete $INC{$key}; # warn "Reloading $key\n"; if (my $symref = $UndefFields{$key}) { # warn "undeffing fields\n"; no strict 'refs'; undef %{$symref}; } require $key; warn("Apache::Reload: process $$ reloading $key\n") if $DEBUG; } $Stat{$file} = $mtime;
    The same technique, except that it copes with your "${package}::FIELDS" (which $symref in the snippet above is a reference to).

    For further info about %FIELDS, you can check the fields pragma man page (but check 5.8.0 perldelta about the deprecation of 5.6.1 pseudohashes' "user visible" implementation).

    Update: Clarified final note.

      Thanks, I'll take a look.
      I should have tested better before, it seems my eval "no warnings; require $modname"; does not suppress the 'Subroutine redefined' warnings, any idea why?
      Casty
        The module doesn't have 'use warnings;' in it does it? I've used the exact same technique to reload modules except for backwards compatibility I used 'local($^W) = 0;' to supress 'Subroutine redefined'.
Re: Reloading changed modules
by gjb (Vicar) on Dec 21, 2002 at 23:29 UTC

    You may want to have a look at Term::ShellKit, it allows to load/reload modules on the fly and to evaluate Perl expressions.

    Hope this helps, -gjb-