OpenInteract2/Config/Ini.pm

filter: Statementstmt 83.4% 207 / 248 Branchbran 72.1% 75 / 104 Conditioncond 53.0% 26 / 49 MC/DCmcdc 22.2% 6 / 27 Subroutinesub 83.3% 25 / 30 Podpod 0.0% 0 / 13 totaltotal 71.9% 339 / 471 CC 82 SCAR 24.5
CC
82
Cov
77%
get
30.0
_translate_ini
26.5
write_file
18.3
CRAP 11.5
« OpenInteract2/Manage/Website/ThemeInstall.pm ↑ Summary OpenInteract2/Response/Standalone.pm »
1 package OpenInteract2::Config::Ini;
2  
3 # $Id: Ini.pm,v 1.21 2005/03/18 04:09:50 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use File::Basename qw( dirname );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use File::Spec::Functions qw( catfile );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use OpenInteract2::Context qw( CTX );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 87 ▶use OpenInteract2::Exception qw( oi_error );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 87 $OpenInteract2::Config::Ini::VERSION = sprintf("%d.%02d", q$Revision: 1.21 $ =~ /(\d+)\.(\d+)/);
14  
15 87 my ( $log );
16  
17 ########################################
18 # CLASS METHODS
19  
20 # Stuff in metadata (_m):
21 # filename ($): file read from
22 # directory ($): directory file read from
23 # sections (\@): all full sections, in the order they were read
24 # comments (\%): key is full section name, value is comment scalar
25 # order (\@): order of sections read/assigned
26 # order_map (\%): to determine if section exists
27 # global (\@): keys in the 'Global' section
28  
29 31 ▶sub new {
Subroutine new: called CC=8 SCAR=22.7
Pod undocumented
30 31 my ( $pkg, $params ) = @_;
31 31 ▶ my $class = ref $pkg || $pkg;
Condition33%1/3
ref $pkg || $pkg
l!l&&r!l&&!r
0310
MC/DC0%0/2
ref $pkg || $pkg
ref $pkg $pkg
Truth table33%1/3
A || B
A= ref $pkgB= $pkg
ABresult
1X1
011
000
32 31 my $self = bless( {
33 filename => '',
34 directory => '',
35 sections => [],
36 comments => {},
37 order => [],
38 order_map => {},
39 global => [],
40 }, $class );
41 31 ▶ if ( $self->{_m}{filename} = $params->{filename} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'_m'}{'filename'} = $params->{'filename'}) { } 26 5 31
elsif ($params->{'content'}) { } 4 1 5
elsif ($params->{'struct'}) { } 0 1 1
42 26 ▶ $self->{_m}{directory} = $params->{directory} || dirname( $params->{filename} );
Condition66%2/3
$params->{'directory'} || dirname($params->{'filename'})
l!l&&r!l&&!r
1250
MC/DC0%0/2
$params->{'directory'} || dirname($params->{'filename'})
$params->{'directory'} dirname($params->{'filename'})
Truth table66%2/3
A || B
A= $params->{'directory'}B= dirname($params->{'filename'})
ABresult
1X1
011
000
43 26 $self->_translate_ini( OpenInteract2::Config->read_file( $params->{filename} ) );
44 }
45 elsif ( $params->{content} ) {
46 4 ▶ $self->{_m}{directory} = $params->{directory} || '.';
Condition100%2/2
$params->{'directory'} || '.'
l!l
13
MC/DC100%1/1
$params->{'directory'} || '.'
$params->{'directory'}
Truth table100%2/2
A
A= $params->{'directory'}
Aresult
11
00
47 4 $self->_translate_ini( $params->{content} );
48 }
49 elsif ( $params->{struct} ) {
50 0 ▶ $self->{_m}{directory} = $params->{directory} || '.';
Condition0%0/2
$params->{'directory'} || '.'
l!l
00
MC/DC0%0/1
$params->{'directory'} || '.'
$params->{'directory'}
Truth table0%0/2
A
A= $params->{'directory'}
Aresult
11
00
51 0 $self->_translate_struct_to_ini( $params->{struct} );
52 }
53 29 return $self;
54 }
55  
56 # Return the INI data as a raw hashref (naive for now...)
57  
58 2 ▶sub as_data {
Subroutine as_data: called CC=3 SCAR=11.0
Pod undocumented
59 2 my ( $self ) = @_;
60 2 my %data = ();
61 2 ▶ while ( my ( $k, $v ) = each %{ $self } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self;}) 28 2 30
62 28 ▶ $data{ $k } = $v unless ( $k eq '_m' );
Branch: 1 branch
BranchTrueFalseTotal
unless $k eq "_m" 26 2 28
63 }
64 2 return \%data;
65 }
66  
67  
68 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod undocumented
69 0 my ( $self, $section, @p ) = @_;
70 0 ▶ my ( $sub_section, $param ) = ( $p[1] ) ? ( $p[0], $p[1] ) : ( undef, $p[0] );
Branch: 1 branch
BranchTrueFalseTotal
$p[1] ? : 0 0 0
71 my $item = ( $sub_section )
72 ? $self->{ $section }{ $sub_section }{ $param }
73 0 ▶ : $self->{ $section }{ $param };
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 0 0 0
74 0 ▶ return $item unless ( ref $item eq 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $item eq "ARRAY" 0 0 0
75 0 ▶ return wantarray ? @{ $item } : $item->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
76 }
77  
78  
79 13 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod undocumented
80 13 my ( $self, $section, @p ) = @_;
81 13 ▶ my ( $sub_section, $param, $value ) = ( $p[2] )
Branch: 1 branch
BranchTrueFalseTotal
$p[2] ? : 2 11 13
82 ? ( $p[0], $p[1], $p[2] )
83 : ( undef, $p[0], $p[1] );
84 13 ▶ unless ( $self->is_section( $section, $sub_section ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_section($section, $sub_section)) 3 10 13
85 3 $self->add_section( $section, $sub_section );
86 }
87 13 return $self->_read_item( $section, $sub_section, $param, $value );
88 }
89  
90  
91 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod undocumented
92 0 my ( $self, $section, @p ) = @_;
93 0 ▶ my ( $sub_section, $param ) = ( $p[1] )
Branch: 1 branch
BranchTrueFalseTotal
$p[1] ? : 0 0 0
94 ? ( $p[0], $p[1] ) : ( undef, $p[0] );
95 0 ▶ delete $self->{ $section }{ $sub_section }{ $param } if ( $sub_section );
Branch: 1 branch
BranchTrueFalseTotal
if $sub_section 0 0 0
96 0 delete $self->{ $section }{ $param };
97 }
98  
99  
100 13 ▶sub is_section {
Subroutine is_section: called CC=2 SCAR=6.9
Pod undocumented
101 13 my ( $self, $section, $sub_section ) = @_;
102 13 ▶ my $full_section = ( $sub_section )
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 2 11 13
103 ? "$section $sub_section" : $section;
104 13 return $self->{_m}{order_map}{ $full_section };
105 }
106  
107 95 ▶sub add_section {
Subroutine add_section: called CC=2 SCAR=6.9
Pod undocumented
108 95 my ( $self, $section, $sub_section ) = @_;
109 95 ▶ my $full_section = ( $sub_section )
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 35 60 95
110 ? "$section $sub_section" : $section;
111 95 push @{ $self->{_m}{order} }, $full_section;
112 95 $self->{_m}{order_map}{ $full_section }++;
113 }
114  
115 1 ▶sub sections {
Subroutine sections: called CC=1 SCAR=0.0
Pod undocumented
116 1 my ( $self ) = @_;
117 1 return @{ $self->{_m}{order} };
118 }
119  
120 0 ▶sub main_sections {
Subroutine main_sections: not called CC=3 SCAR=24.8
Pod undocumented
121 0 my ( $self ) = @_;
122 0 my @main = ();
123 0 foreach my $full_section ( @{ $self->{_m}{order} } ) {
124 0 ▶ push @main, $full_section unless ( $full_section =~ /\s/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $full_section =~ /\s/ 0 0 0
125 }
126 0 return @main;
127 }
128  
129 92 ▶sub add_comments {
Subroutine add_comments: called CC=3 SCAR=11.0
Pod undocumented
130 92 my ( $self, @items ) = @_;
131 92 my ( $section, $sub_section, $comments );
132 92 ▶ if ( scalar @items == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @items == 3) { } 33 59 92
133 33 ( $section, $sub_section, $comments ) = @items;
134 }
135 else {
136 59 ( $section, $comments ) = @items;
137 }
138 92 ▶ my $full_section = ( $sub_section )
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 33 59 92
139 ? "$section $sub_section" : $section;
140 92 $self->{_m}{comments}{ $full_section } = $comments;
141 }
142  
143 3 ▶sub get_comments {
Subroutine get_comments: called CC=2 SCAR=6.9
Pod undocumented
144 3 my ( $self, $section, $sub_section ) = @_;
145 3 ▶ my $full_section = ( $sub_section )
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 2 1 3
146 ? "$section $sub_section" : $section;
147 3 return $self->{_m}{comments}{ $full_section };
148 }
149  
150 ########################################
151 # INPUT
152 ########################################
153  
154 30 ▶sub _translate_ini {
Subroutine _translate_ini: called CC=21 SCAR=32.8
155 30 my ( $self, $content ) = @_;
156 30 ▶ $log ||= get_logger( LOG_CONFIG );
Condition66%2/3
$log ||= get_logger("OI2.CONFIG")
l!l&&r!l&&!r
2370
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log get_logger("OI2.CONFIG")
Truth table66%2/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
ABresult
1X1
011
000
157  
158 # Content can be either an arrayref of lines from a file or a
159 # scalar with the content.
160  
161 30 ▶ my $lines = ( ref $content eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
ref $content eq 'ARRAY' ? : 26 4 30
162 ? $content : [ split( "\n", $content ) ];
163  
164 # Temporary holding for comments
165  
166 30 my @comments = ();
167 30 my ( $section, $sub_section );
168  
169 30 my $line_number = 0;
170 30 my ( $multiline_value, $multiline_param );
171 30 my $in_multiline = 0;
172  
173 30 my $resolved_lines = $self->_resolve_all_includes( $lines );
174  
175 # Cycle through the lines: skip blanks; accumulate comments for
176 # each section; register section/subsection; add parameter/value
177  
178 28 for ( @{ $resolved_lines } ) {
179 922 chomp;
180 922 $line_number++;
181 922 ▶ next if ( /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 112 810 922
182 810 ▶ if ( /^# Written by OpenInteract2::Config::Ini at/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^# Written by OpenInteract2::Config::Ini at/) 0 810 810
183 0 next; # ... get rid of current line
184 }
185 810 s/\s+$//;
186  
187 810 ▶ if ( /^\s*\#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\#/) 297 513 810
188 297 push @comments, $_;
189 297 $multiline_param = '';
190 297 $multiline_value = '';
191 297 $in_multiline = 0;
192 297 next;
193 }
194  
195 513 ▶ if ( /^\s*\[\s*(\S|\S.*\S)\s*\]\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\[\s*(\S|\S.*\S)\s*\]\s*$/) 92 421 513
196 92 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Found section [$1]") 0 92 92
197 $log->debug( "Found section [$1]" );
198 92 ( $section, $sub_section ) =
199 $self->_read_section_head( $1, \@comments );
200 92 @comments = ();
201 92 next;
202 }
203 421 my ( $param, $value );
204  
205 421 my $this_multiline = $_ =~ s|\\$||;
206 421 ▶ my $add_value = $_ || '';
Condition50%1/2
$_ || ''
l!l
4210
MC/DC0%0/1
$_ || ''
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
207 421 ▶ if ( $in_multiline and $this_multiline ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($in_multiline and $this_multiline) { } 0 421 421
elsif ($in_multiline) { } 9 412 421
elsif (/^\s*\@(.)/) { } 12 400 412
Condition66%2/3
$in_multiline and $this_multiline
!ll&&!rl&&r
41290
MC/DC0%0/2
$in_multiline and $this_multiline
$in_multiline $this_multiline
Truth table66%2/3
A and B
A= $in_multilineB= $this_multiline
ABresult
0X0
100
111
208 0 $multiline_value .= "$add_value\n";
209 0 next;
210 }
211 elsif ( $in_multiline ) {
212 9 $param = $multiline_param;
213 9 $value = $multiline_value . $add_value;
214 9 $multiline_param = '';
215 9 $multiline_value = '';
216 9 $in_multiline = 0;
217 }
218  
219 # Line is assigning multiple values at once...
220 elsif ( /^\s*\@(.)/ ) {
221 12 my $separator = $1;
222 12 s/^\s*@.\s*//;
223 12 my ( $value_listing );
224 12 ( $param, $value_listing ) = /^\s*([^=]+?)\s*=\s*(.*)\s*$/;
225 12 ▶ if ( $this_multiline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_multiline) 0 12 12
226 0 die "Cannot define multiple values (with a leading '\@') ",
227 "on a line that is continued (with a trailing '\\') ",
228 "at line number $line_number defining '$param'\n";
229 }
230 12 $value = [ split /\s*$separator\s*/, $value_listing ];
231 }
232 else {
233 400 ( $param, $value ) = /^\s*([^=]+?)\s*=\s*(.*)\s*$/;
234 400 ▶ if ( $this_multiline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_multiline) 9 391 400
235 9 $multiline_param = $param;
236 9 ▶ $multiline_value = $value || '';
Condition50%1/2
$value || ''
l!l
90
MC/DC0%0/1
$value || ''
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
237 9 $in_multiline = 1;
238 9 next;
239 }
240 }
241  
242 412 ▶ if ( $log->is_debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($log->is_debug) 0 412 412
243 0 ▶ my $show_section = ( $sub_section )
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 0 0 0
244 ? "$section.$sub_section" : $section;
245 0 ▶ my $show_param = $param || '';
Condition0%0/2
$param || ''
l!l
00
MC/DC0%0/1
$param || ''
$param
Truth table0%0/2
A
A= $param
Aresult
11
00
246 0 ▶ my $show_value = $value || '';
Condition0%0/2
$value || ''
l!l
00
MC/DC0%0/1
$value || ''
$value
Truth table0%0/2
A
A= $value
Aresult
11
00
247 0 $log->debug( "Line $line_number: $show_section.$show_param = '$show_value'" );
248 }
249 412 $self->_read_item( $section, $sub_section, $param, $value );
250 }
251 28 return $self;
252 }
253  
254 0 ▶sub _translate_struct_to_ini {
Subroutine _translate_struct_to_ini: not called CC=1 SCAR=6.9
255 0 my ( $self, $struct ) = @_;
256 0 die "_translate_struct_to_ini() not done yet!";
257 }
258  
259  
260 33 ▶sub _resolve_all_includes {
Subroutine _resolve_all_includes: called CC=5 SCAR=16.1
261 33 my ( $self, $content ) = @_;
262 33 my @resolved = ();
263 33 for ( @{ $content } ) {
264 933 chomp;
265 933 ▶ if ( /^\@INCLUDE\s*=\s*(.*)\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\@INCLUDE\s*=\s*(.*)\s*$/) { } 5 928 933
266 5 my $include_file = $1;
267 5 my $absolute_file = catfile( $self->{_m}{directory}, $include_file );
268 5 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Asked to include file '${include_file}'; ", "using absolute filename ", $absolute_file) 0 5 5
269 $log->debug( "Asked to include file '$include_file'; ",
270 "using absolute filename ", $absolute_file );
271 5 my $included_raw = eval {
272 5 OpenInteract2::Config->read_file( $absolute_file )
273 };
274 5 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 3 5
275 2 oi_error "Failed to read INI configuration -- cannot \@INCLUDE ",
276 "file '$include_file': $@";
277 }
278 3 my $included_resolved = $self->_resolve_all_includes( $included_raw );
279 3 push @resolved, @{ $included_resolved };
280 }
281 else {
282 928 push @resolved, "$_\n";
283 }
284 }
285 31 return \@resolved;
286 }
287  
288  
289 92 ▶sub _read_section_head {
Subroutine _read_section_head: called CC=4 SCAR=13.9
290 92 my ( $self, $full_section, $comments ) = @_;
291 92 my $comment_text = join "\n", @{ $comments };
292 92 ▶ if ( $full_section =~ /^([\w\-]+)\s+([\w\-]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_section =~ /^([\w\-]+)\s+([\w\-]+)$/) 33 59 92
293 33 my ( $section, $sub_section ) = ( $1, $2 );
294 33 ▶ $self->{ $section }{ $sub_section } ||= {};
Condition50%1/2
$self->{$section}{$sub_section} ||= {}
l!l
033
MC/DC0%0/1
$self->{$section}{$sub_section} ||= {}
$self->{$section}{$sub_section}
Truth table50%1/2
A
A= $self->{$section}{$sub_section}
Aresult
11
00
295 33 $self->add_section( $section, $sub_section );
296 33 $self->add_comments( $section, $sub_section, $comment_text );
297 33 return ( $section, $sub_section );
298 }
299 59 $self->add_section( $full_section );
300 59 $self->add_comments( $full_section, $comment_text );
301 59 ▶ $self->{ $full_section } ||= {};
Condition50%1/2
$self->{$full_section} ||= {}
l!l
059
MC/DC0%0/1
$self->{$full_section} ||= {}
$self->{$full_section}
Truth table50%1/2
A
A= $self->{$full_section}
Aresult
11
00
302 59 return ( $full_section, undef );
303 }
304  
305  
306 425 ▶sub _read_item {
Subroutine _read_item: called CC=6 SCAR=17.9
307 425 my ( $self, $section, $sub_section, $param, $value ) = @_;
308  
309 # Special case -- 'Global' stuff goes in the config object root
310  
311 425 ▶ if ( $section eq 'Global' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section eq "Global") 7 418 425
312 7 push @{ $self->{_m}{global} }, $param;
313 7 $self->_set_value( $self, $param, $value );
314 7 return;
315 }
316  
317 418 ▶ $self->{ $section } ||= {};
Condition100%2/2
$self->{$section} ||= {}
l!l
4171
MC/DC100%1/1
$self->{$section} ||= {}
$self->{$section}
Truth table100%2/2
A
A= $self->{$section}
Aresult
11
00
318 418 ▶ if ( $sub_section ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub_section) 108 310 418
319 108 ▶ $self->{ $section }{ $sub_section } ||= {};
Condition100%2/2
$self->{$section}{$sub_section} ||= {}
l!l
1062
MC/DC100%1/1
$self->{$section}{$sub_section} ||= {}
$self->{$section}{$sub_section}
Truth table100%2/2
A
A= $self->{$section}{$sub_section}
Aresult
11
00
320 }
321  
322 return ( $sub_section )
323 ? $self->_set_value( $self->{ $section }{ $sub_section }, $param, $value )
324 418 ▶ : $self->_set_value( $self->{ $section }, $param, $value );
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 108 310 418
325 }
326  
327  
328 # NOTE: $value can be a scalar or an arrayref (generally when a line
329 # defines multiple values at once)
330  
331 425 ▶sub _set_value {
Subroutine _set_value: called CC=7 SCAR=19.5
332 425 my ( $self, $set_in, $param, $value ) = @_;
333 425 ▶ return unless ( $param );
Branch: 1 branch
BranchTrueFalseTotal
unless $param 0 425 425
334 425 my $existing = $set_in->{ $param };
335 425 ▶ my @values = ( ref $value ) ? @{ $value } : ( $value );
Branch: 1 branch
BranchTrueFalseTotal
ref $value ? : 16 409 425
336 425 ▶ if ( $existing and ref $existing eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($existing and ref $existing eq 'ARRAY') { } 20 405 425
elsif ($existing) { } 22 383 405
elsif (scalar @values > 1) { } 8 375 383
Condition100%3/3
$existing and ref $existing eq "ARRAY"
!ll&&!rl&&r
3832220
MC/DC100%2/2
$existing and ref $existing eq "ARRAY"
$existing ref $existing eq "ARRAY"
Truth table100%3/3
A and B
A= $existingB= ref $existing eq "ARRAY"
ABresult
0X0
100
111
337 20 push @{ $set_in->{ $param } }, @values;
338 }
339 elsif ( $existing ) {
340 22 $set_in->{ $param } = [ $existing, @values ];
341 }
342 elsif ( scalar @values > 1 ) {
343 8 $set_in->{ $param } = [ @values ];
344 }
345 else {
346 375 $set_in->{ $param } = $values[0];
347 }
348 }
349  
350 ########################################
351 # OUTPUT
352 ########################################
353  
354 # to STDOUT
355 0 ▶sub output {
Subroutine output: not called CC=2 SCAR=17.9
Pod undocumented
356 0 my ( $self ) = @_;
357 0 foreach my $key ( @{ $self->{_m}{global} } ) {
358 0 $self->{Global}{ $key } = $self->{ $key };
359 }
360 0 print $self->_output_header();
361 0 print $self->_output_all_sections();
362 }
363  
364 1 ▶sub write_file {
Subroutine write_file: called CC=10 SCAR=29.1
Pod undocumented
365 1 my ( $self, $filename ) = @_;
366 1 ▶ $log ||= get_logger( LOG_CONFIG );
Condition33%1/3
$log ||= get_logger("OI2.CONFIG")
l!l&&r!l&&!r
010
MC/DC0%0/2
$log ||= get_logger("OI2.CONFIG")
$log get_logger("OI2.CONFIG")
Truth table33%1/3
A ||= B
A= $logB= get_logger("OI2.CONFIG")
ABresult
1X1
011
000
367  
368 1 ▶ $filename ||= $self->{_m}{filename} || 'config.ini';
Condition20%1/5
$filename ||= $self->{"_m"}{"filename"} || "config.ini"
l!l&&r!l&&!r
100
$self->{'_m'}{'filename'} || 'config.ini'
l!l
00
MC/DC0%0/2
$filename ||= $self->{"_m"}{"filename"} || "config.ini"
$filename $self->{'_m'}{'filename'}
Truth table33%1/3
A ||= B
A= $filenameB= $self->{'_m'}{'filename'}
ABresult
1X1
011
000
369 1 my ( $original_filename ) = "";
370 1 ▶ if ( -f $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) 0 1 1
371 0 $original_filename = $filename;
372 0 $filename = "$filename.new";
373 }
374 1 ▶ $original_filename ||= '';
Condition50%1/2
$original_filename ||= ""
l!l
01
MC/DC0%0/1
$original_filename ||= ""
$original_filename
Truth table50%1/2
A
A= $original_filename
Aresult
11
00
375  
376 # Set 'Global' items from the config object root
377  
378 1 foreach my $key ( @{ $self->{_m}{global} } ) {
379 0 $self->{Global}{ $key } = $self->{ $key };
380 }
381  
382 $log->is_debug &&
383 1 ▶ $log->debug( "Writing INI to [$filename] (original: ",
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Writing INI to [$filename] (original: ", "$original_filename)") 0 1 1
384 "$original_filename)" );
385 1 ▶ open( OUT, '>', $filename )
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", $filename or die "Cannot write configuration to [$filename]: $!" 1 0 1
386 || die "Cannot write configuration to [$filename]: $!";
387 1 print OUT $self->_output_header();
388 1 print OUT $self->_output_all_sections();
389 1 close( OUT );
390 1 ▶ if ( $original_filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($original_filename) 0 1 1
391 0 unlink( $original_filename );
392 0 rename( $filename, $original_filename );
393 0 $filename = $original_filename;
394 }
395 1 return $filename;
396 }
397  
398 1 ▶sub _output_header {
Subroutine _output_header: called CC=1 SCAR=0.0
399 1 my ( $self ) = @_;
400 1 return "# Written by ", ref $self, " at ", scalar localtime, "\n";
401 }
402  
403 1 ▶sub _output_all_sections {
Subroutine _output_all_sections: called CC=3 SCAR=11.2
404 1 my ( $self ) = @_;
405 1 my $out = '';
406 1 foreach my $full_section ( $self->sections ) {
407 3 my ( $section, $sub_section ) = split /\s+/, $full_section;
408 3 my $comments = $self->get_comments( $section, $sub_section );
409 3 ▶ if ( $comments ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($comments) 0 3 3
410 0 $out .= "$comments\n";
411 }
412 3 $out .= join( "\n", "[$full_section]",
413 $self->_output_section( $section, $sub_section ),
414 '' );
415 }
416 1 return $out;
417 }
418  
419 3 ▶sub _output_section {
Subroutine _output_section: called CC=6 SCAR=17.9
420 3 my ( $self, $section, $sub_section ) = @_;
421 my $show_from = ( $sub_section )
422 ? $self->{ $section }{ $sub_section }
423 3 ▶ : $self->{ $section };
Branch: 1 branch
BranchTrueFalseTotal
$sub_section ? : 2 1 3
424 3 my @items = ();
425 3 foreach my $key ( keys %{ $show_from } ) {
426 15 ▶ if ( ref $show_from->{ $key } eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $show_from->{$key} eq 'ARRAY') { } 2 13 15
elsif (ref $show_from->{$key} eq 'HASH') { } 2 11 13
427 2 foreach my $value ( @{ $show_from->{ $key } } ) {
428 4 push @items, $self->_show_item( $key, $value );
429 }
430 }
431 elsif ( ref $show_from->{ $key } eq 'HASH' ) {
432 # no-op -- this should get picked up later
433 }
434 else {
435 11 push @items, $self->_show_item( $key, $show_from->{ $key } );
436 }
437 }
438 3 return join "\n", @items;
439 }
440  
441  
442 15 ▶sub _show_item {
Subroutine _show_item: called CC=3 SCAR=11.1
443 15 ▶ my $l = $_[1] || '';
Condition50%1/2
$_[1] || ''
l!l
150
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
444 15 ▶ my $r = $_[2] || '';
Condition100%2/2
$_[2] || ''
l!l
114
MC/DC100%1/1
$_[2] || ''
$_[2]
Truth table100%2/2
A
A= $_[2]
Aresult
11
00
445 15 return join( ' = ', $l, $r );
446 }
447  
448 87 1;
449  
450 __END__
« OpenInteract2/Manage/Website/ThemeInstall.pm ↑ Summary OpenInteract2/Response/Standalone.pm »