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

With Perl being interpreted; is there any way to replace module X, with an updated module X1, in a running Perl process?

In a former life, I learned C++ from James Coplien's "Advanced C++" book, and a training course he gave. One technique he presented was how AT&T was updating software in their switches "on-the-fly". The running process would do a dynamic load, and switch pointers in a v-table to start using the new code, on the next object instantiation; without bringing down the process.

Obviously, this isn't for a linear start-to-stop program. But for long-running daemons, that do , say, network inspection; and a new (example only) BGP decoder is ready to be used. Can that be inserted into an existing monitor without disrupting state or the on-going monitor process.

I am familiar with the "save state, shutdown, patch, restart, restore, run" methodology; But each of those steps is a coding/testing/documenting effort, and it can't be done in the time between 2 packets on the wire(for example).

Thoughts...

  • Comment on On-the-fly module replacements : On topic - really

Replies are listed 'Best First'.
Re: On-the-fly module replacements : On topic - really
by moritz (Cardinal) on Nov 19, 2008 at 17:57 UTC
    Re-loading the code is easy: with delete $INC{Your/Module.pm}; require Your::Module you can achieve that.

    But that doesn't preserve any lexical or package variable from disappearing in the mean time. If there were no important informations, you can just re-initialize the module. Otherwise you might want to try PadWalker to recover the lexical variables. But I don't know if that can also set variables in foreign scopes, and I don't know how well it plays with closures.

    Of course that doesn't tell you how to handle the down time in between, and frankly I have no idea about it. Hopefully others can help you there.

      There's no down time in between, at least with processes, since they all have their own %INC. Not sure if all threads have their own or not.
Re: On-the-fly module replacements : On topic - really
by JavaFan (Canon) on Nov 19, 2008 at 18:02 UTC
    It depends.

    It's not too hard to force recompiling a module, do will do, and so will require and use (in an eval, after deleting it from %INC). It will replace the subs with new subs.

    However, if the module also initializes variables (package variables, or lexicals with a file wide scope), those variables will reset (and note that a simple 'my $var;' is considered an initialization). Depending on the module, this may break your program.

    So, the answer is 'yes', but you want to be careful how you code your module.

Re: On-the-fly module replacements : On topic - really
by Perlbotics (Archbishop) on Nov 19, 2008 at 20:41 UTC