my/Config.pm
branchhs12
changeset 72 d3d2108f07da
parent 71 e25fc893e203
child 73 9c304b77cf02
equal deleted inserted replaced
71:e25fc893e203 72:d3d2108f07da
     1 package my::Config;
       
     2 use strict;
       
     3 use warnings;
       
     4 
       
     5 use base "Exporter";
       
     6 
       
     7 our $VERSION   = 0.0;
       
     8 our @EXPORT_OK = qw(get_config);
       
     9 
       
    10 sub get_config(@) {
       
    11 
       
    12     # read configuration
       
    13     my @configs = @_;
       
    14     my %config;
       
    15 
       
    16     # the first config FILE
       
    17     my ($_) = grep { -f } @configs;
       
    18     open(my $cf, $_) or die "Can't open $_: $!\n";
       
    19 
       
    20     while (<$cf>) {
       
    21         s/#.*//;
       
    22         s/\s//g;
       
    23         next unless length;
       
    24         my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
       
    25         $config{$cname} = $ccont;
       
    26     }
       
    27 
       
    28     # now merge the config hashes
       
    29     foreach my $o (grep { ref eq "HASH" } @configs) {
       
    30         %config =
       
    31           (%config, map { $_ => $o->{$_} } grep { defined $o->{$_} } keys %$o);
       
    32     }
       
    33     return %config;
       
    34 }
       
    35 
       
    36 1;