File Coverage

blib/lib/DBIx/dbMan/Config.pm
Criterion Covered Total %
statement 12 72 16.6
branch 0 24 0.0
condition 0 9 0.0
subroutine 4 11 36.3
pod 0 3 0.0
total 16 119 13.4


line stmt bran cond sub pod time code
1             package DBIx::dbMan::Config;
2              
3 3     3   20 use strict;
  3         4  
  3         85  
4 3     3   946 use locale;
  3         1380  
  3         14  
5 3     3   152 use vars qw/$AUTOLOAD/;
  3         7  
  3         159  
6 3     3   980 use POSIX;
  3         16947  
  3         17  
7              
8             our $VERSION = '0.04';
9              
10             1;
11              
12             sub new {
13 0     0 0   my $class = shift;
14 0           my $obj = bless { @_ }, $class;
15              
16 0           $obj->{config} = {};
17 0   0       $obj->{configfile} = $obj->{-file} || $obj->_configfile();
18 0 0         $obj->_load if $obj->{configfile};
19              
20 0           return $obj;
21             }
22              
23             sub _bhashes {
24 0     0     my $line = shift;
25              
26 0           $line =~ s/#/\\#/g;
27 0           return $line;
28             }
29              
30             sub _load {
31 0     0     my $obj = shift;
32 0 0         if (open F,$obj->{configfile}) {
33 0           while () {
34 0           my $key; my $value;
35 0           chomp;
36 0           s/\\/\\\\/g; # double backslashes
37 0           s/^(['"])(.*?)([^\\])\1/$1.(_bhashes($2.$3)).$1/eg;
  0            
38 0           s/([^\\])(['"])(.*?)([^\\])\2/$1.$2.(_bhashes($3.$4)).$2/eg;
  0            
39             # backslash # in ''
40 0           s/^#.*$//; # whole-line comment
41 0           s/([^\\])#.*$/$1/; # other comment
42 0           s/\\#/#/g; # unbackslash #
43 0           s/\\\\/\\/g; # single backslashes
44 0           s/^\s+//; # starting whitespaces
45 0           s/\s+$//; # ending whitespaces
46 0 0         next unless $_; # empty line
47 0 0         if (/^(\S+)\s+(.*)$/) {
48 0           ($key,$value) = ($1,$2);
49             } else {
50 0           ($key,$value) = ($_,'');
51             }
52 0           $value =~ s/^(['"])(.*)\1$/$2/; # quoted line
53 0           push @{$obj->{config}->{$key}},$value;
  0            
54             }
55 0           close F;
56             }
57             }
58              
59             sub merge {
60 0     0 0   my ($obj,$config) = @_;
61              
62 0 0         return 0 unless $config;
63              
64 0           for my $tag ($config->all_tags) {
65 0 0         if ( ref $config->$tag eq "ARRAY" ) {
66 0           push @{$obj->{config}->{$tag}}, @{$config->$tag};
  0            
  0            
67             } else {
68 0           push @{$obj->{config}->{$tag}}, $config->$tag;
  0            
69             }
70             }
71 0           return 1;
72             }
73              
74             sub _configfile {
75 0     0     my $obj = shift;
76            
77 0           my $res = $ENV{DBMAN_CONFIG};
78 0 0 0       return $res if $res and -e $res;
79              
80 0           $res = $ENV{HOME}.'/.dbman/config';
81 0 0         return $res if -e $res;
82              
83 0           return '/etc/dbman.conf';
84             }
85              
86             sub all_tags {
87 0     0 0   my $obj = shift;
88 0           return keys %{$obj->{config}};
  0            
89             }
90              
91             sub AUTOLOAD {
92 0     0     my $obj = shift;
93              
94 0           $AUTOLOAD =~ s/^DBIx::dbMan::Config:://;
95 0           my $res = $obj->{config}->{$AUTOLOAD};
96 0 0         if (defined $res) {
97 0 0 0       if (ref $res and scalar @$res > 1) {
98 0 0         return wantarray ? @$res : $res;
99             } else {
100 0 0         return wantarray ? @$res : $res->[0];
101             }
102             } else {
103 0           return undef;
104             }
105             }