Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force unload a Perl module?

Tags:

module

perl

Hi I am using a perl script written by another person who is no longer in the company. If I run the script as a stand alone, then the output are as expected. But when I call the script from another code repeatedly, the output is wrong except for the first time.

I suspect some variables are not initialised properly. When it is called standalone, each time it exits and all the variable values are initialised to defaults. But when called from another perl script, the modules and the variable values are probably carried over to the next call of the script.
Is there any way to flush out the called script from memory before I call it next time?

I tried enabling warning and it was throwing up 1000s of lines of warning...!

EDIT: How I am calling the other script:

The code looks like this:

do "processing.pl";
...
...
...

process(params); #A function in processing.pl
...
...
...
like image 727
Manoj Avatar asked Aug 03 '09 12:08

Manoj


People also ask

Where are Perl modules stored on Windows?

My Perl modules are in ~/perl/install, for example. Well, in my situation, user files all in NFS path, and locate just exclude NFS. Since the PERL5LIB is a bit long on that system, find is not a good way. For Windows, might need to use double-quotes on the outside, single quotes on the inside.

Where does Perl look for modules?

Whatever the case, Perl gives you options to manage where it looks for modules. Suppose you have a program in your ~/work directory that uses a module named Site::User . By default, Perl will search all of the directories in the special @INC variable for a file named Site/User.pm.

How do I use Perl modules?

A module can be loaded by calling the use function. #!/usr/bin/perl use Foo; bar( "a" ); blat( "b" ); Notice that we didn't have to fully qualify the package's function names. The use function will export a list of symbols from a module given a few added statements inside a module.


3 Answers

If you want to force the module to be reloaded, delete its entry from %INC and then reload it.

For example:

sub reload_module { 
    delete $INC{'Your/Silly/Module.pm'};
    require Your::Silly::Module;
    Your::Silly::Module->import;
}

Note that if this module relies on globals in other modules being set, those may need to be reloaded as well. There's no easy way to know without taking a peak at the code.

like image 84
friedo Avatar answered Sep 18 '22 17:09

friedo


Hi I am using a perl script written by another person who is no longer in the company. I tried enabling warning and it was throwing up 1000s of lines of warning...!

There's your problem right there. The script was not written properly, and should be rewritten.

Ask yourself this question: if it has 1000s of warnings when you enable strict checking, how can you be sure that it is doing the right thing? How can you be sure that it is not clobbering files, trashing data sets, making a mess of your filesystem? Chances are it is doing all of these things, either deliberately or accidentally.

I wouldn't trust running an error-filled script written by someone no longer with the company. I'd rewrite it and be sure that it was doing what I needed it to do.

like image 28
Ether Avatar answered Sep 21 '22 17:09

Ether


Unloading a module is a more difficult task than simply removing the %INC entry of the module. Take a look at Class::Unload from CPAN.

like image 40
tsee Avatar answered Sep 21 '22 17:09

tsee