Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Reloading Modules

by rendler (Pilgrim)
on Feb 03, 2002 at 05:56 UTC ( [id://143029]=perlquestion: print w/replies, xml ) Need Help??

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

I would like to reload my modules from a script, I've attempted this:
sub reload_modules { for (keys %INC) { if (/^QStat/) { delete $INC{$_}; require $_; } }
From the main script itself but that didn't seem to do much good (I'm calling the sub from another module btw). Then I tried adding the sub to all of my custom modules that the whole script is using and called them all from that first module. Again that didn't work, I've also tried leaving out require $_;.

Replies are listed 'Best First'.
Re: Reloading Modules
by rob_au (Abbot) on Feb 03, 2002 at 06:11 UTC
    I think the more pressing question is - Why are you needing to reload your modules in this fashion?

    Depending on your requirement for this functionality, you could use eval and call your module at run-time only with the scope of this function. eg.

    my $func = eval { use MyFunction; return MyFunction->method(); };

    The problem with this approach however, is that your module usage will only have a limited scope. Although, depending upon your script design, this could also offer some advantages.

     

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

      It's an IRC bot and each of the modules extend it's functionality in some way, so it becomes a pain having to restart the thing just to test minor changes.
      You don't want to use eval(BLOCK) though. The block is compiled at the surrounding's compile-time. You want eval(EXPR).

      Compare this:
      #!/usr/bin/perl -l print 'pre'; eval { BEGIN { print 'compiling eval block' } }; print 'post';
      with this:
      #!/usr/bin/perl -l print 'pre'; eval q{ BEGIN { print 'compiling eval expr' } }; print 'post';
Re: Reloading Modules
by chromatic (Archbishop) on Feb 03, 2002 at 06:04 UTC
    Define "didn't work". I created a module called 'QStat.pm', consisting of:
    package QStat; BEGIN { print "Compiling QStat!\n"; } sub foo {} 1;
    I then turned your subroutine into a standalone script, and added a line to use QStat at the beginning. Under Perl 5.6.1 and 5.7.almost3 I receive:
    Compiling QStat! Compiling QStat! Subroutine foo redefined at QStat.pm line 7.
    Exactly what I expect. Perhaps you need to print the value of $_ within your loop. Your regexp may not be matching.
      Yes it does that for me too but when I change something in one of the modules and then make it reload the changes in whatever sub I've changed don't seem to show through.
        This is interesting!

        If you're importing subs, you must also replace them in the importing package. It's best to call import() on the reloaded package specifically. Observe:

        use QStat; foo(); print "QStat foo is: ", \&QStat::foo, "\n"; print "Imported foo is: ", \&main::foo, "\n"; <STDIN>; my $loc = $INC{QStat}; delete $INC{QStat}; require $loc; # slightly more difficult # QStat->import(); print "After import, foo is: ", \&main::foo, "\n"; print "And in the package it is: ", \&QStat::foo, "\n";
        Try it with and without the import(), and you'll see what's going on. *foo in the main package and *foo in QStat will point to different CVs, and that's the problem. (The STDIN part is in there so I could change what foo() does while the program is running.)

        I wonder if it would be worth writing a 'reimport' module...

Re: Reloading Modules
by IlyaM (Parson) on Feb 03, 2002 at 08:30 UTC
    I don't know if it will work "out of box" for non mod_perl code but at least you can "steal" code from Apache::StatINC or Apache::Reload. These two modules reload other modules when they change on the disk.

    --
    Ilya Martynov (http://martynov.org/)

Re: Reloading Modules
by maverick (Curate) on Feb 03, 2002 at 18:32 UTC
    This technique works for me as well. I'm actually using it in a mod_perl app server to reload user modules upon change. I snagged the code right out of Apache::StatINC. If you look at Apache::Symbol, it makes special provisions for undefining exported functions and this may be why you aren't seeing the changes.

    /\/\averick
    perl -l -e "eval pack('h*','072796e6470272f2c5f2c5166756279636b672');"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://143029]
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: (6)
As of 2024-04-24 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found