Contributed by Anonymous Monk
on Aug 18, 2000 at 11:40 UTC
Q&A
> files
Description: I have a program that requires some things set straight. Instead
of setting them in the script itself, I would like to move them
to the configuration file. What is the easiest/best way to read
and set them from the config file on program start up? Answer: What is the easiest way of having a configuration file for my program? contributed by Anonymous Monk 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; | Answer: What is the easiest way of having a configuration file for my program? contributed by fundflow 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. | Answer: What is the easiest way of having a configuration file for my program? contributed by Corion 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. | Answer: What is the easiest way of having a configuration file for my program? contributed by GhodMode      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 | Answer: What is the easiest way of having a configuration file for my program? contributed by princepawn I would take a look at App::Config on CPAN by
Andy Wardley (author id: ABW)
| Answer: What is the easiest way of having a configuration file for my program? contributed by ghenry 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. | Answer: What is the easiest way of having a configuration file for my program? contributed by epoptai 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
|
Please (register and) log in if you wish to add an answer
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|