source a config file

C

carloschoenberg

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.
 
A

A. Sinan Unur

(e-mail address removed) wrote in
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.

It is always nice to know what you want and what you don't want.

Did you have a Perl question?

Sinan.
 
S

Sam Holden

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?
 
M

Malcolm Dew-Jones

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
 
C

carloschoenberg

A. Sinan Unur said:
(e-mail address removed) wrote in


It is always nice to know what you want and what you don't want.

Did you have a Perl question?

I want to do what I want to do (described above), in Perl. I want to
do this without doing what I don't want to do (described above).

Here's one way I tried to do it, based on a random suggestion found on
the net:

$ cat a
$x="hi";

$ cat b
use strict;
use warnings;

{ package MyConfig; do './a'; }

print "$MyConfig::x\n";

But this happens:
$ perl b
Name "MyConfig::x" used only once: possible typo at b line 6.
hi
 
A

A. Sinan Unur

(e-mail address removed) wrote in
....

Here's one way I tried to do it, based on a random suggestion found on
the net:

$ cat a
$x="hi";

$ cat b
use strict;
use warnings;

{ package MyConfig; do './a'; }

print "$MyConfig::x\n";

But this happens:
$ perl b
Name "MyConfig::x" used only once: possible typo at b line 6.
hi

If all you want is to be able to refer to variables etc in the MyConfig
namespace, you why not just use a proper module (with no exporter):

D:\Home> cat MyConfig.pm
package MyConfig;
our $x = 'hi';
1;
__END__

D:\Home> cat c.pl
#! perl;

use strict;
use warnings;

use MyConfig;

print $MyConfig::x;
__END__

D:\Home> perl c.pl
hi


Sinan.
 
C

carloschoenberg

A. Sinan Unur said:
If all you want is to be able to refer to variables etc in the MyConfig
namespace, you why not just use a proper module (with no exporter):

Is there any trickery that will make this work if I don't know the
name of the config file at compile time? For example if it's being
given as a command-line argument.
 
P

Paul Lalli

"A. Sinan Unur" <[email protected]> wrote in message

Is there any trickery that will make this work if I don't know the
name of the config file at compile time? For example if it's being
given as a command-line argument.

Without using Exporter, this is the best I could come up with:

package Mytest;
use strict;
use warnings;

our $foo = "Hello World";

1;

-----------------------------

#!/usr/bin/perl
use strict;
use warnings;

my $package = shift or die "Usage: $0 <Package>\n";
require $package . ".pm";

no strict 'refs';
my $bar = ${${$package.'::'}{foo}};
print "Bar: $bar\n";
__END__

Basically, we access the symbol table by creating the name of the table
( "Mytest::"), using that string as a symref, dereferencing it and
obtaining the 'foo' entry, and then grabbing the scalar value out of
that typeglob.

Nasty, nasty stuff.

All things considered, I'd use Exporter.

Paul Lalli
 
B

Brian McCauley

Here's one way I tried to do it, based on a random suggestion found on
the net:

$ cat a
$x="hi";

$ cat b
use strict;
use warnings;

{ package MyConfig; do './a'; }

print "$MyConfig::x\n";

But this happens:
$ perl b
Name "MyConfig::x" used only once: possible typo at b line 6.
hi

You can simply 'no warnings qw(once)' [1] or make sure you mention all
your config parameters at least twice (adding a dummy mention if necessary).

[1] This warning not really all that valuable when you've got 'use
strict' anyhow.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,430
Latest member
7dog123

Latest Threads

Top