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

The problem:

What I'm doing:

The loop is something like this:

$SOURCE='stuff.src'; $COMP='stuff.pm'; require $COMP; *dostuff=\&Stuff::dostuff; while (<>) { if (-M $SOURCE < -M $COMP) { # source changed? print "compiling...\n"; system "cp $SOURCE $COMP"; # ok, fake compilation delete $INC{$COMP}; # make sure 'require' will load the module no warnings; # avoid some warnings require $COMP; *dostuff=\&Stuff::dostuff; # import } dostuff($_); }

And the "data" file $SOURCE:

package Stuff; no warnings; sub dostuff { print "ver 1: ",shift,"\n"; } 1;

Is this usable? Does it leak memory (in the real case the compiled file would be quite larger)? Can I use something similar inside a mod_perl handler? Is ignoring the "redefined" warnings potentially dangerous?

Is there a better way?

Thanks...

Replies are listed 'Best First'.
Re: Repeated 'require's
by chromatic (Archbishop) on Nov 11, 2002 at 17:24 UTC

    If you store the modification time of $SOURCE in a variable, you can save one filetest per check. Otherwise, it looks like code I've written. (You can also disable just the redefine warning.)

    I don't believe there is a leak, but say that with two caveats. First, in a mod_perl environment, you're going to write to copied-on-write pages. Each child process will have to load the new module, and that will take up previously-shared memory. Second, if there are any code references to functions in the reloaded package, they might not behave as you'd expect. I wouldn't be surprised if they leaked a little, but I'm not quite sure they do.

Re: Repeated 'require's
by Kanji (Parson) on Nov 11, 2002 at 17:38 UTC
    Can I use something similar inside a mod_perl handler?

    If that's your real goal, then might I suggest Apache::StatINC (alt.)?

        --k.