Link to home
Start Free TrialLog in
Avatar of Zap32
Zap32

asked on

Reload modules

I've got a perl program that runs 24/7, that use subs located in another file. 'require file.sub'.
I wish to be able to change the file.sub, and then make the main program to use the changes, without restarting it.
Is this possible, and how?
Avatar of PerlKing
PerlKing

I am not very sure if this works because I didn't try it.

My idea is to have a signal handler that reloads the module. And whenever you change the file, you just have to send the singal to the program.

Add this to your perl program:
$SIG{USR1} = { sub do "require file.sub"; };

And whenever you change file.sub, just do this at the shell prompt:
kill -USR1 <pid>
where <pid> is the process id of your perl program.

Your program isn't getting restarted but it would just do what you want, it reloads the file.sub

I hope that helps.
Hi PerlKing,
> $SIG{USR1} = { sub do "require file.sub"; };
Might be problematic. I'm not sure what happens if your prog is within your module when you want to replace it.

Cheers,
Stefan
Zap32,
Check if you need to delete you module's entry from %INC before doing require again.

Stefan
ASKER CERTIFIED SOLUTION
Avatar of Dave Cross
Dave Cross
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zap32

ASKER

Thanks for all the quick responses.

First I just defined some subs in an other file then the main program, and had the main thing just require the subs. When i tried to use the module called Module::Reload, it said it hade reloaded the file, but when i tried to run the function i had changed, it printed the same ting.
But when I made a module out of the sub, as in your example Dave, it worked :D
I tried to have the 'Thing->import' but that wouldn't work, (looks like it runs the sub it imports ? because it exited the program and told that it couldn't run that function on the object that the sub use.. complicated story... :) ) But when i removed it, it worked..