Sam Holden (
[email protected]) wrote:
: On 19 Oct 2004 21:23:59 -0700, (e-mail address removed) wrote:
: > I want to source (do/require/use) a config file. It must be compatible
: > with warnings and strict.
: >
: > I don't want warnings about a variable being used only once.
: >
: > I don't want to put too much cruft in the config file. A package
: > statement or a my is ok but an exporter is not. There will be many
: > config files.
: That's nice. Do you have a question?
I think he's asking for advice on how best to do this.
There are some nice code examples amongst the perl distrib that use the
constant module to define config constants. grep "use constant" in the
perl directory tree would probably find them.
Otherwise, the following is the stripped down version of one similar
config file I have. If the syntax is wrong it's because I just stripped
it down - the original works. It includes exporter, but if the config
uses the correct packages then exporting is not required. I didn't
quickly find an example without exporter, but I have done this same basic
setup without exporter in the past. The point is to get all globals into
a single @list_of_globals so that all programs can refer to a single value
to define a set of globals, with little redundancy, and allowing warnings
and strict to check (almost) everything.
# the config file
use strict;
######################
package My::Config;
######################
our (@ISA, @EXPORT);
require Exporter;
@ISA = ('Exporter');
@EXPORT = @::MY_GLOBALS;
# ----------------------------
# configuration options
# ----------------------------
BEGIN{ @::MY_GLOBALS =
qw(
$MAX_SIZE
$USER_DOC_ROOT
) } #/BEGIN
use vars @::MY_GLOBALS ; # this checks for typos in the config file
$MAX_SIZE = 100_000;
$USER_DOC_ROOT = '/a/path/to/a/dir/';
1; # true for caller
# example use of the config file
use My::Config;
use vars @::MY_GLOBALS;
There is only a single variable that doesn't get checked - @::MY_GLOBAL