£okì has asked for the wisdom of the Perl Monks concerning the following question:

I've split my code into included modules I'm including using "require" ( I can change this ). However, I'd like to be able to rebuild some subroutines without restarting the script. My idea was to undefine the subroutines in the modules (.pl files with simply subroutines in them) and then re-require the module. It appears I can undefine the subs by using
foreach my $sub (@subs) { undef(&{$sub}); } require "%s/modules/%s.pl", $base{dir}, $module;
Where @subs is an array of subroutines in that .pl file However at this point, all it does is undefine that subroutine, the require does not seem to help. I know I'm way off track here. Please point me in the correct direction again.

Replies are listed 'Best First'.
Re: reloading of module
by tinita (Parson) on Dec 30, 2004 at 21:25 UTC
    supposed your module has a package My::Module you can do
      delete $INC{"My/Module.pm"};
    then requiring again it will do the right thing.
    see perldoc -f require

    update: actually, i should read the docs, too. the filename is stored in %INC, so if you have module.pl, you do delete $INC{"module.pl"}

      Hmm, I suppose I could build them as modules. . Not how they are set up right now but it wouldn't take much. Thanks.
        or you might be able to just do "module.pl";
        see perldoc -f do
Re: reloading of module
by revdiablo (Prior) on Dec 30, 2004 at 21:26 UTC
    My idea was to undefine the subroutines in the modules ... and then re-require the module.

    This is a fine idea, but if you look in perldoc -f require, you'll see that require keeps track of what it has loaded already, and will not load something it already has.

    You can get around this behavior by deleting the entry in %INC for that filename. For example:

    # in Foo.pm print "foo\n"; # in test.pl require "Foo.pm"; delete $INC{"Foo.pm"}; require "Foo.pm";
Re: reloading of module
by borisz (Canon) on Dec 30, 2004 at 22:16 UTC
      Ok, I'm loading it up as a module now and I've done both, deleting the $INC and reloading it and using module::refresh. However, I keep getting it giving me:

      "Subroutine on_public redefined at XXXX"

      This is the same error as before. Do I need to scrap or reload the subroutines as well? If so, how?
        Try this, but do _not_ delete $INC{xx} if you use this module.
        # somewhere at the top my $refresher = Module::Refresher->new; .... { no warnings 'redefine'; $refresher->refresh_module('module_name'); }
        Boris