lib/DNStools/Config.pm
changeset 88 0e1e5027e9c0
parent 87 6d624831079f
equal deleted inserted replaced
52:53c95f2ff0ac 88:0e1e5027e9c0
       
     1 package DNStools::Config;
       
     2 
       
     3 use strict;
       
     4 use warnings;
       
     5 
       
     6 use base "Exporter";
       
     7 
       
     8 our $VERSION   = 0.0;
       
     9 our @EXPORT_OK = qw(get_config);
       
    10 
       
    11 sub get_config(@) {
       
    12 
       
    13     # read configuration
       
    14     my @configs =
       
    15         @_                          ? @_
       
    16       : defined $ENV{DNSTOOLS_CONF} ? $ENV{DNSTOOLS_CONF}
       
    17       :   ("dnstools.conf", "$ENV{HOME}/.dnstools.conf", "/etc/dnstools.conf");
       
    18     my %config;
       
    19 
       
    20     # the first config FILE
       
    21     my ($_) = grep { -f } @configs;
       
    22 
       
    23     die "no config file found, searched for @configs\n" if not $_;
       
    24     open(my $cf, $_) or die "Can't open $_: $!\n";
       
    25 
       
    26     while (<$cf>) {
       
    27         s/#.*//;
       
    28         s/\s//g;
       
    29         next unless length;
       
    30         my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
       
    31         $config{$cname} = $ccont;
       
    32     }
       
    33 
       
    34     # now merge the config hashes
       
    35     foreach my $o (grep { ref eq "HASH" } @configs) {
       
    36         %config =
       
    37           (%config, map { $_ => $o->{$_} } grep { defined $o->{$_} } keys %$o);
       
    38     }
       
    39     return %config;
       
    40 }
       
    41 
       
    42 1;
       
    43 
       
    44 __END__
       
    45 
       
    46 =head1 NAME
       
    47 
       
    48     DNStools::Config - config parser
       
    49 
       
    50 =head1 SYNOPSIS
       
    51 
       
    52     use DNStools::Config qw(get_config);
       
    53     %config = get_config();
       
    54     %config = get_config($file1, $file2, ...);
       
    55 
       
    56 =head1 DESCRIPTION
       
    57 
       
    58 Simple config file parser. The format is simple:
       
    59 
       
    60     key = value
       
    61 
       
    62 All spaces are ignored.
       
    63 
       
    64 =head1 FUNCTIONS
       
    65 
       
    66 =over
       
    67 
       
    68 =item B<get_config>(I<list of config files>)
       
    69 
       
    70 Read the first file of the list (or dies if none of the files is found).
       
    71 Returns a hash with the config keys and values.
       
    72 
       
    73 If the list is empty, the configuration file is search in some default
       
    74 locations: C<$DNSTOOLS_CONF>, F<./dnstools.conf>,
       
    75 F<$HOME/.dnstools.conf>, F</etc/dnstools.conf>.
       
    76 
       
    77 =back
       
    78 
       
    79 =cut
       
    80 
       
    81 
       
    82