Solved

Reload modules

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 69
delete query using perl dbi 3 92
Expect & sshpass package for Solaris 10 x86 15 857
PERL - Find newest folder 12 102
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

13 Experts available now in Live!

Get 1:1 Help Now