I'll second the vote for Config::General -- I've not tried too many others, but I found it quick and easy to get working (i.e. about two minutes). It's designed to match the apache config format. Options are name/value pairs (equals optional). Also supports blocks (become hashes), named blocks (hashes within hashes), and multiple identical options (arrays), long lines separated with \, here documents, include files, and numerous options to control how these fancy configuration settings are handled (including a default hash). As I said, though, the basic operation is quick and painless.

Another nice feature is that Config::General has both save_file() and save_string() methods if for some reason you'd like to get the text of the config file and handle file operations yourself (or print to command line for redirection).

Stealing a page (rather, function) from Config::Auto, I use a routine to search for a config file with a bunch of standard names in a bunch of standard places so it doesn't even have to be entered on the command line or hard-coded. So the following snippet of code is just part of my standard script template now.

# command line options, including config already gotten from Getopt::L +ong and stored in %opt # procedural approach -- Config::General also has an OO approach $opt{config} ||= find_file(); my %config; %config = ParseConfig(-ConfigFile => $opt{config}, -AutoTrue => 1) if $opt{config}; # find_file copied and modified from a version found in Config::Auto sub find_file { my $x; my $whoami = basename($0); my $bindir = dirname($0); $whoami =~ s/\.pl$//; for ("${whoami}config", ".${whoami}config", "${whoami}.config", ".${whoami}.config", "${whoami}conf", ".${whoami}conf", "${whoami}.conf", ".${whoami}.conf", "${whoami}rc", ".${whoami}rc") { return $_ if -r $_; return $x if -r ($x=catfile($bindir,$_)); return $x if -r ($x=catfile($ENV{HOME},$_)); return $x if -r ($x=catfile(rootdir(),"etc",$_)); } return undef; }

I thought about releasing the find_file code in a standalone module, but figured I'd see how Config::Auto matured and if it winds up included similar functionality.

-xdg

Code posted by xdg on PerlMonks is public domain. It has no warranties, express or implied. Posted code may not have been tested. Use at your own risk.

DG: edited code example for clarity and elegance


In reply to Re: Re: Saving application configuration to files by xdg
in thread Saving application configuration to files by crabbdean

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.