7 @Onis::Config::EXPORT_OK = qw/get_config parse_argv read_config get_checksum/;
9 @Onis::Config::ISA = ('Exporter');
13 Onis::Config - Parsing of configuration files and query method.
17 use Config qw#get_config read_config#;
19 read_config ("filename");
20 read_config ($filehandle);
28 Here are the syntax rules:
34 An option 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!";
46 html: '<span style="color: #fe0000;">';
50 Hashes are start comments and are ignored to the end of the line. Hashes
51 enclosed in quotes are B<not> interpreted as comments.. See html-example
56 Linebreaks and spaces (unless when in quotes..) are ignored. Strings may
57 not span multiple lines. Use something along this lines instead:
59 multiplelineoption: "This is a very very long"
60 "string that continues in the next line";
64 Any key may occur more than once. You can separate two or more values with
67 key: value1, value2, "This, is ONE value..";
76 my $VERSION = '$Id: Config.pm,v 1.10 2004/09/16 10:30:00 octo Exp $';
77 print STDERR $/, __FILE__, ": $VERSION" if ($::DEBUG);
81 =head1 EXPORTED FUNCTIONS
85 =item B<get_config> (I<$key>)
87 Queries the config structure for the given key and returns the value(s). In
88 list context all values are returned, in scalar context only the most recent
98 if (!defined ($config->{$key}))
100 return (wantarray () ? () : '');
103 $val = $config->{$key};
115 =item B<parse_argv> (I<@argv>)
117 Parses ARGV and adds command-line options to the internal config structure.
127 my $item = shift (@argv);
129 if ($item =~ m/^--?(\S+)/)
135 print STDERR $/, __FILE__, ": No value for key '$key'",
140 my $val = shift (@argv);
142 push (@{$config->{$key}}, $val);
146 push (@{$config->{'input'}}, $item);
150 print STDERR $/, __FILE__, ': Ignoring empty argument.';
168 for (split (m/\n+/s, $text))
174 while ($line =~ m/^[^#]*(['"]).*?\1/)
176 $line =~ s/(['"])(.*?)\1/<:$rep:>/;
189 while ($text =~ m/(\w+):([^;]+);/g)
192 my @val = split (m/,/, $2);
194 s/<:(\d+):>/$rep[$1]/eg for (@val);
196 push (@{$config->{$key}}, @val);
202 =item B<read_config> (I<$file>)
204 Reads the configuration file. $file must either be a filename, a reference to
205 one or a reference to a filehandle. Complains, is file does not exist.
215 local ($/) = undef; # slurp mode ;)
217 if (ref ($arg) eq 'GLOB')
221 elsif (!ref ($arg) || ref ($arg) eq 'SCALAR')
224 if (ref ($arg)) { $scalar_arg = $$arg; }
225 else { $scalar_arg = $arg; }
229 print STDERR $/, __FILE__, ': Configuration file ',
230 "'$scalar_arg' does not exist";
234 unless (open ($fh, "< $scalar_arg"))
236 print STDERR $/, __FILE__, ': Unable to open ',
245 my $type = ref ($arg);
247 print STDERR $/, __FILE__, ": Reference type $type not ",
252 # By now we should have a valid filehandle in $fh
256 close ($fh) if ($need_close);
258 parse_config ($text);
267 Florian octo Forster E<lt>octo at verplant.orgE<gt>