Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

What is the easiest way of having a configuration file for my program?

by Anonymous Monk
on Aug 18, 2000 at 11:40 UTC ( [id://28440]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (files)

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?

Originally posted as a Categorized Question.

  • Comment on What is the easiest way of having a configuration file for my program?

Replies are listed 'Best First'.
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;
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.
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
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.

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

        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!!!
        Could you post this as a separate answer to the question, along with a brief description (better yet, example) of what differentiates it? Thanks.
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)
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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://28440]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 21:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found