File Coverage

blib/lib/Autocache/Config.pm
Criterion Covered Total %
statement 44 46 95.6
branch 12 18 66.6
condition 2 3 66.6
subroutine 10 10 100.0
pod 0 1 0.0
total 68 78 87.1


line stmt bran cond sub pod time code
1             package Autocache::Config;
2              
3 5     5   34 use strict;
  5         20  
  5         227  
4 5     5   28 use warnings;
  5         6  
  5         155  
5              
6 5     5   22 use base 'Autocache::Config::Node';
  5         10  
  5         3347  
7              
8 5     5   51 use Carp;
  5         9  
  5         621  
9 5     5   19492 use IO::File;
  5         83223  
  5         776  
10 5     5   8234 use Data::Dumper;
  5         74719  
  5         2960  
11              
12             sub new
13             {
14 5     5 0 18 my ($class,$filename) = @_;
15 5         69 my $self = $class->SUPER::new( '' );
16              
17 5 100       26 if( $filename )
18             {
19 4 50       27 confess "configuration file supplied cannot be found/read"
20             unless _file_good( $filename );
21 4         27 $self->_load_config_file( $filename );
22             }
23             else
24             {
25 1         6 $filename = _locate_config_file();
26 1 50       6 $self->_load_config_file( $filename )
27             if defined $filename;
28             }
29              
30              
31 5         21 return $self;
32             }
33              
34             sub _load_config_file
35             {
36 4     4   12 my ($self,$filename) = @_;
37              
38 4 50       52 my $fh = IO::File->new( $filename, 'r' )
39             or confess "cannot open file for reading '$filename'";
40              
41 4         774 while( my $line = <$fh> )
42             {
43 27 100       117 next if $line =~ /^\s*$/;
44 20 100       63 next if $line =~ /^#/;
45 16 50       59 next unless $line =~ /^autocache./;
46 16         61 $line =~ s/^autocache.//;
47 16         33 chomp $line;
48 16         85 my ( $key, $value ) = split /\s+=\s+/, $line, 2;
49 16         76 $self->get_node( $key )->value( $value );
50             }
51              
52 4         60 $fh->close;
53 4         75 return 1;
54             }
55              
56             sub _locate_config_file
57             {
58 1     1   11 my $filename = sprintf '%s/etc/autocache.conf', $ENV{HOME};
59              
60 1 50       5 if( _file_good( $filename ) )
61             {
62 0         0 return $filename;
63             }
64              
65 1         3 $filename = '/etc/autocache.conf';
66              
67 1 50       52 if( _file_good( $filename ) )
68             {
69 0         0 return $filename;
70             }
71              
72 1         6 return undef;
73             }
74              
75             sub _file_good
76             {
77 6   66 6   291 return ( -e $_[0] && -r _ );
78             }
79              
80             1;