my/Config.pm
branchhs12
changeset 71 e25fc893e203
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/my/Config.pm	Mon Jan 03 16:49:56 2011 +0100
@@ -0,0 +1,36 @@
+package my::Config;
+use strict;
+use warnings;
+
+use base "Exporter";
+
+our $VERSION   = 0.0;
+our @EXPORT_OK = qw(get_config);
+
+sub get_config(@) {
+
+    # read configuration
+    my @configs = @_;
+    my %config;
+
+    # the first config FILE
+    my ($_) = grep { -f } @configs;
+    open(my $cf, $_) or die "Can't open $_: $!\n";
+
+    while (<$cf>) {
+        s/#.*//;
+        s/\s//g;
+        next unless length;
+        my ($cname, $ccont) = split(/\s*=\s*/, $_, 2);
+        $config{$cname} = $ccont;
+    }
+
+    # now merge the config hashes
+    foreach my $o (grep { ref eq "HASH" } @configs) {
+        %config =
+          (%config, map { $_ => $o->{$_} } grep { defined $o->{$_} } keys %$o);
+    }
+    return %config;
+}
+
+1;