?
Solved

Reload modules

Posted on 2004-04-19
5
Medium Priority
?
863 Views
Last Modified: 2008-03-10
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?
0
Comment
Question by:Zap32
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 3

Expert Comment

by:PerlKing
ID: 10859046
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.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10859122
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
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10859253
Zap32,
Check if you need to delete you module's entry from %INC before doing require again.

Stefan
0
 
LVL 8

Accepted Solution

by:
davorg earned 2000 total points
ID: 10859475
Something like this might work for you:

In Thing.pm

package Thing;
                                                                               
use strict;
use warnings;
                                                                               
use Exporter;
                                                                               
our @ISA = 'Exporter';
our @EXPORT = 'thing';
                                                                               
no warnings 'redefine';
                                                                               
sub thing {
  10;
}
                                                                               
1;

In thing.pl

#!/usr/bin/perl
                                                                               
use strict;
use warnings;
                                                                               
my $time = 0;
                                                                               
while (1) {
  if (!$INC{'Thing.pm'} or $time > -M $INC{'Thing.pm'}) {
    print "Reloading Thing.pm\n";
    delete $INC{'Thing.pm'};
    require Thing;
    Thing->import;
    $time = -M $INC{'Thing.pm'};
  }
                                                                               
  print thing(), "\n";
                                                                               
  sleep 10;
}

Every 10 seconds it checks the timestamp on Thing.pm and if the module has been changed, it reloads it.

One neat trick - you need to delete the entry from %INC in order for Perl to reload the module.

Dave...
0
 

Author Comment

by:Zap32
ID: 10861965
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..  
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question