Framework/Base.pm

filter: Statementstmt 92.5% 136 / 147 Branchbran 85.2% 87 / 102 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 89.2% 249 / 279 CC 64 SCAR 39.2
CC
64
Cov
89%
expand_keys
60.5
new
7.8
BEGIN
1.0
CRAP 50.2
« Framework.pm ↑ Summary Framework/Feature/Args.pm »
1 package App::Framework::Base ;
2  
3 =head1 NAME
4  
5 App::Framework::Base - Application feature
6  
7 =head1 SYNOPSIS
8  
9 use App::Framework::Base ;
10  
11  
12 =head1 DESCRIPTION
13  
14 Base object for all application objects (core/extensions/features etc)
15  
16 B<DOCUMENTATION TO BE COMPLETED>
17  
18 =cut
19  
20 34 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 34 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 34 our $VERSION = "1.100" ;
24  
25 #============================================================================================
26 # USES
27 #============================================================================================
28 34 ▶use App::Framework::Base::Object::ErrorHandle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 #============================================================================================
31 # OBJECT HIERARCHY
32 #============================================================================================
33 34 our @ISA = qw(App::Framework::Base::Object::ErrorHandle) ;
34  
35 #============================================================================================
36 # GLOBALS
37 #============================================================================================
38  
39 34 our $PRIORITY_CORE = 10 ;
40 34 our $PRIORITY_SYSTEM = 100 ;
41 34 our $PRIORITY_USER = 1000 ;
42 34 our $PRIORITY_DEFAULT = 32767 ;
43  
44 34 our $class_debug = 0 ;
45  
46 =head2 FIELDS
47  
48 The following fields should be defined either in the call to 'new()', as part of a 'set()' call, or called by their accessor method
49 (which is the same name as the field):
50  
51  
52 =over 4
53  
54 =item B<requires> - list of required modules
55  
56 ARRAY ref of a list of module names that are required to be loaded by this object.
57  
58 =item B<loaded> - list of which modules have been loaded
59  
60 HASH containing the modules loaded (used as key), with the value set to 1 if the module loaded ok; 0 otherwise
61  
62 =item B<requires_ok> - all required modules are ok
63  
64 Flag that is set if all required modules loaded correctly
65  
66 =back
67  
68 =cut
69  
70 34 my %FIELDS = (
71 'priority' => $PRIORITY_DEFAULT,
72 'requires' => [],
73
74 'loaded' => {}, # list of which modules have been loaded
75 'requires_ok' => 0, # all required modules are ok
76 );
77  
78 #============================================================================================
79  
80 =head2 CONSTRUCTOR
81  
82 =over 4
83  
84 =cut
85  
86 #============================================================================================
87  
88 =item B< new([%args]) >
89  
90 Create a new feature.
91  
92 The %args are specified as they would be in the B<set> method.
93  
94 =cut
95  
96 sub new
97 182 ▶{
Subroutine new: called CC=7 SCAR=20.6
Pod documented
98 182 my ($obj, %args) = @_ ;
99  
100 182 ▶ my $class = ref($obj) || $obj ;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
01820
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
101  
102 182 ▶print "App::Framework::Base->new() class=$class\n" if $class_debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 6 176 182
103  
104 # Create object
105 182 my $this = $class->SUPER::new(%args) ;
106  
107 ## Check for any required modules
108 182 my $ok = 1 ;
109 182 my %loaded ;
110 182 foreach my $module (@{$this->requires})
111 {
112 2 ▶ eval "package $class; use $module;" ;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶ if ($@)
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 2 2
114 {
115 0 $loaded{$module} = 0 ;
116 0 $ok = 0 ;
117 }
118 else
119 {
120 2 $loaded{$module} = 1 ;
121 }
122 }
123 182 $this->requires_ok($ok) ;
124 182 $this->loaded(\%loaded) ;
125  
126 ## First check that all required modules loaded correcly
127 182 ▶ if (!$this->requires_ok)
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->requires_ok) 0 182 182
128 {
129 0 my $loaded_href = $class->loaded ;
130 0 my $failed_modules = join ', ', grep {$loaded_href->{$_}} keys %$loaded_href ;
131 0 $this->throw_fatal("Failed to load: $failed_modules") ;
132 }
133  
134 182 ▶print "App::Framework::Base->new() - END\n" if $class_debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 6 176 182
135  
136 182 return($this) ;
137 }
138  
139  
140  
141 #============================================================================================
142  
143 =back
144  
145 =head2 CLASS METHODS
146  
147 =over 4
148  
149 =cut
150  
151 #============================================================================================
152  
153 #-----------------------------------------------------------------------------
154  
155 =item B< init_class([%args]) >
156  
157 Initialises the object class variables.
158  
159 =cut
160  
161 sub init_class
162 182 ▶{
Subroutine init_class: called CC=1 SCAR=0.0
Pod documented
163 182 my $class = shift ;
164 182 my (%args) = @_ ;
165  
166 # Add extra fields
167 182 $class->add_fields(\%FIELDS, \%args) ;
168  
169 # init class
170 182 $class->SUPER::init_class(%args) ;
171  
172 }
173  
174 #----------------------------------------------------------------------------
175  
176 =item B<expand_keys($hash_ref, $vars_aref)>
177  
178 Processes all of the HASH values, replacing any variables with their contents. The variable
179 values are taken from the ARRAY ref I<$vars_aref>, which is an array of hashes. Each hash
180 containing variable name / variable value pairs.
181  
182 The HASH values being expanded can be either scalar, or an ARRAY ref. In the case of the ARRAY ref each
183 ARRAY entry must be a scalar (e.g. an array of file lines).
184  
185 =cut
186  
187 sub expand_keys
188 93 ▶{
Subroutine expand_keys: called CC=58 SCAR=41.0
Pod documented
189 93 my $class = shift ;
190 93 my ($hash_ref, $vars_aref, $_state_href, $_to_expand) = @_ ;
191  
192 93 ▶print "expand_keys($hash_ref, $vars_aref)\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 5 88 93
193 93 ▶$class->prt_data("vars=", $vars_aref, "hash=", $hash_ref) if $class_debug ;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 5 88 93
194  
195 93 ▶ my %to_expand = $_to_expand ? (%$_to_expand) : (%$hash_ref) ;
Branch: 1 branch
BranchTrueFalseTotal
$_to_expand ? : 5 88 93
196 93 ▶ if (!$_state_href)
Branch: 1 branch
BranchTrueFalseTotal
unless ($_state_href) 88 5 93
197 {
198 ## Top-level
199 88 my %data_ref ;
200
201 # create state HASH
202 88 $_state_href = {} ;
203
204 # scan through hash looking for variables
205 88 %to_expand = () ;
206 88 foreach my $key (keys %$hash_ref)
207 {
208 281 my @vals ;
209 281 ▶ if (ref($hash_ref->{$key}) eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash_ref->{$key} eq 'ARRAY') { } 80 201 281
elsif (not ref $hash_ref->{$key}) { } 201 0 201
210 {
211 80 @vals = @{$hash_ref->{$key}} ;
212 }
213 elsif (!ref($hash_ref->{$key}))
214 {
215 201 push @vals, $hash_ref->{$key} ;
216 }
217
218 ## Set up state - provide a level of indirection so that we can handle the case where multiple keys point to the same data
219 281 ▶ my $ref = $hash_ref->{$key} || '' ;
Condition100%2/2
$hash_ref->{$key} || ''
l!l
25130
MC/DC100%1/1
$hash_ref->{$key} || ''
$hash_ref->{$key}
Truth table100%2/2
A
A= $hash_ref->{$key}
Aresult
11
00
220 281 ▶ if ($ref && exists($data_ref{"$ref"}))
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and exists $data_ref{"$ref"}) { } 41 240 281
Condition100%3/3
$ref and exists $data_ref{"$ref"}
!ll&&!rl&&r
3021041
MC/DC100%2/2
$ref and exists $data_ref{"$ref"}
$ref exists $data_ref{"$ref"}
Truth table100%3/3
A and B
A= $refB= exists $data_ref{"$ref"}
ABresult
0X0
100
111
221 {
222 41 ▶print " + already seen data for key=$key\n" if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 4 37 41
223 # already got created a state for this data, point to it
224 41 $_state_href->{$key} = $data_ref{"$ref"} ;
225 }
226 else
227 {
228 240 ▶print " + new state key=$key\n" if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 18 222 240
229 240 my $state = 'expanded' ;
230 240 $_state_href->{$key} = \$state ;
231 }
232  
233 # save data reference
234 281 ▶ $data_ref{"$ref"} = $_state_href->{$key} if $ref ;
Branch: 1 branch
BranchTrueFalseTotal
if $ref 251 30 281
235
236 281 ▶print " + check for expansion...\n" if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 22 259 281
237 281 foreach my $val (@vals)
238 {
239 1445 ▶ next unless $val ;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 274 1171 1445
240  
241 1171 ▶print " + + val=$val\n" if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 82 1089 1171
242  
243 1171 ▶ if (index($val, '$') >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, "\$") != -1) 35 1136 1171
244 {
245 35 ▶print " + + + needs expanding\n" if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 16 19 35
246 35 $to_expand{$key}++ ;
247 35 ${$_state_href->{$key}} = 'to_expand' ;
248 35 last ;
249 }
250 }
251 }
252 }
253  
254 93 ▶$class->prt_data("to expand=", \%to_expand) if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 5 88 93
255  
256 93 ▶$class->prt_data("Hash=", $hash_ref) if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 5 88 93
257  
258 ## Expand them
259 93 foreach my $key (keys %to_expand)
260 {
261 40 ▶ print " # Key=$key State=${$_state_href->{$key}}\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 18 22 40
262
263 # skip if not valid (if called recursively with a variable that is not in the hash)
264 40 ▶ next unless exists($hash_ref->{$key}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash_ref->{$key} 0 40 40
265  
266 # Do replacement iff required
267 40 ▶ next if ${$_state_href->{$key}} eq 'expanded' ;
Branch: 1 branch
BranchTrueFalseTotal
if ${$_state_href->{$key};} eq "expanded" 12 28 40
268  
269 28 my @vals ;
270 28 ▶ if (ref($hash_ref->{$key}) eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $hash_ref->{$key} eq 'ARRAY') { } 7 21 28
elsif (not ref $hash_ref->{$key}) { } 21 0 21
271 {
272 7 foreach my $val (@{$hash_ref->{$key}})
273 {
274 76 push @vals, \$val ;
275 }
276 }
277 elsif (!ref($hash_ref->{$key}))
278 {
279 21 push @vals, \$hash_ref->{$key} ;
280 }
281
282 # mark as expanding
283 28 ${$_state_href->{$key}} = 'expanding' ;
284  
285 28 ▶$class->prt_data("Vals to expand=", \@vals) if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 13 15 28
286  
287 #use re 'debugcolor' ;
288  
289 28 foreach my $val_ref (@vals)
290 {
291  
292 97 ▶ print " # Expand \"$$val_ref\" ...\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 56 41 97
293  
294 97 $$val_ref =~ s{
295 (?:
296 [\\\$]\$ # escaped dollar
297 \{{0,1} # optional brace
298 (\w+) # find a "word" and store it in $1
299 \}{0,1} # optional brace
300 )
301 |
302 (?:
303 \$ # find a literal dollar sign
304 \{{0,1} # optional brace
305 (\w+) # find a "word" and store it in $1
306 \}{0,1} # optional brace
307 )
308 }{
309 68 my $prefix = '' ;
310 68 my ($escaped, $var) = ($1, $2) ;
311
312 68 ▶ $escaped ||= '' ;
Condition100%2/2
$escaped ||= ""
l!l
1652
MC/DC100%1/1
$escaped ||= ""
$escaped
Truth table100%2/2
A
A= $escaped
Aresult
11
00
313 68 ▶ $var ||= '' ;
Condition100%2/2
$var ||= ""
l!l
5216
MC/DC100%1/1
$var ||= ""
$var
Truth table100%2/2
A
A= $var
Aresult
11
00
314
315 68 ▶ print " # esc=\"$escaped\", prefix=\"$prefix\", var=\"$var\"\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 46 22 68
316
317 68 my $replace='' ;
318 68 ▶ if ($escaped)
Branch: 1 branch
BranchTrueFalseTotal
if ($escaped) { } 16 52 68
319 {
320 16 $prefix = '$' ;
321 16 $replace = $escaped ;
322 16 ▶ print " ## escaped prefix=$prefix replace=$replace\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 16 0 16
323 16 ▶ print " ## DONE\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 16 0 16
324 }
325 else
326 {
327 ## use current HASH values before vars
328 52 ▶ if (defined $hash_ref->{$var})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hash_ref->{$var}) 30 22 52
329 {
330 30 ▶print " ## var=$var current state=${$_state_href->{$var}}\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 13 17 30
331 30 ▶ if (${$_state_href->{$var}} eq 'to_expand')
Branch: 1 branch
BranchTrueFalseTotal
if (${$_state_href->{$var};} eq "to_expand") 5 25 30
332 {
333 5 ▶print " ## var=$var call expand..\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 2 3 5
334 # go expand it first
335 5 $class->expand_keys($hash_ref, $vars_aref, $_state_href, {$var => 1}) ;
336 }
337 30 ▶ if (${$_state_href->{$var}} eq 'expanded')
Branch: 1 branch
BranchTrueFalseTotal
if (${$_state_href->{$var};} eq "expanded") 30 0 30
338 {
339 30 ▶print " ## var=$var already expanded\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 13 17 30
340 30 $replace = $hash_ref->{$var}; # expand variable
341 0 ▶ $replace = join("\n", @{$hash_ref->{$var}}) if (ref($hash_ref->{$var}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash_ref->{$var} eq "ARRAY" 0 30 30
342 }
343 }
344 52 ▶print " ## var=$var can replace from hash=$replace\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 30 22 52
345
346 ## If not found, use vars
347 52 ▶ if (!$replace)
Branch: 1 branch
BranchTrueFalseTotal
unless ($replace) 22 30 52
348 {
349 ## use vars
350 22 foreach my $href (@$vars_aref)
351 {
352 46 ▶ if (defined $href->{$var})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $href->{$var}) 22 24 46
353 {
354 22 $replace = $href->{$var}; # expand variable
355 0 ▶ $replace = join("\n", @{$hash_ref->{$var}}) if (ref($href->{$var}) eq 'ARRAY') ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $href->{$var} eq "ARRAY" 0 22 22
356 22 ▶ print " ## found var=$var replace=$replace\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 17 5 22
357 22 last ;
358 }
359 }
360 }
361 52 ▶print " ## var=$var can replace now=$replace\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 30 22 52
362  
363 52 ▶ if (!$replace)
Branch: 1 branch
BranchTrueFalseTotal
unless ($replace) 0 52 52
364 {
365 0 $replace = "" ;
366 0 ▶ print " ## no replacement\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 0 0 0
367 0 ▶ print " ## DONE\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 0 0 0
368 }
369 }
370
371 68 ▶ print " ## ALL DONE $key: $escaped$var = \"$prefix$replace\"\n\n" if $class_debug;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug 46 22 68
372 68 "$prefix$replace" ;
373 }egxm; ## NOTE: /m is for multiline anchors; /s is for multiline dots
374 }
375  
376 28 ▶$class->prt_data("Hash now=", $hash_ref) if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 13 15 28
377  
378 # mark as expanded
379 28 ${$_state_href->{$key}} = 'expanded' ;
380  
381 28 ▶$class->prt_data("State now=", $_state_href) if $class_debug>=2;
Branch: 1 branch
BranchTrueFalseTotal
if $App::Framework::Base::class_debug >= 2 13 15 28
382 }
383 }
384  
385  
386  
387 ##============================================================================================
388 #
389 #=back
390 #
391 #=head2 OBJECT DATA METHODS
392 #
393 #=over 4
394 #
395 #=cut
396 #
397 ##============================================================================================
398  
399  
400 ##============================================================================================
401 #
402 #=back
403 #
404 #=head2 OBJECT METHODS
405 #
406 #=over 4
407 #
408 #=cut
409 #
410 ##============================================================================================
411  
412  
413 #============================================================================================
414 #
415 # PRIVATE
416 #
417 #============================================================================================
418  
419 # ============================================================================================
420 # END OF PACKAGE
421  
422 =back
423  
424 =head1 DIAGNOSTICS
425  
426 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
427  
428 =head1 AUTHOR
429  
430 Steve Price C<< <sdprice at cpan.org> >>
431  
432 =head1 BUGS
433  
434 None that I know of!
435  
436 =cut
437  
438  
439 34 1;
440  
441 __END__
« Framework.pm ↑ Summary Framework/Feature/Args.pm »