7 @Yaala::Config::EXPORT_OK = qw/get_config parse_argv read_config get_checksum/;
9 @Yaala::Config::ISA = ('Exporter');
13 Parsing of configuration files and query method.
17 use Yaala::Config qw#get_config read_config#;
19 read_config ("filename");
20 read_config ($filehandle);
28 Here are the syntax rules:
34 An options starts with a keyword, followed by a colon, then the value for
35 that key and is ended with a semi-colon. Example:
41 Text in single- or souble quotes is taken literaly. Quotes can not be
42 escaped. However, singlequotes enclosed in double quotes (and vice versa)
43 are perfectly ok. Examples:
45 teststring: "Yay, it's a string!";
47 html: '<span style="color: #fe0000;">';
51 Hashes are start comments and are ignored to the end of the line. Hashes
52 enclosed in quotes are B<not> interpreted as comments.. See html-example
57 Linebreaks and spaces (unless when in quotes..) are ignored. Strings may
58 not span multiple lines. Use something along this lines instead:
60 multiplelineoption: "This is a very very long"
61 "string that continues in the next line";
65 Any key may occur more than once. You can separate two or more values with
68 key: value1, value2, "This, is ONE value..";
76 =head2 Structure of $config
78 C<$config-E<gt>{'key'} = ['val0', 'val1', ...];>
84 my $VERSION = '$Id: Config.pm,v 1.4 2003/12/07 14:52:02 octo Exp $';
85 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
89 =head2 get_config ($key)
91 Queries the config structure for the given key and returns the value(s).
92 In list context all values are returned, in scalar context only the most
102 if (!defined ($config->{$key}))
104 return (wantarray () ? () : '');
107 $val = $config->{$key};
119 =head2 parse_argv (@argv)
121 Parses ARGV and adds command-line options to the internal config
132 my $item = shift (@argv);
134 if ($item =~ m/^--?(\S+)/)
140 print STDERR $/, __FILE__, ": No value for key '$key'",
145 my $val = shift (@argv);
147 push (@{$config->{$key}}, $val);
151 push (@{$config->{'input'}}, $item);
155 print STDERR $/, __FILE__, ': Ignoring empty argument.';
162 =head2 parse_config ($string)
164 Parses $string and adds the extracted configuration options to the
180 for (split (m/\n+/s, $text))
186 while ($line =~ m/^[^#]*(['"]).*?\1/)
188 $line =~ s/(['"])(.*?)\1/<:$rep:>/;
201 while ($text =~ m/(\w+):([^;]+);/g)
204 my @val = split (m/,/, $2);
206 s/<:(\d+):>/$rep[$1]/eg for (@val);
208 push (@{$config->{$key}}, @val);
214 =head2 read_config ($file)
216 Reads the configuration file. $file must either be a filename, a reference
217 to one or a reference to a filehandle.
227 local ($/) = undef; # slurp mode ;)
229 if (ref ($arg) eq 'GLOB')
233 elsif (!ref ($arg) || ref ($arg) eq 'SCALAR')
236 if (ref ($arg)) { $scalar_arg = $$arg; }
237 else { $scalar_arg = $arg; }
241 print STDERR $/, __FILE__, ': Configuration file ',
242 "'$scalar_arg' does not exist";
246 unless (open ($fh, "< $scalar_arg"))
248 print STDERR $/, __FILE__, ': Unable to open ',
257 my $type = ref ($arg);
259 print STDERR $/, __FILE__, ": Reference type $type not ",
264 # By now we should have a valid filehandle in $fh
268 close ($fh) if ($need_close);
270 parse_config ($text);
277 my $logtype = get_config ('logtype');
278 my @selects = get_config ('select');
280 my $checksum = lc ($logtype) . '::' . join (':', map { lc ($_) } (sort (@selects)));
287 Florian octo Forster E<lt>octo@verplant.orgE<gt>