Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   How to re-"use Module" if the Module has changed? (http://www.velocityreviews.com/forums/t896372-how-to-re-use-module-if-the-module-has-changed.html)

andrew.fabbro@gmail.com 01-31-2006 07:09 PM

How to re-"use Module" if the Module has changed?
 
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 01-31-2006 07:20 PM

Re: How to re-"use Module" if the Module has changed?
 
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 01-31-2006 07:25 PM

Re: How to re-"use Module" if the Module has changed?
 
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 01-31-2006 07:26 PM

Re: How to re-"use Module" if the Module has changed?
 
>>>>> "PL" == Paul Lalli <mritty@gmail.com> writes:

PL> 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.


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 01-31-2006 08:52 PM

Re: How to re-"use Module" if the Module has changed?
 
>>>>> "DS" == DJ Stunks <DJStunks@gmail.com> writes:

DS> 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:


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


All times are GMT. The time now is 09:08 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.