File Coverage

blib/lib/BW/Config.pm
Criterion Covered Total %
statement 15 58 25.8
branch 0 12 0.0
condition 0 9 0.0
subroutine 5 13 38.4
pod 5 6 83.3
total 25 98 25.5


line stmt bran cond sub pod time code
1             # BW::Config.pm
2             # by Bill Weinman Support for config files
3             # Based upon bwConfig by Bill Weinman
4             #
5             # Copyright (c) 1995-2010 The BearHeart Group, LLC
6             #
7              
8             package BW::Config;
9 1     1   1354 use strict;
  1         2  
  1         45  
10 1     1   6 use warnings;
  1         2  
  1         30  
11              
12 1     1   5 use IO::File;
  1         2  
  1         596  
13 1     1   8 use BW::Constants;
  1         2  
  1         72  
14 1     1   6 use base qw( BW::Base );
  1         2  
  1         1052  
15              
16             our $VERSION = 1.0;
17 0     0 1   sub version { $VERSION }
18              
19             sub _init
20             {
21 0     0     my $self = shift;
22 0           $self->SUPER::_init(@_);
23              
24             # arrange the room
25 0           $self->{raw_lines} = [];
26 0           $self->{keywords} = [];
27 0           $self->{values} = {};
28 0           $self->{arrays} = {};
29 0           $self->{me} = ref($self);
30              
31             # shall we parse?
32 0 0         $self->parse() if $self->{filename};
33              
34 0           return SUCCESS;
35             }
36              
37             # _setter_getter entry points
38 0     0 1   sub filename { BW::Base::_setter_getter(@_); }
39              
40             ### getters only
41              
42             # get the keywords arrayref
43             sub keywords
44             {
45 0     0 1   my $self = shift;
46 0   0       return $self->{keywords} || [];
47             }
48              
49             # get the values hash
50             sub values
51             {
52 0     0 1   my $self = shift;
53 0   0       return $self->{values} || {};
54             }
55              
56             # get the arrays hash
57             sub arrays
58             {
59 0     0 1   my $self = shift;
60 0   0       return $self->{arrays} || {};
61             }
62              
63             ### worker bees
64              
65             # parse the file
66             sub parse
67             {
68 0     0 0   my $sn = 'parse';
69 0           my $self = shift;
70 0           my $args = shift;
71              
72             # find the filename
73 0   0       my $filename = $args->{filename} || $self->filename;
74              
75 0 0         return $self->_error("$sn: No filename.") unless $filename;
76              
77             # set the filename property while opening the file
78 0 0         my $fh = new IO::File( $filename, 'r' )
79             or return $self->_error("$sn: Cannot open config file ($filename): $!");
80              
81 0           while (<$fh>) {
82 0           s/#.*//; # trim end-of-line comments
83 0           $_ = $self->_trim($_); # trim surrounding whitespace
84 0 0         next if /^$/; # skip semantically empty lines
85              
86 0           push @{ $self->{raw_lines} }, $_;
  0            
87 0           my ( $lh, $rh ) = split( /=/, $_, 2 );
88 0           $lh = $self->_trim($lh); # trim them up
89 0           $rh = $self->_trim($rh);
90 0           push @{ $self->{keywords} }, $lh;
  0            
91 0 0         $rh = '' unless $rh; # avoid undef
92 0           $self->{values}{$lh} = $rh;
93 0           @{ $self->{arrays}{$lh} } = split( /:/, $rh );
  0            
94             }
95 0           return TRUE;
96             }
97              
98             # trim leading/trailing space from a string
99             sub _trim
100             {
101 0     0     my $self = shift;
102 0 0         my $string = shift or return '';
103              
104 0           $string =~ /\s*(.*?)\s*$/;
105 0           return $1;
106             }
107              
108             1;
109              
110             __END__