]> mj.ucw.cz Git - libucw.git/blob - lib/perl/Config.pm
Added a Perl module for parsing options and configuration.
[libucw.git] / lib / perl / Config.pm
1 # Perl module for parsing Sherlock configuration files (using the config utility)
2 # (c) 2002 Martin Mares <mj@ucw.cz>
3
4 package Sherlock::Config;
5
6 use strict;
7 use warnings;
8 use Getopt::Long;
9
10 our %Sections = ();
11
12 sub Parse(@) {
13         my @options = @_;
14         my $defargs = "";
15         push @options, "config|C=s" => sub { my ($o,$a)=@_; $defargs .= " -C'$a'"; };
16         push @options, "set|S=s" => sub { my ($o,$a)=@_; $defargs .= " -S'$a'"; };
17         Getopt::Long::GetOptions(@options) or return 0;
18         foreach my $section (keys %Sections) {
19                 my $opts = $Sections{$section};
20                 my $optlist = join(" ", keys %$opts);
21                 my @l = `bin/config $defargs $section $optlist`;
22                 $? && exit 1;
23                 foreach my $o (@l) {
24                         $o =~ /^CF_([^=]+)="(.*)"\n$/ or die "Cannot parse bin/config output: $_";
25                         my $var = $$opts{$1};
26                         my $val = $2;
27                         if (ref $var eq "SCALAR") {
28                                 $$var = $val;
29                         } elsif (ref $var eq "ARRAY") {
30                                 push @$var, $val;
31                         } elsif (ref $var) {
32                                 die ("Sherlock::Config::Parse: don't know how to set $o");
33                         }
34                 }
35         }
36         1;
37 }
38
39 1;  # OK