Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Forcing a PERL script to reload libraries

by Anonymous Monk
on Dec 20, 2000 at 20:13 UTC ( [id://47598]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a very complex PERL script that I use for my radio station (plug away: http://ourthing.radio.binary9.net). Everytime I make a modification to my main "playdaemon" script, I have to kill it and restart it; which causes a hiccup in the stream (and restarts the current song).

What I would like to do is something like this (non-functioning):

playdaemon:

#!/usr/bin/perl

require "MyRoutines.pl";

$SIG{HUP}=sub { require "MyRoutines.pl"; };

while (1) {
   DoIt();
}
__END__

Then, as I change the contents of MyRoutines.pl, I could send a SIGHUP to my program and it would reload the contents of MyRoutines.pl to reflect the changes.

Is such a thing possible? I believe my problem is that PERL already knows it has loaded the MyRoutines.pl library and won't do so again. How many I accomplish this?

TIA!

  • Comment on Forcing a PERL script to reload libraries

Replies are listed 'Best First'.
Re: Forcing a PERL script to reload libraries
by merlyn (Sage) on Dec 20, 2000 at 20:14 UTC
    Poke through %INC, which is Perl's memory of what's already been required. It'll be obvious what you need to delete from there before calling require again.

    -- Randal L. Schwartz, Perl hacker

      Yay!! A simple
      
      $SIG{HUP}=sub {
          delete $INC{"MyRoutines.pl"};
          require "MyRoutines.pl";
      }
      
      
      did the trick! Thank you! I didn't realize you could just modify that hash.

      Btw, I'm the same poster as the original question, I just decided to create an account :)

Re: Forcing a PERL script to reload libraries
by KM (Priest) on Dec 20, 2000 at 20:24 UTC
    You could try:

    $SIG{HUP} = sub { delete $INC{'MyRoutines.pl'}; require "MyRoutines.pl +";};

    Perl will look in %INC to see what has been 'used'. When it sees something is already in there, it will not re-use/require it. By deleting it, you can force it to re-require.

    Cheers,
    KM

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://47598]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 02:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found