in reply to Saving application configuration to files

If your config files are simple, I've found the configkey=configvalue set to be the fastest and simplest to use for my purposes. I have a generic routine that I can include in any program to read a specific config file in any program as follows (based on the routine in the Perl Cookbook). I have tried config files using some of the modules mentioned here and in XML format but, for my purposes, it seemed to make things a little more complex without adding a feature that I need (not that they don't have useful features mind you. Just not one that I need.). One thing I have found very helpful in the scheme below, is that I can specify configurations for a set of programs that together make up a system giving me the option of specifiying common elements and program specific elements. You can also specify more than one section to read options from and it will read them sequentially.

my $cfg = ReadConfig("./configfilename",qw(someprog02)) ############################################################; sub ReadConfig{ # ReadConfig([full path & filename],[section list]) # this subroutine reads a configuration file and returning a # reference to a hash containing the key value pairs. The # routine will use a full path name if provided or look in # the current directory for the configuration file. The # routine will parse all options or just the options provided # in the section parameter. By default, it will include any # value in the [ALL] section if one is existing. The config # reader will process each section in the file in the order # passed to it by the list section. By default, [ALL] will # be processed first and need not be included in the list # unless you want [ALL] to be after some other section. The # routine will overwrite any options set in previous sections # will the option for the current section so that an option # for pgmdir in the section [someprog01] will overlay the # pgmdir option set in the [ALL] section (assuming the [ALL] # section precedes [someprog01] in the config file Note, this # reader will capitalize all section headers so case does not # matter my $infile = shift; my @sect = @_; my %config = (); my %tmpcfg = (); my $line = ''; my $valkey = ''; my $value = ''; my $sectkey = 'ALL'; if(-e $infile and open(INFILE, "<$infile")){ while($line = <INFILE>){ chomp($line); $line =~ s/#.*//; #ignore comments $line =~ s/^\s+//; #delete leading whitespace $line =~ s/\s+$//; #delete trailing whitespace #skip it nothing is left next unless length($line); if($line =~ m/[[](\w+)[\]]/x){ $sectkey = lc($1); }else{ ($valkey,$value) = split(/\s*=\s*/,$line,2) ; $tmpcfg{$sectkey}{lc($valkey)} = $value; } } } unshift @sect, 'ALL'; # prepend 'ALL' section foreach $sectkey (map { lc } @sect){ $config{$_} = $tmpcfg{$sectkey}{$_} foreach (sort keys %{$tmpc +fg{$sectkey}}); } return (%config) ? \%config : '' ; } #end ReadConfig(); ############################################################; __CONFIGFILE__ # test configuration # # this is the configuration file for the SOMESYS processing # system directory entries are relative to basedir unless # otherwise specified # [all] baseshare=\\UNCDRIVE\DATA\ # basedir=K:\DataDrive\Development\rel-2_6\ [someprog01] [someprog02] workdir=ftpbakprocessing\ seplenopts=0,1,2 reclenopts=770,364,750,344 filelenopts=0,1,2 [someprog03] [end]