in reply to Re: Saving application configuration to files
in thread Saving application configuration to files
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
|
|---|