3 =head1 NAME $RCSfile: GetConfig.pm,v $
5 Simple config file parsing
13 Andrew DeFaria <Andrew@DeFaria.com>
21 Tue Feb 14 11:03:18 PST 2006
25 $Date: 2013/01/17 01:08:34 $
33 # Comment lines are skipped - white space is eliminated...
38 version: $major_version.$release
40 my %opts = GetConfig "myconfig.cfg";
41 print "Application Name:\t" . $opts {app} . "(" . $opts {version} . )\n";
42 print "Iterrations:\t\t" . $opts {nbr_iterrations} . "\n";
46 Application Name: MyApp (1.2)
51 This module is a simple interface to reading config files. Config file
52 format is roughly like .XDefaults format - <name>:<value> pairs. A
53 hash of the name/value pairs are returned. Variable interpolation is
54 supported such that env(1) variables will be interpolated as well as
55 previously defined values. Thus:
58 temp_dir: $HOME/$temp_files
59 temp_dir2: $HOME/$foo/$temp_files
63 $conf{temp_files} => "tmp"
64 $conf{temp_dir} => "~/tmp"
65 $conf{temp_dir2} => "~/$foo/tmp"
67 In other word, $HOME would be expanded because it's set in your
68 environment and $temp_files would be expanded because you set it in
69 the first line. Finally $foo would not be expanded because it was not
70 set in the first place. This is useful if other processing wants to
71 provide further interpolation.
75 The following routines are exported:
92 # Interpolate variable in str (if any) from %opts
93 sub interpolate ($%) {
94 my ($str, %opts) = @_;
96 # Since we wish to leave undefined $var references in tact the following while
97 # loop would loop indefinitely if we don't change the variable. So we work
98 # with a copy of $str changing it always, but only changing the original $str
99 # for proper interpolations.
102 while ($copyStr =~ /\$(\w+)/) {
105 if (exists $opts{$var}) {
106 $str =~ s/\$$var/$opts{$var}/;
107 $copyStr =~ s/\$$var/$opts{$var}/;
108 } elsif (exists $ENV{$var}) {
109 $str =~ s/\$$var/$ENV{$var}/;
110 $copyStr =~ s/\$$var/$ENV{$var}/;
112 $copyStr =~ s/\$$var//;
119 sub _processFile ($%) {
120 my ($configFile, %opts) = @_;
122 while (<$configFile>) {
125 next if /^\s*[\#|\!]/; # Skip comments
127 if (/\s*(\w*)\s*:\s*(.*)\s*$/) {
131 # Strip trailing spaces
135 $value = interpolate $value, %opts;
138 # If the key exists already then we have a case of multiple values for
139 # the same key. Since we support this we need to replace the scalar
140 # value with an array of values...
141 if (ref $opts{$key} eq "ARRAY") {
142 # It's already an array, just add to it!
143 push @{$opts{$key}}, $value;
145 # It's not an array so make it one
148 push @a, $opts{$key};
153 # It's a simple value
154 $opts{$key} = $value;
167 =head2 GetConfig ($conf)
169 Reads $filename looking for .XDefaults style name/value pairs and
184 Name of configuration file
204 =item Hash of name/value pairs
218 open my $configFile, '<', $filename
219 or carp "Unable to open config file $filename";
221 %opts = _processFile $configFile;
238 =head1 INCOMPATABILITIES
242 =head1 BUGS AND LIMITATIONS
244 There are no known bugs in this module.
246 Please report problems to Andrew DeFaria (Andrew@DeFaria.com).
250 Andrew DeFaria (Andrew@DeFaria.com)
252 =head1 LICENSE AND COPYRIGHT
254 This Perl Module is freely available; you can redistribute it and/or
255 modify it under the terms of the GNU General Public License as
256 published by the Free Software Foundation; either version 2 of the
257 License, or (at your option) any later version.
259 This Perl Module is distributed in the hope that it will be useful,
260 but WITHOUT ANY WARRANTY; without even the implied warranty of
261 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
262 General Public License (L<http://www.gnu.org/copyleft/gpl.html>) for more
265 You should have received a copy of the GNU General Public License
266 along with this Perl Module; if not, write to the Free Software Foundation,
267 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.