Object/Conf.pm

filter: Statementstmt 72.9% 54 / 74 Branchbran 54.0% 27 / 50 Conditioncond 48.0% 12 / 25 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 100% 6 / 6 Podpod 50.0% 2 / 4 totaltotal 60.1% 104 / 173 CC 38 SCAR 46.8
CC
38
Cov
62%
read_conf_file
128.0
conf_files
4.1
BEGIN
1.0
CRAP 107.5
« Machine.pm ↑ Summary Test/More.pm »
1 package Basset::Object::Conf;
2  
3 #Basset::Object::Conf Copyright and (c) 2002, 2003, 2004, 2005, 2006 James A Thomason III
4 #Basset::Object::Conf is distributed under the terms of the Perl Artistic License.
5  
6 =pod
7  
8 =head1 NAME
9  
10 Basset::Object::Conf - used to read conf files
11  
12 =head1 AUTHOR
13  
14 Jim Thomason, jim@jimandkoka.com
15  
16 =head1 DESCRIPTION
17  
18 It's good not to set up default values inside of your module. Believe me, I know. Lord knows I've gotten chewed out enough for Carp::Notify
19 having the defaults in the module. Anyway, this module includes instructions for the conf file format, how the read_conf_file method works,
20 and some bits of interaction with the rest of the system. See Basset::Object for more information.
21  
22 =cut
23  
24  
25 9 $VERSION = '1.03';
26  
27 #
28 # Basset::Object::Conf isa Basset::Object, but there are circular inheritance reasons. So, instead,
29 # @ISA is set from within the read_conf_file method
30 #
31 #
32 #use Basset::Object;
33 #@ISA = qw(Basset::Object);
34  
35 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =pod
39  
40 =head1 SET-UP
41  
42 You'll need to specify your conf files. There is the @conf_files array, toss in as many conf files as you'd like
43  
44 my @conf_files = qw(
45 /etc/mail.bulkmail.cfg
46 /etc/mail.bulkmail.cf2
47 );
48
49 It'll just silently ignore any conf files that aren't present, so don't expect any errors. That's to allow you
50 to place multiple conf files in for use on multiple servers and then not worry about them.
51  
52 Multiple conf files are in significance order. So if mail.bulkmail.cfg and mail.bulkmail.cf2 both define a value
53 for 'foo', then the one in mail.bulkmail.cfg is used. And so on, conf files listed earlier are more important.
54 There is no way for a program to later look at a less significant conf value.
55  
56 =cut
57  
58 9 our @conf_files = (qw(
59 /etc/basset.conf
60 ./basset.conf
61 Basset/Object/basset.conf
62 lib/Basset/Object/basset.conf
63 ),
64 );
65 9 our %conf_files = ();
66  
67 773 ▶sub conf_files {
Subroutine conf_files: called CC=3 SCAR=14.2
Pod undocumented
68 773 my $class = shift;
69 773 foreach (reverse @_) {
70 0 ▶ unshift @conf_files, $_ unless $conf_files{$_}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $Basset::Object::Conf::conf_files{$_}++ 0 0 0
71 }
72  
73 773 return @conf_files;
74
75 }
76  
77 9 our $default_package = 'Basset::Object';
78  
79 =pod
80  
81 =over
82  
83 =item read_conf_file
84  
85 read_conf_file will read in the conf files specified in the @conf_files array up at the top.
86  
87 You can also pass in a list of conf files to read, in most to least significant order, same as the @conf_files array.
88  
89 my $conf = Mail::Bulkmail::Object->read_conf_file();
90 or
91 my $conf = Mail::Bulkmail::Object->read_conf_file('conf_files' => '/other/conf.file');
92 or
93 my $conf = Mail::Bulkmail::Object->read_conf_file('conf_files' => ['/other/conf.file', '/additional/conf.file']);
94
95 If you pass in a list of conf files, then the internal @conf_files array is bypassed.
96  
97 $conf is a hashref of hashrefs. the main keys are the package names, the values are the hashes of the values
98 for that object.
99  
100 Example:
101  
102 #conf file
103 define package Mail::Bulkmail
104
105 use_envelope = 1
106 safe_banned = 0
107
108 define package Mail::Bulkmail::Server
109
110 Smtp = your.smtp.com
111 Port = 25
112
113 $conf = {
114 'Mail::Bulkmail' => {
115 'use_envelope' => 1,
116 'safe_banned' => 1
117 },
118 'Mail::Bulkmail::Server' => {
119 'Smtp' => 'your.smtp.com',
120 'safe_banned' => 1
121 }
122 };
123
124 read_conf_file is called at object initialization. Any defaults for your object are read in at this time.
125 You'll rarely need to read the conf file yourself, since at object creation it is read and parsed and the values passed
126 on.
127  
128 Note that it will combine the conf file in with an existing conf hash. To get a fresh one, pass in the conf_hash parameter.
129  
130 Basset::Object->read_conf_file('conf_hash' => {});
131  
132 B<Be sure to read up on the conf file structure, below>
133  
134 The conf file is only re-read if it has been modified since the last time it was read.
135  
136 =cut
137  
138 9 our $conf = {};
139 9 our $loaded = {};
140  
141 778 ▶sub conf {
Subroutine conf: called CC=1 SCAR=0.0
Pod documented
142 778 return $conf;
143 }
144  
145 1555 ▶sub loaded {
Subroutine loaded: called CC=1 SCAR=0.0
Pod undocumented
146 1555 return $loaded;
147 }
148
149 773 ▶sub read_conf_file {
Subroutine read_conf_file: called CC=36 SCAR=48.5
Pod documented
150 773 my $class = shift;
151
152 #this is a major league hack. Since Basset::Object::Conf isa Basset::Object, we wait
153 #until now to set its inheritance. Basset::Object::Conf should never be instantiated, so it's
154 #not an issue. The first time read_conf_file is called, we set this to be a Basset::Object.
155 #
156 #This way, we can successfully compile this module first, and have Basset::Object use it.
157 773 ▶ unless (@Basset::Object::Conf::ISA) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@Basset::Object::Conf::ISA) 9 764 773
158 9 require Basset::Object;
159 9 @Basset::Object::Conf::ISA = qw(Basset::Object);
160 }
161  
162 773 my %init = @_;
163
164 773 ▶ if (defined $init{'conf_files'} && ! ref $init{'conf_files'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init{"conf_files"} and not ref $init{"conf_files"}) 0 773 773
Condition33%1/3
defined $init{"conf_files"} and not ref $init{"conf_files"}
!ll&&!rl&&r
77300
MC/DC0%0/2
defined $init{"conf_files"} and not ref $init{"conf_files"}
defined $init{"conf_files"} not ref $init{"conf_files"}
Truth table33%1/3
A and not(B)
A= defined $init{"conf_files"}B= not ref $init{"conf_files"}
ABresult
0X0
100
111
165 0 $init{'conf_files'} = [$init{'conf_files'}];
166 }
167  
168 0 ▶ my @confs = reverse($init{'conf_files'} ? @{$init{'conf_files'}} : $class->conf_files);
Branch: 1 branch
BranchTrueFalseTotal
$init{'conf_files'} ? : 0 773 773
169 773 ▶ my $conf = $init{'conf_hash'} ? $init{'conf_hash'} : $class->conf;
Branch: 1 branch
BranchTrueFalseTotal
$init{'conf_hash'} ? : 0 773 773
170  
171 773 $conf->{$default_package}->{'types'}->{'conf'} = $class;
172  
173 773 foreach my $conf_file (@confs){
174 3092 ▶ next unless -e $conf_file ;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $conf_file 2319 773 3092
175  
176 773 ▶ if (! $class->loaded->{$conf_file} || -M $conf_file < $class->loaded->{$conf_file} || @_){
Branch: 1 branch
BranchTrueFalseTotal
if (not $class->loaded->{$conf_file} or -M $conf_file < $class->loaded->{$conf_file} or @_) 9 764 773
Condition66%4/6
not $class->loaded->{$conf_file} or -M $conf_file < $class->loaded->{$conf_file} or @_
l!l&&r!l&&!r
90764
not $class->loaded->{$conf_file} or -M $conf_file < $class->loaded->{$conf_file}
l!l&&r!l&&!r
90764
MC/DC33%1/3
not $class->loaded->{$conf_file} or -M $conf_file < $class->loaded->{$conf_file} or @_
not $class->loaded->{$conf_file} -M $conf_file < $class->loaded->{$conf_file} @_
Truth table50%2/4
not(A) or B or C
A= not $class->loaded->{$conf_file}B= -M $conf_file < $class->loaded->{$conf_file}C= @_
ABCresult
1XX1
01X1
0011
0000
177  
178 9 my $pkg = $default_package;
179  
180 9 my $handle = $class->gen_handle;
181  
182 9 ▶ open ($handle, $conf_file) || next;
Branch: 1 branch
BranchTrueFalseTotal
open $handle, $conf_file or next 9 0 9
183 9 ▶ while (my $line = <$handle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $handle)) 378 9 387
184  
185 378 ▶ next if ! defined $line || $line =~ /^\s*#/ || $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $line or $line =~ /^\s*#/ or $line =~ /^\s*$/ 252 126 378
Condition83%5/6
not defined $line or $line =~ /^\s*#/ or $line =~ /^\s*$/
l!l&&r!l&&!r
144108126
not defined $line or $line =~ /^\s*#/
l!l&&r!l&&!r
0144234
MC/DC66%2/3
not defined $line or $line =~ /^\s*#/ or $line =~ /^\s*$/
not defined $line $line =~ /^\s*#/ $line =~ /^\s*$/
Truth table75%3/4
not(A) or B or C
A= not defined $lineB= $line =~ /^\s*#/C= $line =~ /^\s*$/
ABCresult
1XX1
01X1
0011
0000
186
187 126 ▶ if ($line =~ /^define package\s+(\S+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^define package\s+(\S+)/) 36 90 126
188 36 $pkg = $1;
189 36 next;
190 };
191
192 90 ▶ if ($line =~ /^include file\s+(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^include file\s+(\S+)/) 0 90 90
193 0 my $subconf = $class->read_conf_file($1);
194 0 foreach my $pkg (keys %$subconf) {
195 0 my $pkgconf = $conf->{$pkg};
196 0 my $subpkgconf = $subconf->{$pkg};
197 0 @$pkgconf{keys %$subpkgconf} = values %$subpkgconf;
198 };
199 0 next;
200 };
201
202 90 $line =~ s/(?:^\s+|\s+$)//g;
203 90 ▶ $line =~ /^(?:\s*(\d+)\s*:)?\s*([-+]?\w+)\s*([@%]?)=\s*(.+)/
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^(?:\s*(\d+)\s*:)?\s*([-+]?\w+)\s*([\@%]?)=\s*(.+)/ or return $class->error("Invalid conf file : $line", "BOC-02") 90 0 90
204 or return $class->error("Invalid conf file : $line", "BOC-02");
205  
206 90 my ($user, $key, $ref, $val) = ($1, $2, $3, $4);
207  
208 90 ▶ unless (defined $val){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 90 90
209 0 ($user, $key, $ref, $val) = ($user, $key, undef, $ref);
210 };
211
212 90 ▶ unless (defined $ref){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ref) 0 90 90
213 0 ($user, $key, $ref, $val) = (undef, $user, $ref, $key);
214 };
215  
216 90 ▶ ($user, $key, $val) = (undef, $user, $key) unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 90 90
217
218 90 ▶ next if defined $user && $user != $>;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and $user != $> 0 90 90
Condition33%1/3
defined $user and $user != $>
!ll&&!rl&&r
9000
MC/DC0%0/2
defined $user and $user != $>
defined $user $user != $>
Truth table33%1/3
A and B
A= defined $userB= $user != $>
ABresult
0X0
100
111
219
220 90 ▶ $val = undef if $val eq 'undef';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "undef" 0 90 90
221  
222 90 ▶ $val = eval qq{return "$val"} if defined $val && $val =~ /^\\/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val and $val =~ /^\\/ 0 90 90
Condition33%1/3
defined $val and $val =~ /^\\/
!ll&&!rl&&r
0900
MC/DC0%0/2
defined $val and $val =~ /^\\/
defined $val $val =~ /^\\/
Truth table33%1/3
A and B
A= defined $valB= $val =~ /^\\/
ABresult
0X0
100
111
223
224 90 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) { } 0 90 90
225 0 ▶ if ($ref eq '@') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq '@') { } 0 0 0
elsif ($ref eq '%') { } 0 0 0
226 0 ▶ $conf->{$pkg}->{$key} ||= [];
Condition0%0/2
$conf->{$pkg}{$key} ||= []
l!l
00
MC/DC0%0/1
$conf->{$pkg}{$key} ||= []
$conf->{$pkg}{$key}
Truth table0%0/2
A
A= $conf->{$pkg}{$key}
Aresult
11
00
227 0 push @{$conf->{$pkg}->{$key}}, $val;
228 } elsif ($ref eq '%') {
229 0 ▶ $conf->{$pkg}->{$key} ||= {};
Condition0%0/2
$conf->{$pkg}{$key} ||= {}
l!l
00
MC/DC0%0/1
$conf->{$pkg}{$key} ||= {}
$conf->{$pkg}{$key}
Truth table0%0/2
A
A= $conf->{$pkg}{$key}
Aresult
11
00
230 0 my ($k, $v) = split(/\s*=\s*/, $val);
231 0 $conf->{$pkg}->{$key}->{$k} = $v;
232 }
233 }
234 else {
235 90 $conf->{$pkg}->{$key} = $val;
236 };
237 }; #end while
238 9 close $handle;
239
240 #this is an irritating hack. In order to notify, types needs to be defined
241 #and chances are, this is the first place we'd define it due to circular inheritance
242 #issues.
243
244 9 ▶ unless (! $class->loaded->{$conf_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->loaded->{$conf_file}) 0 9 9
245 0 ▶ Basset::Object->add_trickle_class_attr('types')
Branch: 1 branch
BranchTrueFalseTotal
unless "Basset::Object"->can("types") 0 0 0
246 unless Basset::Object->can('types');
247 0 $class->notify('ConfFileReRead', $conf_file);
248 }
249  
250 9 ▶ $class->loaded->{$conf_file} = -M $conf_file unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 9 0 9
251 }; #end if
252 }; #end foreach
253 773 return $conf;
254
255 }; #end sub
256  
257 9 1;
258  
259 __END__
« Machine.pm ↑ Summary Test/More.pm »