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

I have a collection of separate files which all go into one set of scripts. A lot of them share the same congiguration variables I add to the top of each file, but they need to edit 3 or 4 files with the same variable information. How can I import these variables so they edit let's say a config.txt file? Is this what require "config.txt"; does?

Replies are listed 'Best First'.
Re: Set of scripts using same variables
by rinceWind (Monsignor) on Mar 15, 2004 at 17:19 UTC
    There are several modules on CPAN which will do this. Recommended are Config::Simple and Config::Tiny.

    --
    I'm Not Just Another Perl Hacker

Re: Set of scripts using same variables
by tcf22 (Priest) on Mar 15, 2004 at 17:31 UTC
    If they are configuration vars, then I would go with rinceWind's suggestion of the Config::* modules.

    If you have groups of scripts with that same piece of code, you can just stick it in a common script an require it.

    Script1.pl
    require 'common_stuff.pl'; &common_function;

    common_stuff.pl
    sub common_function(){ #...code goes here... }

    - Tom

Re: Set of scripts using same variables
by matija (Priest) on Mar 15, 2004 at 17:32 UTC
    You can do it that way, as long as config.pl (let's not call it .txt) has valid Perl syntax. And note that it must return a true value - usualy this is done by placing a 1; on the last line of the file.

    Having said that, there are a whole lot of modules on CPAN that deal with reading configuration files, like Config::Tiny, Config::General or even Config::IniFiles if you happen to like that format.

        Ugh, please don't use XML for configuration files (except if you particularly enjoy torturing users (or yourself)).

        Makeshifts last the longest.