Home > Archive > PERL Miscellaneous > January 2006 > How to re-"use Module" if the Module has changed?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How to re-"use Module" if the Module has changed?
|
|
| andrew.fabbro@gmail.com 2006-01-31, 7:00 pm |
| 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
| |
| Paul Lalli 2006-01-31, 7:00 pm |
| andrew.fabbro@gmail.com wrote:
> 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
| |
| DJ Stunks 2006-01-31, 7:00 pm |
| andrew.fabbro@gmail.com wrote:
> 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
| |
| Uri Guttman 2006-01-31, 7:00 pm |
| >>>>> "PL" == Paul Lalli <mritty@gmail.com> writes:
PL> andrew.fabbro@gmail.com wrote:[color=darkred]
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
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
| |
| Uri Guttman 2006-01-31, 7:00 pm |
| >>>>> "DS" == DJ Stunks <DJStunks@gmail.com> writes:
DS> andrew.fabbro@gmail.com wrote:[color=darkred]
[color=darkred]
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
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
|
|
|
|
|