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

Once I have loaded a file using require, is there a clean way to reload the file?

I am running analyses on several 'families' of text files (Hazard reports, requirements documents, FMEAs.)

A Perl program analyzes the text-files. The program has many options, which I store in a HoH called %runSwitches. Each text-file needs a slightly different set of options.

Each document family has a default set of options, which must be tweaked for the particular text file.

Here's how I set it up: Each document has a short configuration file – documentName.pl. Each family has a file with a set of defaults – family_defaults.pl. The short configuration file loads the defaults, then asserts the tweeks:

require "family_default.pl"; # default values into %runSwitch +es %runSwitches = (%runSwitches, # tweeks for a single document switch1 => 'tweek1', switch2 => 'tweek2');
This works fine. Except now I have a script that iterates though all the documents in a family. The first doc called loads the defaults and runs fine. The second file executes require "family_default.pl"; and does nothing, because family_default.pl is already in %INC. So the tweaks from the first file are still in effect, and I lose. I could Both of these seem like awkward hacks. Is there a better way to do this?

Replies are listed 'Best First'.
Re: Right way to force the reloading of a file
by ikegami (Patriarch) on Sep 14, 2009 at 20:46 UTC

    Once I have loaded a file using require, is there a clean way to reload the file?

    Reloading the file is easy. It's unloading the effects of the previous version that relies on a lot of assumptions and limitations.

    But your question is wrong. The problem is your use of require to load something other than a module. You want do.

    That said, needing do is a sure sign of a bad design. This is no exception.

Re: Right way to force the reloading of a file
by moritz (Cardinal) on Sep 14, 2009 at 20:43 UTC

    The clean way to solve your problem is to extract all the "interesting" code into a subroutine that takes configuration data as parameter(s), and put that in a module and export it.

    The existing script can then call this subroutine, as well as your recursive script.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Right way to force the reloading of a file
by gloryhack (Deacon) on Sep 16, 2009 at 04:06 UTC
    Rather than trying to outsmart Perl, outsmart the problem you're trying to solve. Were this mine to do, I'd start by having a good look at Config::Any for inspiration.