Well, let me just give you a brief example of the 3 ways i was referring to.

1.    script that holds global vars, let's call it config.pl

### config.pl ###
$usr = 'foo';
$pwd = 'bar;
$host = 'bleh.com';

### in your script you'd say: ###
require 'config.pl'
use vars qw($usr $pwd $host); #these 3 vars are 'used' by your config file,
thus poluting your namespace that way

2.    script that just uses one hashref to hold all the vars you need, let's
call it config.pl too

### config.pl ###
$href = {
    usr    => 'foo',
    pwd  => 'bar',
    host  => 'bleh.com',
};

### in your script you'd say: ###
require 'config.pl'
use vars qw($href);    #now it's just one variable that's being used, still
holding all your config data

3.    a package and a constructor, exporting stuff, let's call it Foo.pm (a
module)

### config.pm ###
package Foo;

sub new {
     my $self = shift;
     my $class = ref($self) || $self; #yes, i know randal disagrees on this,
but i'll use the example anyway

     $self = {
         usr     => 'foo',
         pwd   => 'bar',
         host   => 'bleh.com',
     };

     bless ($self, $class);
     return $self;
}

### in your script you'd say: ###
use Foo;    #will work if the file is in a dir which is in your @INC array,
else use the 'use lib' pragma (perldoc lib)
my $object = new Foo;    #by far the cleanest way, but also a bit slower and
more of a hassle imo

you can then retrieve values as follows:
my $usr = $object->{usr} #for example

you could even add closure to all this and require methods to access your
data but that's a bit beyond the scope of this reply... roughly, these are
the 3 ways you can use (within perl)... of course you can also use a plain
text file and interpret it, but in my opinion that's more hassle then it's
worth
and like i said, i usually go with 2. when i want a 'quick and fast' way...
3. if i'm writing a module of course...

hope this clears things up,

Jos Boumans

<snip>
> #!perl -w
> use strict;
> use diagnostics;
> my (%Cfg);
>
> my @FN = split (/\./, $0);
> if (!-e "$FN[0].cfg") { # Create a cfg file (if it's not there...)
>     print qq[\n\tThe config file does not exist, creating $FN[0].cfg.
>     \tPlease check that the settings are what you need.\n\n];
>     CreateCFG();
> } else { # read the cfg file
>     open(CONFIG, "<$FN[0].cfg") || die "Can't open $FN[0].cfg : $!";
>     while (<CONFIG>) { # process the Config file
>         chomp; $_ =~ tr/A-Z/a-z/; s/#.*//; s/^\s+//; s/\s+$//; # clean the
lines
>         next unless length; # anything left?
>         my ($Key, $Val) = split(/\s*=\s*/, $_, 2);
>         $Cfg{$Key} = "$Val\n"; # populate the hash
>         chomp $Cfg{$Key};
>     } # end while CONFIG
>      #%Cfg; # no caps
> } # end if/else create/use .cfg
>
> sub CreateCFG {
>     open(CfgOUT, ">$FN[0].cfg") || die "Can't create $FN[0].cfg : $!";
>     print CfgOUT <<endCONFIG; # Create config file
> # Case and white space do not matter, use "#" for comments
> Parm1 = Str  # Comment
> Parm2 = String
> Parm3 = Another String # Doc on this parm
> Parm4 = String
> endCONFIG
> }
>
> foreach my $Key (sort keys %Cfg) {
>     print "$Key = $Cfg{$Key}\n";
> }
>
> TM> Not sure how #1 would work, and I don't know if I want to get into
> TM> attempting to make a module #3 yet (this code I am working on may get
put
> TM> in a module though.  It is fairly broad, and I couldn't find anything
> TM> that did it on cpan.)
>
> TM> Wednesday, June 27, 2001, 12:58:37 PM, you wrote:
>
> JIB>> I usually use a config file for this now you could do this 3 ways
> JIB>> as i see it
>
> JIB>> -    just have a file that holds a lot of global vars and
'require/use' that
> JIB>> in your mian script
> JIB>> -    get one hashref that you globalise and thus reduce namespace
pollution
> JIB>> -    make it a module, give it a constructor returning a hashref and
thus
> JIB>> not polluting *anything*
>
> JIB>> i usually use #2 for what it's worth

Reply via email to