Yes. Specifically it is possible to reload a module. The trick is to update %INC so that a subsequent call to require reloads the module. Consider:
# reload.pl
use strict;
use warnings;
for (1 .. 2) {
delete $INC{'ReloadModule.pm'};
require ReloadModule;
for (1 .. 3) {
print ReloadModule::nextCount(), " ";
}
print "\n";
}
# ReloadModule.pm
use strict;
use warnings;
no warnings 'redefine';
package ReloadModule;
my $count = 0;
sub nextCount {
return ++$count;
}
1;
Prints:
1 2 3
1 2 3
Note the use of no warnings 'redefine'; in the module to be reloaded to suppress "Subroutine nextCount redefined at ..." warnings.
True laziness is hard work
|