Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-24 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found