Hey Tim,

Wednesday, June 27, 2001, 3:16:45 PM, you wrote:

TM> Hey Jos,

TM> Not sure I understand entirely, but I think I usually do #2 also.

TM> For example, I tend to do something like this in my code.  Comments
TM> would be appreciated.

TM> Is this what you are talking about?

ok, the previous bit did not work as I thought, but this does.

#!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

>>>
>>>   I need to pass a number of parms to my program.  Is there a "best
>>>   Practice" for how to do this?  Command line is not something I want
>>>   to do.
>>>
>>>   I did search on "Config" from search.cpan.org, but it returned 99
>>>   modules!
>>>
>>>   Any suggestions?  In the past, I have done it by hand, but...




-- 
[EMAIL PROTECTED]
Using The Bat! eMail v1.53d
Windows NT 5.0.2195 (Service Pack 1)
Keep honking while I reload. 

Reply via email to