blib/lib/Config/Simple.pm

filter: Statementstmt 77.3% 307 / 397 Branchbran 66.7% 175 / 262 Conditioncond 65.9% 31 / 47 MC/DCmcdc 37.9% 11 / 29 Subroutinesub 92.8% 39 / 42 Podpod 53.8% 14 / 26 totaltotal 71.8% 577 / 803 CC 153 SCAR 26.6
CC
153
Cov
73%
parse_http_file
110.0
param
17.8
guess_syntax
17.3
CRAP 14.3
↑ Summary
1 package Config::Simple;
2  
3 # $Id: Simple.pm,v 3.55 2005/02/10 18:57:16 sherzodr Exp $
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # uncomment the following line while debugging. Otherwise,
7 # it's too slow for production environment
8 #use diagnostics;
9 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Fcntl qw(:DEFAULT :flock);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Text::ParseWords 'parse_line';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use vars qw($VERSION $DEFAULTNS $LC $USEQQ $errstr);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use AutoLoader 'AUTOLOAD';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15  
16 6 $VERSION = '4.58';
17 7 $DEFAULTNS = 'default';
18  
19 4 ▶sub import {
Subroutine import: called CC=4 SCAR=15.2
20 4 my $class = shift;
21 4 for ( @_ ) {
22 0 ▶ if ( $_ eq '-lc' ) { $LC = 1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "-lc") 0 3 3
23 3 ▶ if ( $_ eq '-strict' ) { $USEQQ = 1; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "-strict") 3 0 3
24 }
25 }
26  
27  
28  
29 # delimiter used by Text::ParseWords::parse_line()
30 165 ▶sub READ_DELIM () { return '\s*,\s*' }
Subroutine READ_DELIM: called CC=1 SCAR=0.0
Pod undocumented
31 # delimiter used by as_string()
32 69 ▶sub WRITE_DELIM() { return ', ' }
Subroutine WRITE_DELIM: called CC=1 SCAR=0.0
Pod undocumented
33 sub DEBUG () { 0 }
34  
35  
36 12 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
37 12 my $class = shift;
38 12 ▶ $class = ref($class) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
39  
40 12 my $self = {
41 _FILE_HANDLE => undef, # holds a reference to an opened cfg file
42 _FILE_NAME => undef, # holds the name of the read configuration file
43 _STACK => [], # currently not implemented
44 _DATA => {}, # actual key/value pairs are stored in _DATA
45 _SYNTAX => undef, # holds the syntax of the read cfg file
46 _SUB_SYNTAX => undef, # holds the sub-syntax (like for simplified ini)
47 _ARGS => {}, # holds all key/values passed to new()
48 _OO_INTERFACE => 1, # currently not implemented
49 _IS_MODIFIED => 0, # to prevent writing file back if they were not modified
50 };
51 12 bless ($self, $class);
52 12 ▶ $self->_init(@_) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init(@_) or return 11 1 12
53 11 return $self;
54 }
55  
56  
57  
58  
59 12 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
60 12 my $self = shift;
61
62 # if it was an auto save mode, write the changes
63 # back only if the values have been modified.
64 12 ▶ if ( $self->autosave() && $self->_is_modified() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autosave and $self->_is_modified) 1 11 12
Condition66%2/3
$self->autosave and $self->_is_modified
!ll&&!rl&&r
1101
MC/DC50%1/2
$self->autosave and $self->_is_modified
$self->autosave $self->_is_modified
Truth table66%2/3
A and B
A= $self->autosaveB= $self->_is_modified
ABresult
0X0
100
111
65 1 $self->write();
66 }
67 }
68  
69  
70  
71  
72 # initialize the object
73 12 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.3
74 12 my $self = shift;
75  
76 12 ▶ if ( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 5 7 12
elsif (@_ % 2) { } 0 7 7
77 5 return $self->read($_[0]);
78 } elsif ( @_ % 2 ) {
79 0 croak "new(): Illegal arguments detected";
80 } else {
81 7 $self->{_ARGS} = { @_ };
82 }
83 # if syntax was given, call syntax()
84 7 ▶ if ( exists $self->{_ARGS}->{syntax} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ARGS"}{"syntax"}) 1 6 7
85 1 $self->syntax($self->{_ARGS}->{syntax});
86 }
87 # if autosave was set, call autosave
88 7 ▶ if ( exists $self->{_ARGS}->{autosave} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ARGS"}{"autosave"}) 0 7 7
89 0 $self->autosave($self->{_ARGS}->{autosave});
90 }
91 # If filename was passed, call read()
92 7 ▶ if ( exists ($self->{_ARGS}->{filename}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_ARGS"}{"filename"}) 1 6 7
93 1 return $self->read( $self->{_ARGS}->{filename} );
94 }
95 6 return 1;
96 }
97  
98  
99  
100 21 ▶sub _is_modified {
Subroutine _is_modified: called CC=2 SCAR=6.9
101 21 my ($self, $bool) = @_;
102  
103 21 ▶ if ( defined $bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bool) 20 1 21
104 20 $self->{_IS_MODIFIED} = $bool;
105 }
106 21 return $self->{_IS_MODIFIED};
107 }
108  
109  
110  
111 13 ▶sub autosave {
Subroutine autosave: called CC=2 SCAR=6.9
Pod documented
112 13 my ($self, $bool) = @_;
113  
114 13 ▶ if ( defined $bool ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bool) 1 12 13
115 1 $self->{_ARGS}->{autosave} = $bool;
116 }
117 13 return $self->{_ARGS}->{autosave};
118 }
119  
120  
121 6 ▶sub syntax {
Subroutine syntax: called CC=2 SCAR=6.9
Pod undocumented
122 6 my ($self, $syntax) = @_;
123  
124 6 ▶ if ( defined $syntax ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $syntax) 1 5 6
125 1 $self->{_SYNTAX} = $syntax;
126 }
127 6 return $self->{_SYNTAX};
128 }
129  
130  
131 # takes a filename or a file handle and returns a filehandle
132 21 ▶sub _get_fh {
Subroutine _get_fh: called CC=6 SCAR=18.6
133 21 my ($self, $arg, $mode) = @_;
134
135 21 ▶ unless ( defined $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $arg) 0 21 21
136 0 croak "_get_fh(): filename is missing";
137 }
138 21 ▶ if ( ref($arg) && (ref($arg) eq 'GLOB') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg and ref $arg eq "GLOB") 2 19 21
Condition66%2/3
ref $arg and ref $arg eq "GLOB"
!ll&&!rl&&r
1902
MC/DC50%1/2
ref $arg and ref $arg eq "GLOB"
ref $arg ref $arg eq "GLOB"
Truth table66%2/3
A and B
A= ref $argB= ref $arg eq "GLOB"
ABresult
0X0
100
111
139 2 return ($arg, 0);
140 }
141 19 ▶ unless ( defined $mode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mode) 0 19 19
142 0 $mode = O_RDONLY;
143 }
144 19 ▶ unless ( sysopen(FH, $arg, $mode) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen FH, $arg, $mode) 2 17 19
145 2 $self->error("couldn't open $arg: $!");
146 2 return undef;
147 }
148 17 return (\*FH, 1);
149 }
150  
151  
152  
153 11 ▶sub read {
Subroutine read: called CC=9 SCAR=25.4
Pod documented
154 11 my ($self, $file) = @_;
155
156 # making sure one object doesn't work on more than one
157 # file at a time
158 11 ▶ if ( defined $self->{_FILE_HANDLE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_FILE_HANDLE"}) 0 11 11
159 0 croak "Open file handle detected. If you're trying to parse another file, close() it first.";
160 }
161 11 ▶ unless ( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 11 11
162 0 croak "Usage: OBJ->read(\$file_name)";
163 }
164
165 11 $self->{_FILE_NAME} = $file;
166 11 ▶ $self->{_FILE_HANDLE} = $self->_get_fh($file, O_RDONLY) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_FILE_HANDLE"} = $self->_get_fh($file, 0) or return undef 9 2 11
167
168 9 ▶ $self->{_SYNTAX} = $self->guess_syntax(\*FH) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_SYNTAX"} = $self->guess_syntax(\*FH) or return undef 9 0 9
169  
170 # call respective parsers
171  
172 9 ▶ if ( $self->{_SYNTAX} eq 'ini' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'_SYNTAX'} eq 'ini') { } 7 2 9
elsif ($self->{'_SYNTAX'} eq 'simple') { } 2 0 2
elsif ($self->{'_SYNTAX'} eq 'http') { } 0 0 0
173 7 $self->{_DATA} = $self->parse_ini_file($file);
174 } elsif ( $self->{_SYNTAX} eq 'simple' ) {
175 2 $self->{_DATA} = $self->parse_cfg_file(\*FH);
176 } elsif ( $self->{_SYNTAX} eq 'http' ) {
177 0 $self->{_DATA} = $self->parse_http_file(\*FH);
178 }
179  
180 9 ▶ if ( $self->{_DATA} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_DATA"}) 9 0 9
181 9 return $self->{_DATA};
182 }
183  
184 0 die "Something went wrong. No supported configuration file syntax found";
185 }
186  
187  
188 0 ▶sub close {
Subroutine close: not called CC=3 SCAR=24.8
Pod undocumented
189 0 my $self = shift;
190  
191 0 ▶ my $fh = $self->{_FILE_HANDLE} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->{"_FILE_HANDLE"} or return 0 0 0
192 0 ▶ unless ( close($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::close $fh) 0 0 0
193 0 $self->error("couldn't close the file: $!");
194 0 return undef;
195 }
196 0 return 1;
197 }
198  
199  
200  
201  
202  
203 # tries to guess the syntax of the configuration file.
204 # returns 'ini', 'simple' or 'http'.
205 9 ▶sub guess_syntax {
Subroutine guess_syntax: called CC=12 SCAR=28.5
Pod undocumented
206 9 my ($self, $fh) = @_;
207  
208 9 ▶ unless ( defined $fh ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 9 9
209 0 ▶ $fh = $self->{_FILE_HANDLE} or die "'_FILE_HANDLE' is not defined";
Branch: 1 branch
BranchTrueFalseTotal
$fh = $self->{"_FILE_HANDLE"} or die "'_FILE_HANDLE' is not defined" 0 0 0
210 }
211 9 ▶ unless ( seek($fh, 0, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0) 0 9 9
212 0 $self->error("Couldn't seek($fh, 0, 0): $!");
213 0 return undef;
214 }
215  
216 # now we keep reading the file line by line untill we can identify the
217 # syntax
218 9 verbose("Trying to guess the file syntax...");
219 9 my ($syntax, $sub_syntax);
220 9 ▶ while ( <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 36 0 36
221 # skipping empty lines and comments. They don't tell much anyway
222 36 ▶ /^(\n|\#|;)/ and next;
Branch: 1 branch
BranchTrueFalseTotal
/^(\n|\#|;)/ and next 27 9 36
223  
224 # If there's no alpha-numeric value in this line, ignore it
225 9 ▶ /\w/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/\w/ or next 9 0 9
226  
227 # trim $/
228 9 chomp();
229  
230 # If there's a block, it is an ini syntax
231 9 ▶ /^\s*\[\s*[^\]]+\s*\]\s*$/ and $syntax = 'ini', last;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*\[\s*[^\]]+\s*\]\s*$/ and $syntax = "ini", last 6 3 9
232  
233 # If we can read key/value pairs separated by '=', it still
234 # is an ini syntax with a default block assumed
235 3 ▶ /^\s*[^=]+\s*=\s*.*\s*$/ and $syntax = 'ini', $self->{_SUB_SYNTAX} = 'simple-ini', last;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*[^=]+\s*=\s*.*\s*$/ and $syntax = "ini", $self->{"_SUB_SYNTAX"} = "simple-ini", last 1 2 3
236  
237 # If we can read key/value pairs separated by ':', it is an
238 # http syntax
239 2 ▶ /^\s*[\w-]+\s*:\s*.*\s*$/ and $syntax = 'http', last;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*[\w-]+\s*:\s*.*\s*$/ and $syntax = "http", last 0 2 2
240  
241 # If we can read key/value pairs separated by just whites,
242 # it is a simple syntax.
243 2 ▶ /^\s*[\w-]+\s+.*$/ and $syntax = 'simple', last;
Branch: 1 branch
BranchTrueFalseTotal
/^\s*[\w-]+\s+.*$/ and $syntax = "simple", last 2 0 2
244 }
245  
246 9 ▶ if ( $syntax ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax) 9 0 9
247 9 return $syntax;
248 }
249  
250 0 $self->error("Couldn't identify the syntax used");
251 0 return undef;
252  
253 }
254  
255  
256  
257  
258  
259 8 ▶sub parse_ini_file {
Subroutine parse_ini_file: called CC=11 SCAR=27.0
Pod undocumented
260 8 my ($class, $file) = @_;
261  
262 8 ▶ my ($fh, $close_fh) = $class->_get_fh($file, O_RDONLY) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $class->_get_fh($file, 0) or return 8 0 8
263 8 ▶ unless(flock($fh, LOCK_SH) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 1) 0 8 8
264 0 $errstr = "couldn't acquire shared lock on $fh: $!";
265 0 return undef;
266 }
267
268 8 ▶ unless ( seek($fh, 0, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0) 0 8 8
269 0 $errstr = "couldn't seek to the beginning of the file: $!";
270 0 return undef;
271 }
272  
273 8 my $bn = $DEFAULTNS;
274 8 my %data = ();
275 8 my $line;
276 8 ▶ while ( defined($line=<$fh>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 235 8 243
277 # skipping comments and empty lines:
278  
279 235 ▶ $line =~ /^\s*(\n|\#|;)/ and next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*(\n|\#|;)/ and next 63 172 235
280 172 ▶ $line =~ /\S/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\S/ or next 172 0 172
281  
282 172 chomp $line;
283
284 172 $line =~ s/^\s+//g;
285 172 $line =~ s/\s+$//g;
286
287 # parsing the block name:
288 172 ▶ $line =~ /^\s*\[\s*([^\]]+)\s*\]$/ and $bn = lcase($1), next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*\[\s*([^\]]+)\s*\]$/ and $bn = lcase($1), next 30 142 172
289 # parsing key/value pairs
290 142 ▶ $line =~ /^\s*([^=]*\w)\s*=\s*(.*)\s*$/ and $data{$bn}->{lcase($1)}=[parse_line(READ_DELIM, 0, $2)], next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*([^=]*\w)\s*=\s*(.*)\s*$/ and $data{$bn}{lcase($1)} = [parse_line(&READ_DELIM(), 0, $2)], next 142 0 142
291 # if we came this far, the syntax couldn't be validated:
292 0 $errstr = "syntax error on line $. '$line'";
293 0 return undef;
294 }
295 8 ▶ unless(flock($fh, LOCK_UN) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 8) 0 8 8
296 0 $errstr = "couldn't unlock file: $!";
297 0 return undef;
298 }
299 8 ▶ if ( $close_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_fh) 8 0 8
300 8 CORE::close($fh);
301 }
302 8 return \%data;
303 }
304  
305  
306 249 ▶sub lcase {
Subroutine lcase: called CC=2 SCAR=8.1
Pod undocumented
307 249 my $str = shift;
308 249 ▶ $LC or return $str;
Branch: 1 branch
BranchTrueFalseTotal
$Config::Simple::LC or return $str 0 249 249
309 0 return lc($str);
310 }
311  
312  
313  
314  
315 2 ▶sub parse_cfg_file {
Subroutine parse_cfg_file: called CC=10 SCAR=26.8
Pod undocumented
316 2 my ($class, $file) = @_;
317  
318 2 ▶ my ($fh, $close_fh) = $class->_get_fh($file, O_RDONLY) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $class->_get_fh($file, 0) or return 2 0 2
319
320 2 ▶ unless ( flock($fh, LOCK_SH) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 1) 0 2 2
321 0 $errstr = "couldn't get shared lock on $fh: $!";
322 0 return undef;
323 }
324  
325 2 ▶ unless ( seek($fh, 0, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0) 0 2 2
326 0 $errstr = "couldn't seek to the start of the file: :$!";
327 }
328  
329 2 my %data = ();
330 2 my $line;
331 2 ▶ while ( defined($line=<$fh>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 31 2 33
332 # skipping comments and empty lines:
333 31 ▶ $line =~ /^(\n|\#)/ and next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^(\n|\#)/ and next 8 23 31
334 23 ▶ $line =~ /\S/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\S/ or next 23 0 23
335 23 chomp $line;
336 23 $line =~ s/^\s+//g;
337 23 $line =~ s/\s+$//g;
338 # parsing key/value pairs
339 23 ▶ $line =~ /^\s*([\w-]+)\s+(.*)\s*$/ and $data{lcase($1)}=[parse_line(READ_DELIM, 0, $2)], next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*([\w-]+)\s+(.*)\s*$/ and $data{lcase($1)} = [parse_line(&READ_DELIM(), 0, $2)], next 23 0 23
340 # if we came this far, the syntax couldn't be validated:
341 0 $errstr = "syntax error on line $.: '$line'";
342 0 return undef;
343 }
344 2 ▶ unless ( flock($fh, LOCK_UN) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 8) 0 2 2
345 0 $errstr = "couldn't unlock the file: $!";
346 0 return undef;
347 }
348
349 2 ▶ if ( $close_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_fh) 0 2 2
350 0 CORE::close($fh);
351 }
352 2 return \%data;
353 }
354  
355  
356  
357 0 ▶sub parse_http_file {
Subroutine parse_http_file: not called CC=10 SCAR=47.0
Pod undocumented
358 0 my ($class, $file) = @_;
359  
360 0 ▶ my ($fh, $close_fh) = $class->_get_fh($file, O_RDONLY) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $class->_get_fh($file, 0) or return 0 0 0
361 0 ▶ unless ( flock($fh, LOCK_SH) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 1) 0 0 0
362 0 $errstr = "couldn't get shared lock on file: $!";
363 0 return undef;
364 }
365  
366 0 ▶ unless( seek($fh, 0, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0) 0 0 0
367 0 $errstr = "couldn't seek to the start of the file: $!";
368 0 return undef;
369 }
370 0 my %data = ();
371 0 my $line;
372 0 ▶ while ( defined($line= <$fh>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
373 # skipping comments and empty lines:
374 0 ▶ $line =~ /^(\n|\#)/ and next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^(\n|\#)/ and next 0 0 0
375 0 ▶ $line =~ /\S/ or next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /\S/ or next 0 0 0
376 # stripping $/:
377 0 chomp $line;
378 0 $line =~ s/^\s+//g;
379 0 $line =~ s/\s+$//g;
380 # parsing key/value pairs:
381 0 ▶ $line =~ /^\s*([\w-]+)\s*:\s*(.*)$/ and $data{lcase($1)}=[parse_line(READ_DELIM, 0, $2)], next;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^\s*([\w-]+)\s*:\s*(.*)$/ and $data{lcase($1)} = [parse_line(&READ_DELIM(), 0, $2)], next 0 0 0
382 # if we came this far, the syntax couldn't be validated:
383 0 $errstr = "syntax error on line $.: '$line'";
384 0 return undef;
385 }
386 0 ▶ unless ( flock($fh, LOCK_UN) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock $fh, 8) 0 0 0
387 0 $errstr = "couldn't unlock file: $!";
388 0 return undef;
389 }
390 0 ▶ if ( $close_fh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($close_fh) 0 0 0
391 0 CORE::close($fh);
392 }
393 0 return \%data;
394 }
395  
396  
397 58 ▶sub param {
Subroutine param: called CC=16 SCAR=28.8
Pod documented
398 58 my $self = shift;
399  
400 # If called with no arguments, return all the
401 # possible keys
402 58 ▶ unless ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 1 57 58
403 1 my $vars = $self->vars();
404 1 return keys %$vars;
405 }
406 # if called with a single argument, return the value
407 # matching this key
408 57 ▶ if ( @_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 39 18 57
409 39 return $self->get_param(@_);
410 }
411 # if we come this far, we were called with multiple
412 # arguments. Go figure!
413 18 my $args = {
414 '-name', undef,
415 '-value', undef,
416 '-values', undef,
417 '-block', undef,
418 @_
419 };
420 18 ▶ if ( defined $args->{'-name'} && (defined($args->{'-value'}) || defined($args->{'-values'})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"-name"} and defined $args->{"-value"} || defined $args->{"-values"}) 4 14 18
Condition66%4/6
defined $args->{"-name"} and defined $args->{"-value"} || defined $args->{"-values"}
!ll&&!rl&&r
1404
defined $args->{"-value"} or defined $args->{"-values"}
l!l&&r!l&&!r
220
MC/DC33%1/3
defined $args->{"-name"} and defined $args->{"-value"} || defined $args->{"-values"}
defined $args->{"-name"} defined $args->{"-value"} defined $args->{"-values"}
Truth table75%3/4
A and B or C
A= defined $args->{"-name"}B= defined $args->{"-value"}C= defined $args->{"-values"}
ABCresult
0XX0
1000
11X1
1011
421 # OBJ->param(-name=>'..', -value=>'...') syntax:
422 4 ▶ return $self->set_param($args->{'-name'}, $args->{'-value'}||$args->{'-values'});
Condition66%2/3
$args->{'-value'} || $args->{'-values'}
l!l&&r!l&&!r
220
MC/DC0%0/2
$args->{'-value'} || $args->{'-values'}
$args->{'-value'} $args->{'-values'}
Truth table66%2/3
A || B
A= $args->{'-value'}B= $args->{'-values'}
ABresult
1X1
011
000
423  
424 }
425 14 ▶ if ( defined($args->{'-name'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"-name"}) 0 14 14
426 # OBJ->param(-name=>'...') syntax:
427 0 return $self->get_param($args->{'-name'});
428
429 }
430 14 ▶ if ( defined($args->{'-block'}) && (defined($args->{'-values'}) || defined($args->{'-value'})) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"-block"} and defined $args->{"-values"} || defined $args->{"-value"}) 1 13 14
Condition83%5/6
defined $args->{"-block"} and defined $args->{"-values"} || defined $args->{"-value"}
!ll&&!rl&&r
1121
defined $args->{"-values"} or defined $args->{"-value"}
l!l&&r!l&&!r
012
MC/DC66%2/3
defined $args->{"-block"} and defined $args->{"-values"} || defined $args->{"-value"}
defined $args->{"-block"} defined $args->{"-values"} defined $args->{"-value"}
Truth table75%3/4
A and B or C
A= defined $args->{"-block"}B= defined $args->{"-values"}C= defined $args->{"-value"}
ABCresult
0XX0
1000
11X1
1011
431 1 ▶ return $self->set_block($args->{'-block'}, $args->{'-values'}||$args->{'-value'});
Condition33%1/3
$args->{'-values'} || $args->{'-value'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{'-values'} || $args->{'-value'}
$args->{'-values'} $args->{'-value'}
Truth table33%1/3
A || B
A= $args->{'-values'}B= $args->{'-value'}
ABresult
1X1
011
000
432 }
433 13 ▶ if ( defined($args->{'-block'}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"-block"}) 2 11 13
434 2 return $self->get_block($args->{'-block'});
435 }
436
437 11 ▶ if ( @_ % 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 11 11
438 0 croak "param(): illegal syntax";
439 }
440 11 my $nset = 0;
441 11 ▶ for ( my $i = 0; $i < @_; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @_) 11 11 22
442 11 ▶ $self->set_param($_[$i], $_[$i+1]) && $nset++;
Branch: 1 branch
BranchTrueFalseTotal
$self->set_param($_[$i], $_[$i + 1]) and ++$nset 11 0 11
443 }
444 11 return $nset;
445 }
446  
447  
448  
449  
450 39 ▶sub get_param {
Subroutine get_param: called CC=10 SCAR=23.3
Pod undocumented
451 39 my ($self, $arg) = @_;
452  
453 39 ▶ unless ( $arg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg) 0 39 39
454 0 croak "Usage: OBJ->get_param(\$key)";
455 }
456 39 $arg = lcase($arg);
457 39 ▶ my $syntax = $self->{_SYNTAX} or die "'_SYNTAX' is undefined";
Branch: 1 branch
BranchTrueFalseTotal
my $syntax = $self->{"_SYNTAX"} or die "'_SYNTAX' is undefined" 39 0 39
458 # If it was an ini-style, we should first
459 # split the argument into its block name and key
460 # components:
461 39 my $rv = undef;
462 39 ▶ if ( $syntax eq 'ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax eq 'ini') { } 38 1 39
463 38 my ($block_name, $key) = $arg =~ m/^([^\.]+)\.(.*)$/;
464 38 ▶ if ( defined($block_name) && defined($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $block_name and defined $key) { } 37 1 38
Condition66%2/3
defined $block_name and defined $key
!ll&&!rl&&r
1037
MC/DC50%1/2
defined $block_name and defined $key
defined $block_name defined $key
Truth table66%2/3
A and B
A= defined $block_nameB= defined $key
ABresult
0X0
100
111
465 37 $rv = $self->{_DATA}->{$block_name}->{$key};
466 } else {
467 1 $rv = $self->{_DATA}->{$DEFAULTNS}->{$arg};
468 }
469 } else {
470 1 $rv = $self->{_DATA}->{$arg};
471 }
472  
473 39 ▶ defined($rv) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return 39 0 39
474  
475 39 ▶ for ( my $i=0; $i < @$rv; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rv) 43 39 82
476 43 $rv->[$i] =~ s/\\n/\n/g;
477 }
478 39 ▶ return @$rv==1 ? $rv->[0] : (wantarray ? @$rv : $rv);
Branch: 2 branches
BranchTrueFalseTotal
@$rv == 1 ? : 33 6 39
wantarray ? : 1 5 6
479 }
480  
481  
482  
483  
484 3 ▶sub get_block {
Subroutine get_block: called CC=5 SCAR=16.1
Pod documented
485 3 my ($self, $block_name) = @_;
486  
487 3 ▶ unless ( $self->syntax() eq 'ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->syntax eq "ini") 0 3 3
488 0 croak "get_block() is supported only in 'ini' files";
489 }
490 3 ▶ unless ( defined $block_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $block_name) 1 2 3
491 1 return keys %{$self->{_DATA}};
492 }
493 2 my $rv = {};
494 2 ▶ while ( my ($k, $v) = each %{$self->{_DATA}->{$block_name}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_DATA"}{$block_name};}) 8 2 10
495 8 $v =~ s/\\n/\n/g;
496 8 ▶ $rv->{$k} = $v->[1] ? $v : $v->[0];
Branch: 1 branch
BranchTrueFalseTotal
$v->[1] ? : 2 6 8
497 }
498 2 return $rv;
499 }
500  
501  
502  
503  
504  
505 1 ▶sub set_block {
Subroutine set_block: called CC=4 SCAR=13.9
Pod documented
506 1 my ($self, $block_name, $values) = @_;
507  
508 1 ▶ unless ( $self->syntax() eq 'ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->syntax eq "ini") 0 1 1
509 0 croak "set_block() is supported only in 'ini' files";
510 }
511 1 my $processed_values = {};
512 1 ▶ while ( my ($k, $v) = each %$values ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$values) 4 1 5
513 4 $v =~ s/\n/\\n/g;
514 4 ▶ $processed_values->{$k} = (ref($v) eq 'ARRAY') ? $v : [$v];
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 1 3 4
515 4 $self->_is_modified(1);
516 }
517  
518 1 $self->{_DATA}->{$block_name} = $processed_values;
519 1 $self->_is_modified(1);
520 }
521  
522  
523  
524  
525  
526 15 ▶sub set_param {
Subroutine set_param: called CC=8 SCAR=20.8
Pod undocumented
527 15 my ($self, $key, $value) = @_;
528  
529 15 ▶ my $syntax = $self->{_SYNTAX} or die "'_SYNTAX' is not defined";
Branch: 1 branch
BranchTrueFalseTotal
my $syntax = $self->{"_SYNTAX"} or die "'_SYNTAX' is not defined" 15 0 15
530 15 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 2 13 15
531 2 ▶ for (my $i=0; $i < @$value; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$value) 4 2 6
532 4 $value->[$i] =~ s/\n/\\n/g;
533 }
534 } else {
535 13 $value =~ s/\n/\\n/g;
536 }
537 15 ▶ unless ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value eq "ARRAY") 13 2 15
538 13 $value = [$value];
539 }
540 15 $key = lcase($key);
541 # If it was an ini syntax, we should first split the $key
542 # into its block_name and key components
543 15 ▶ if ( $syntax eq 'ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax eq "ini") 12 3 15
544 12 my ($bn, $k) = $key =~ m/^([^\.]+)\.(.*)$/;
545 12 ▶ if ( $bn && $k ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bn and $k) 11 1 12
Condition66%2/3
$bn and $k
!ll&&!rl&&r
1011
MC/DC50%1/2
$bn and $k
$bn $k
Truth table66%2/3
A and B
A= $bnB= $k
ABresult
0X0
100
111
546 11 $self->_is_modified(1);
547 11 return $self->{_DATA}->{$bn}->{$k} = $value;
548 }
549 # most likely the user is assuming default name space then?
550 # Let's hope!
551 1 $self->_is_modified(1);
552 1 return $self->{_DATA}->{$DEFAULTNS}->{$key} = $value;
553 }
554 3 $self->_is_modified(1);
555 3 return $self->{_DATA}->{$key} = $value;
556 }
557  
558  
559  
560  
561  
562  
563  
564  
565 6 ▶sub write {
Subroutine write: called CC=7 SCAR=25.3
Pod documented
566 6 my ($self, $file) = @_;
567  
568 6 ▶ $file ||= $self->{_FILE_NAME} or die "Neither '_FILE_NAME' nor \$filename defined";
Branch: 1 branch
BranchTrueFalseTotal
$file ||= $self->{"_FILE_NAME"} or die "Neither '_FILE_NAME' nor \$filename defined" 6 0 6
Condition66%2/3
$file ||= $self->{"_FILE_NAME"}
l!l&&r!l&&!r
150
MC/DC0%0/2
$file ||= $self->{"_FILE_NAME"}
$file $self->{"_FILE_NAME"}
Truth table66%2/3
A ||= B
A= $fileB= $self->{"_FILE_NAME"}
ABresult
1X1
011
000
569  
570 6 ▶ unless ( sysopen(FH, $file, O_WRONLY|O_CREAT, 0666) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysopen FH, $file, 65, 438) 0 6 6
571 0 $self->error("'$file' couldn't be opened for writing: $!");
572 0 return undef;
573 }
574 6 ▶ unless ( flock(FH, LOCK_EX) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (flock FH, 2) 0 6 6
575 0 $self->error("'$file' couldn't be locked: $!");
576 0 return undef;
577 }
578 6 ▶ unless ( truncate(FH, 0) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (truncate FH, 0) 0 6 6
579 0 $self->error("'$file' couldn't be truncated: $!");
580 0 return undef;
581 }
582 6 print FH $self->as_string();
583 6 ▶ unless ( CORE::close(FH) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::close FH) 0 6 6
584 0 $self->error("Couldn't write into '$file': $!");
585 0 return undef;
586 }
587 6 return 1;
588 }
589  
590  
591  
592 1 ▶sub save {
Subroutine save: called CC=1 SCAR=0.0
Pod documented
593 1 my $self = shift;
594 1 return $self->write(@_);
595 }
596  
597  
598 # generates a writable string
599 6 ▶sub as_string {
Subroutine as_string: called CC=11 SCAR=25.1
Pod documented
600 6 my $self = shift;
601  
602 6 ▶ my $syntax = $self->{_SYNTAX} or die "'_SYNTAX' is not defined";
Branch: 1 branch
BranchTrueFalseTotal
my $syntax = $self->{"_SYNTAX"} or die "'_SYNTAX' is not defined" 6 0 6
603 6 ▶ my $sub_syntax = $self->{_SUB_SYNTAX} || '';
Condition100%2/2
$self->{'_SUB_SYNTAX'} || ''
l!l
15
MC/DC100%1/1
$self->{'_SUB_SYNTAX'} || ''
$self->{'_SUB_SYNTAX'}
Truth table100%2/2
A
A= $self->{'_SUB_SYNTAX'}
Aresult
11
00
604 6 my $currtime = localtime;
605 6 my $STRING = undef;
606 6 ▶ if ( $syntax eq 'ini' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($syntax eq 'ini') { } 4 2 6
elsif ($syntax eq 'http') { } 0 2 2
elsif ($syntax eq 'simple') { } 2 0 2
607 4 $STRING .= "; Config::Simple $VERSION\n";
608 4 $STRING .= "; $currtime\n\n";
609 4 ▶ while ( my ($block_name, $key_values) = each %{$self->{_DATA}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_DATA"};}) 11 4 15
610 11 ▶ unless ( $sub_syntax eq 'simple-ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sub_syntax eq "simple-ini") 10 1 11
611 10 $STRING .= sprintf("[%s]\n", $block_name);
612 }
613 11 ▶ while ( my ($key, $value) = each %{$key_values} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$key_values;}) 46 11 57
614 46 my $values = join (WRITE_DELIM, map { quote_values($_) } @$value);
615 46 $STRING .= sprintf("%s=%s\n", $key, $values );
616 }
617 11 $STRING .= "\n";
618 }
619 } elsif ( $syntax eq 'http' ) {
620 0 $STRING .= "# Config::Simple $VERSION\n";
621 0 $STRING .= "# $currtime\n\n";
622 0 ▶ while ( my ($key, $value) = each %{$self->{_DATA}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_DATA"};}) 0 0 0
623 0 my $values = join (WRITE_DELIM, map { quote_values($_) } @$value);
624 0 $STRING .= sprintf("%s: %s\n", $key, $values);
625 }
626 } elsif ( $syntax eq 'simple' ) {
627 2 $STRING .= "# Config::Simple $VERSION\n";
628 2 $STRING .= "# $currtime\n\n";
629 2 ▶ while ( my ($key, $value) = each %{$self->{_DATA}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"_DATA"};}) 23 2 25
630 23 my $values = join (WRITE_DELIM, map { quote_values($_) } @$value);
631 23 $STRING .= sprintf("%s %s\n", $key, $values);
632 }
633 }
634 6 $STRING .= "\n";
635 6 return $STRING;
636 }
637  
638  
639  
640  
641  
642 # quotes each value before saving into file
643 76 ▶sub quote_values {
Subroutine quote_values: called CC=4 SCAR=13.9
Pod undocumented
644 76 my $string = shift;
645  
646 0 ▶ if ( ref($string) ) { $string = $_[0] }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $string) 0 76 76
647 76 $string =~ s/\\/\\\\/g;
648  
649 76 ▶ if ( $USEQQ && ($string =~ m/\W/) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::Simple::USEQQ and $string =~ /\W/) 12 64 76
Condition100%3/3
$Config::Simple::USEQQ and $string =~ /\W/
!ll&&!rl&&r
57712
MC/DC100%2/2
$Config::Simple::USEQQ and $string =~ /\W/
$Config::Simple::USEQQ $string =~ /\W/
Truth table100%3/3
A and B
A= $Config::Simple::USEQQB= $string =~ /\W/
ABresult
0X0
100
111
650 12 $string =~ s/"/\\"/g;
651 12 $string =~ s/\n/\\n/g;
652 12 return sprintf("\"%s\"", $string);
653 }
654 64 return $string;
655 }
656  
657  
658  
659 # deletes a variable
660 1 ▶sub delete {
Subroutine delete: called CC=5 SCAR=18.7
Pod documented
661 1 my ($self, $key) = @_;
662  
663 1 ▶ my $syntax = $self->syntax() or die "No 'syntax' is defined";
Branch: 1 branch
BranchTrueFalseTotal
my $syntax = $self->syntax or die "No 'syntax' is defined" 1 0 1
664 1 ▶ if ( $syntax eq 'ini' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($syntax eq "ini") 1 0 1
665 1 my ($bn, $k) = $key =~ m/([^\.]+)\.(.*)/;
666 1 ▶ if ( defined($bn) && defined($k) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bn and defined $k) { } 1 0 1
Condition33%1/3
defined $bn and defined $k
!ll&&!rl&&r
001
MC/DC0%0/2
defined $bn and defined $k
defined $bn defined $k
Truth table33%1/3
A and B
A= defined $bnB= defined $k
ABresult
0X0
100
111
667 1 delete $self->{_DATA}->{$bn}->{$k};
668 } else {
669 0 delete $self->{_DATA}->{$DEFAULTNS}->{$key};
670 }
671 1 return 1;
672 }
673 0 delete $self->{_DATA}->{$key};
674 }
675  
676  
677  
678 # clears the '_DATA' entirely.
679 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
680 0 my $self = shift;
681 0 map { $self->delete($_) } $self->param;
682 }
683  
684  
685  
686  
687 6 1;
688 __END__;
↑ Summary