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 format is
52 roughly like .XDefaults format - <name>:<value> pairs. Note that you can use
53 either ':' or '=' as a separator between the name and value. A hash of the
54 name/value pairs are returned. Variable interpolation is supported such that
55 env(1) variables will be interpolated as well as previously defined values.
59 temp_dir: $HOME/$temp_files
60 temp_dir2: $HOME/$foo/$temp_files
64 $conf{temp_files} => "tmp"
65 $conf{temp_dir} => "~/tmp"
66 $conf{temp_dir2} => "~/$foo/tmp"
68 In other word, $HOME would be expanded because it's set in your
69 environment and $temp_files would be expanded because you set it in
70 the first line. Finally $foo would not be expanded because it was not
71 set in the first place. This is useful if other processing wants to
72 provide further interpolation.
76 The following routines are exported:
93 # Interpolate variable in str (if any) from %opts
94 sub interpolate ($%) {
95 my ($str, %opts) = @_;
97 # Since we wish to leave undefined $var references in tact the following while
98 # loop would loop indefinitely if we don't change the variable. So we work
99 # with a copy of $str changing it always, but only changing the original $str
100 # for proper interpolations.
103 while ($copyStr =~ /\$(\w+)/) {
106 if (exists $opts{$var}) {
107 $str =~ s/\$$var/$opts{$var}/;
108 $copyStr =~ s/\$$var/$opts{$var}/;
109 } elsif (exists $ENV{$var}) {
110 $str =~ s/\$$var/$ENV{$var}/;
111 $copyStr =~ s/\$$var/$ENV{$var}/;
113 $copyStr =~ s/\$$var//;
120 sub _processFile ($%) {
121 my ($configFile, %opts) = @_;
123 while (<$configFile>) {
126 next if /^\s*[\#|\!]/; # Skip comments
128 if (/\s*(.*?)\s*[:=]\s*(.*)\s*/) {
132 # Strip trailing spaces
136 $value = interpolate $value, %opts;
139 # If the key exists already then we have a case of multiple values for
140 # the same key. Since we support this we need to replace the scalar
141 # value with an array of values...
142 if (ref $opts{$key} eq "ARRAY") {
143 # It's already an array, just add to it!
144 push @{$opts{$key}}, $value;
146 # It's not an array so make it one
149 push @a, $opts{$key};
154 # It's a simple value
155 $opts{$key} = $value;
168 =head2 GetConfig ($conf)
170 Reads $filename looking for .XDefaults style name/value pairs and
185 Name of configuration file
205 =item Hash of name/value pairs
219 open my $configFile, '<', $filename
220 or carp "Unable to open config file $filename";
222 %opts = _processFile $configFile;
239 =head1 INCOMPATABILITIES
243 =head1 BUGS AND LIMITATIONS
245 There are no known bugs in this module.
247 Please report problems to Andrew DeFaria (Andrew@DeFaria.com).
251 Andrew DeFaria (Andrew@DeFaria.com)
253 =head1 LICENSE AND COPYRIGHT
255 This Perl Module is freely available; you can redistribute it and/or
256 modify it under the terms of the GNU General Public License as
257 published by the Free Software Foundation; either version 2 of the
258 License, or (at your option) any later version.
260 This Perl Module is distributed in the hope that it will be useful,
261 but WITHOUT ANY WARRANTY; without even the implied warranty of
262 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
263 General Public License (L<http://www.gnu.org/copyleft/gpl.html>) for more
266 You should have received a copy of the GNU General Public License
267 along with this Perl Module; if not, write to the Free Software Foundation,
268 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.