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

Discussion in 'Perl Misc' started by andrew.fabbro@gmail.com, Jan 31, 2006.

  1. Guest

    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
     
    , Jan 31, 2006
    #1
    1. Advertising

  2. Paul Lalli Guest

    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
     
    Paul Lalli, Jan 31, 2006
    #2
    1. Advertising

  3. DJ Stunks Guest

    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
     
    DJ Stunks, Jan 31, 2006
    #3
  4. Uri Guttman Guest

    >>>>> "PL" == Paul Lalli <> writes:

    PL> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 31, 2006
    #4
  5. Uri Guttman Guest

    >>>>> "DS" == DJ Stunks <> writes:

    DS> 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 ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 31, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MSFT
    Replies:
    0
    Views:
    416
  2. mike

    Check if DB table has changed

    mike, May 13, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    538
    Steve C. Orr [MVP, MCSD]
    May 13, 2004
  3. Replies:
    1
    Views:
    693
    Rosanne
    Oct 11, 2005
  4. Replies:
    0
    Views:
    170
  5. mxbrunet
    Replies:
    1
    Views:
    219
Loading...

Share This Page