Re: What is the easiest way of having a configuration file for my program?⭐
by Anonymous Monk on Aug 18, 2000 at 16:23 UTC
|
I'd have to say that the "easiest" way would be to just have a perl file that you 'require' into your program. The config would look like
$attr = "value";
$this = 0; | [reply] |
Re: What is the easiest way of having a configuration file for my program?⭐
by fundflow (Chaplain) on Aug 18, 2000 at 18:26 UTC
|
I found the following method to be simple and
readable in terms of configuration file syntax:
TOKEN1=value1
TOKEN2=value2
...
The way to parse it is
while(<>) {
if (/^([A-Z_]+)=(.*)/) {
my ($TAG,$VAL)=($1,$2);
$conf{$1}=$2;
}
}
This lets you have comment lines and can be easily
extended for multi-line values, if needed.
Using __DATA__ section in your script is also useful, as
mentioned by Corion, but you can just initialize
%conf by
%conf=(TOKEN1 => "value1",
TOKEN2 => "value2")
Cheers. | [reply] [d/l] [select] |
Re: What is the easiest way of having a configuration file for my program?⭐
by Corion (Patriarch) on Aug 18, 2000 at 12:00 UTC
|
Personally, I've found the following setup
to be the easiest :
I have a routine, readconfig(), which
takes a string as the parameter (the name of
the configuration file). A template configuration
file, filled with sane defaults, is stored in the
__DATA__ section of my program. Readfile
reads the data either from the file specified,
or, if the name is empty, from the __DATA__
section. I call readconfig() twice,
once to initialize with the sane defaults, and then
again with the name of the config file (if given),
to overwrite the settings differing from the
defaults.
Of course, this answer dosen't help you
with how to organize your configuration data, but
the configuration data depends much on what
your program does (and what data it needs) and what
your audience is. Apache for example uses some kind
of HTML/XML style to store the configuration.
I prefer either .ini style sections
with name=value pairs or simple
name=value pairs.
| [reply] |
Re: What is the easiest way of having a configuration file for my program?⭐
by GhodMode (Pilgrim) on Jul 25, 2002 at 21:44 UTC
|
     I like my configuration file to be written in perl, so I can have common functions which can be used between my programs.
   I start like this...
use lib ("/path/to/file/config");
use PerlConfig; # This is PerlConfig.pm in /path/to/file/config
     Then my config file starts like this...
package Cfg;
     My program can now use variables with $Cfg:: in front of them, or subroutines with &Cfg:: in front of them so that I don't get confused about where things exist.
     For example, I want to have a consistent time/date stamp in all of my output file names. So, I use POSIX 'strftime' in my config file then set $filedt = sub { return strftime("%m%d%H%M%S", localtime) };
     I then use filenames like
my $logfile = "codelog_" . &$Cfg::filedt . ".txt";
GM | [reply] [d/l] [select] |
Re: What is the easiest way of having a configuration file for my program?
by princepawn (Parson) on Aug 18, 2000 at 18:25 UTC
|
I would take a look at App::Config on CPAN by
Andy Wardley (author id: ABW)
| [reply] |
Re: What is the easiest way of having a configuration file for my program?
by ghenry (Vicar) on May 02, 2005 at 08:06 UTC
|
Three modules on CPAN, (one which is already mentioned above, but is now called something else):
Config::General is part of the bigger Config-General module, and AppConfig is the parent of AppConfig::File
They both read configuration files that are in the standard format that you would expect, but give you many, many options.
HTH.
| [reply] |
|
| [reply] |
|
To my beginner eyes, this looks much the same as the two that I mentioned previously.
If I may ask, what sets this apart from them?
Thanks.
Walking the road to enlightenment... I found a penguin and a camel on the way.....
Fancy a yourname@perl.me.uk? Just ask!!!
| [reply] |
|
|
|
Could you post this as a separate answer to the question, along with a brief description (better yet, example) of what differentiates it? Thanks.
| [reply] |
Re: What is the easiest way of having a configuration file for my program?
by epoptai (Curate) on Dec 11, 2000 at 08:30 UTC
|
Here's a way to have multiple config files for a
single script. They're specified with a query such
as myscript.pl?config1, myscript.pl?config2, etc.
$config=$ENV{QUERY_STRING};
unless(require $config){
print "No Configuration File Specified!\n";
exit;
}
I learned that from Brent Michalski's excellent
database tutorials that used to be at
webreview.com
| [reply] [d/l] |