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 = ){ 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 %{$tmpcfg{$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]