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

I'm writing a modular IRC bot.
I currently have a couple of modules which i can load 'on demand' by sending a command to it; it then require's the module, and fills a hash with some references to subs in the module, and hooks into the main code. cool. it works.
Now i need to _unload_ the code, so i can have a drop-in replacement system (cool for debugging, or upgrading a module).
Is there a way to 'unload' code i require'd, or use'd, or otherwise imported ?
I know i can unset $INC{modulename}, but i still need the actual code replaced by the code in the module file..

Replies are listed 'Best First'.
Re: unloading code
by merlyn (Sage) on Oct 07, 2001 at 19:23 UTC
    If you eval a file (like with require), new subroutine definitions will replace old ones, usually just fine. Coderefs taken to old routines still refer to the old routines, however, and closures for those routines will remain alive.

    If you want something fancier, perhaps you can "delete" the entire package. I've seen code to do that somewhere (perhaps even here).

    -- Randal L. Schwartz, Perl hacker

      Curious... it seems that perl dumps core when undefing packages.

      Foo.pm contains:

      package Foo; require Exporter; @ISA = qw(Exporter); @EXPORT = qw(hello say); @EXPORT_OK = qw(hello say); sub hello { "Hello, world!\n"; } sub say { print @_; }

      If I do this (not including extraneous output):

      $ perl -MFoo -de 42 > x %Foo:: (contents of Foo package) > say hello Hello, world! > undef %Foo:: > x %Foo:: empty array > say hello

      ... abort with SIGSEGV in DB::DB. If I run this outside the debugger, it works fine. Running perl 5.6.1 on i686-pc-linux-gnu.