Solved

Reload modules

Posted on 2004-04-19
5
857 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
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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now