How to re-"use Module" if the Module has changed?

A

andrew.fabbro

I have a script which runs as a daemon. Periodically, the user wants
to change some of the config variables for it. Right now, those config
variables are stored in a perl module, and the daemon does a 'use
Module' to import them at run-time.

What I'd like to do is have the module reread its config if it gets a
HUP signal. But how can I say "go and 'use Module' again"? It appears
that if you 'use Module' more than once, subsequent calls are ignored.

For example:

use Module;
print "Value is " . $Module::some_var . "\n";
$junk = <STDIN>; # go and change the value in Module, then hit enter
use Module; # try to re-use and get the new value
print "Value is " . $Module::some_var . "\n";

....and the lines printed are the same (the value of $Module::some_var
does not change in the program).

Ideas? Thanks.

-Drew
 
P

Paul Lalli

I have a script which runs as a daemon. Periodically, the user wants
to change some of the config variables for it. Right now, those config
variables are stored in a perl module, and the daemon does a 'use
Module' to import them at run-time.

What I'd like to do is have the module reread its config if it gets a
HUP signal. But how can I say "go and 'use Module' again"? It appears
that if you 'use Module' more than once, subsequent calls are ignored.

Completely untested, based soley upon the docs in
perldoc -f require

$SIG{HUP} = sub {
delete $INC{'Module.pm'};
require Module;
Module->import();
};

Hope this at least gets you pointed in the right direction....

Paul Lalli
 
D

DJ Stunks

What I'd like to do is have the module reread its config if it gets a
HUP signal. But how can I say "go and 'use Module' again"? It appears
that if you 'use Module' more than once, subsequent calls are ignored.
From the Perl Cookbook:

17.16. Restarting a Server on Demand

Problem
You want your server to shutdown and restart when it receives
a HUP signal, just like inetd or httpd .

Solution
Catch the SIGHUP signal, and re-execute your program:

$SELF = "/usr/local/libexec/myd"; # which program I am
@ARGS = qw(-l /var/log/myd -d); # program arguments

$SIG{HUP} = \&phoenix;

sub phoenix {
# close all your connections, kill your children, and
# generally prepare to be reincarnated with dignity.
exec($SELF, @ARGS) or die "Couldn't restart: $!\n";
}

-jp
 
U

Uri Guttman

PL> Completely untested, based soley upon the docs in
PL> perldoc -f require

PL> $SIG{HUP} = sub {
PL> delete $INC{'Module.pm'};
PL> require Module;
Module-> import();

that import is useless as it is generally meant to modify the symbol
table at compile time and this is late in run time.

and you could just slurp in the module (if you know where it is) and
eval it. also i would isolate the data into its own module as there is
no reason (unless the code will change too) to reload the code each
time. and those variables have to be package globals. this leads to
other issues.

i would not even go in this direction and instead would use a config
(not perl code) file (many on cpan) and just reload that into your data
(preferably a single hash for isolation).

uri
 
U

Uri Guttman

DS> 17.16. Restarting a Server on Demand

he doesn't want to restart the whole server but to just reload its
config params. so that cookbook entry is overkill. it could also cause
trouble by break any live socket connections.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top