Framework/Base/Object/DumpObj.pm

filter: Statementstmt 83.7% 113 / 135 Branchbran 75.0% 39 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 15 / 18 Podpod 100% 8 / 8 totaltotal 82.1% 175 / 213 CC 30 SCAR 17.0
CC
30
Cov
81%
_print_ref
11.9
debug
6.0
verbose
6.0
CRAP 5.5
« Framework/Base/Object.pm ↑ Summary Framework/Base/SearchPath.pm »
1 package App::Framework::Base::Object::DumpObj ;
2  
3 =head1 NAME
4  
5 App::Framework::Base::Object::DumpObj - Dump out an objects contents
6  
7 =head1 SYNOPSIS
8  
9 use App::Framework::Base::Object::DumpObj ;
10  
11  
12  
13 =head1 DESCRIPTION
14  
15 Given a data object (scalar, hash, array etc) prints out that objects contents
16  
17  
18 =head1 REQUIRES
19  
20  
21 =head1 DIAGNOSTICS
22  
23 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
24  
25 =head1 AUTHOR
26  
27 Steve Price C<< <sdprice at cpan.org> >>
28  
29 =head1 BUGS
30  
31 None that I know of!
32  
33 =head1 INTERFACE
34  
35 =over 4
36  
37 =cut
38  
39 35 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 35 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 35 ▶use Cwd ;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43  
44 35 our $VERSION = "2.002" ;
45  
46 35 require Exporter ;
47 35 our @ISA = qw(Exporter);
48 35 our @EXPORT =qw(
49 );
50  
51 35 our @EXPORT_OK =qw(
52 prt_data
53 prtstr_data
54 exclude
55  
56 debug
57 verbose
58  
59 $DEBUG
60 $VERBOSE
61 $PRINT_OBJECTS
62 $PREFIX
63 );
64  
65  
66 #============================================================================================
67 # USES
68 #============================================================================================
69  
70  
71 #============================================================================================
72 # GLOBALS
73 #============================================================================================
74  
75 35 our $DEBUG = 0 ;
76 35 our $VERBOSE = 0 ;
77 35 our $PRINT_OBJECTS = 0 ;
78 35 our $QUOTE_VALS = 0 ;
79 35 our $PREFIX = 0 ;
80  
81 35 my $level ;
82 my %already_seen ;
83 35 my $prt_str ;
84 35 my %excludes ;
85  
86  
87 #============================================================================================
88 # EXPORTED
89 #============================================================================================
90  
91 #---------------------------------------------------------------------------------------------------
92  
93 =item B<debug($level)>
94  
95 Set debug print options to B<$level>.
96  
97 0 = No debug
98 1 = standard debug information
99 2 = verbose debug information
100  
101 =cut
102  
103 sub debug
104 0 ▶{
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
105 0 my ($flag) = @_ ;
106  
107 0 my $old = $DEBUG ;
108  
109 0 ▶ if (defined($flag))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) 0 0 0
110 {
111 # set this module debug flag & sub-modules
112 0 $DEBUG = $flag ;
113 }
114 0 return $old ;
115 }
116  
117 #---------------------------------------------------------------------------------------------------
118  
119 =item B<verbose($level)>
120  
121 Set vebose print options to B<$level>.
122  
123 0 = Non verbose
124 1 = verbose print
125  
126 =cut
127  
128 sub verbose
129 0 ▶{
Subroutine verbose: not called CC=2 SCAR=17.9
Pod documented
130 0 my ($flag) = @_ ;
131  
132 0 my $old = $VERBOSE ;
133  
134 0 ▶ if (defined($flag))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) 0 0 0
135 {
136 # set this module verbose flag & sub-modules
137 0 $VERBOSE = $flag ;
138 }
139 0 return $old ;
140 }
141  
142  
143 #---------------------------------------------------------------------------------------------------
144  
145 =item B<print_objects_flag($flag)>
146  
147 Set option for printing out objects to B<$flag>.
148  
149 0 = Do not print contents of object [DEFAULT]
150 1 = print contents of object
151  
152 =cut
153  
154 sub print_objects_flag
155 1264 ▶{
Subroutine print_objects_flag: called CC=2 SCAR=7.0
Pod documented
156 1264 my ($flag) = @_ ;
157  
158 1264 my $old = $PRINT_OBJECTS ;
159  
160 1264 ▶ if (defined($flag))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) 1264 0 1264
161 {
162 # set this module debug flag & sub-modules
163 1264 $PRINT_OBJECTS = $flag ;
164 }
165 1264 return $old ;
166 }
167  
168  
169 #---------------------------------------------------------------------------------------------------
170  
171 =item B<quote_vals_flag($flag)>
172  
173 Set option quoting the values to B<$flag>.
174  
175 0 = Do not quote values [DEFAULT]
176 1 = Print values inside quotes
177
178 This is useful for re-using the output directly to define an array/hash
179  
180 =cut
181  
182 sub quote_vals_flag
183 0 ▶{
Subroutine quote_vals_flag: not called CC=2 SCAR=17.9
Pod documented
184 0 my ($flag) = @_ ;
185  
186 0 my $old = $QUOTE_VALS ;
187  
188 0 ▶ if (defined($flag))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flag) 0 0 0
189 {
190 # set this module debug flag & sub-modules
191 0 $QUOTE_VALS = $flag ;
192 }
193 0 return $old ;
194 }
195  
196 #---------------------------------------------------------------------------------------------------
197  
198 =item B<exclude(@list)>
199  
200 Set the list of excluded HASH keys. Any keys in a HASH that match the name(s) in the list will not be
201 displayed.
202  
203 Specifying an empty list clears the excludes
204  
205 =cut
206  
207 sub exclude
208 31 ▶{
Subroutine exclude: called CC=1 SCAR=0.0
Pod documented
209 31 my (@list) = @_ ;
210
211 31 %excludes = () ;
212 31 %excludes = map {$_ => 1} @list ;
213  
214 31 return ;
215 }
216  
217 #---------------------------------------------------------------------------------------------------
218  
219 =item B<prefix($prefix)>
220  
221 Prefix all output lines with B<$prefix>
222  
223 Returns previous value
224  
225 =cut
226  
227 sub prefix
228 2340 ▶{
Subroutine prefix: called CC=2 SCAR=7.0
Pod documented
229 2340 my ($prefix) = @_ ;
230
231 2340 my $old = $PREFIX ;
232  
233 2340 ▶ if (defined($prefix))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix) 2340 0 2340
234 {
235 # set this module debug flag & sub-modules
236 2340 $PREFIX = $prefix ;
237 }
238 2340 return $old ;
239 }
240  
241  
242  
243 #---------------------------------------------------------------------
244  
245 =item B<prtstr_data(@list)>
246  
247 Create a multiline string of all items in the list. Handles scalars, hashes (as an array),
248 arrays, ref to scalar, ref to hash, ref to array, object.
249  
250 =cut
251  
252 sub prtstr_data
253 1265 ▶{
Subroutine prtstr_data: called CC=3 SCAR=11.0
Pod documented
254 1265 my (@data_list) = @_ ;
255  
256 1265 $level = -1 ;
257 1265 %already_seen = () ;
258 1265 $prt_str = '' ;
259  
260 1265 foreach my $var (@_)
261 {
262 1445 ▶ if (ref ($var))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var) { } 147 1298 1445
263 {
264 147 _print_ref($var);
265 }
266 else
267 {
268 1298 _print_scalar($var);
269 }
270 }
271  
272 1265 return $prt_str ;
273 }
274  
275 #---------------------------------------------------------------------
276  
277 =item B<prt_data(@list)>
278  
279 Print out each item in the list. Handles scalars, hashes (as an array),
280 arrays, ref to scalar, ref to hash, ref to array, object.
281  
282 =cut
283  
284 sub prt_data
285 1264 ▶{
Subroutine prt_data: called CC=1 SCAR=0.0
Pod documented
286 1264 my (@data_list) = @_ ;
287
288 1264 prtstr_data(@data_list) ;
289 1264 print $prt_str ;
290
291 }
292  
293  
294  
295 # ============================================================================================
296 # UNEXPORTED BY DEFAULT
297 # ============================================================================================
298  
299 #---------------------------------------------------------------------------------------------------
300 sub _print_scalar
301 3075 ▶{
Subroutine _print_scalar: called CC=1 SCAR=0.0
302 3075 ++$level;
303 3075 _print_indented ($_[0]);
304 3075 --$level;
305 }
306  
307 #---------------------------------------------------------------------------------------------------
308 sub _print_ref
309 1047 ▶{
Subroutine _print_ref: called CC=10 SCAR=24.8
310 1047 my $r = $_[0];
311  
312 1047 ▶ if (!defined($r))
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $r) { } 0 1047 1047
elsif (exists $already_seen{$r}) { } 97 950 1047
313 {
314 0 _print_indented ("undef\n");
315 0 return;
316 }
317 elsif (exists ($already_seen{$r}))
318 {
319 97 _print_indented ("# $r (Seen earlier)\n");
320 97 return;
321 }
322 else
323 {
324 950 $already_seen{$r}=1;
325 }
326  
327 950 my $ref_type = ref($r);
328  
329 950 ▶ if ($ref_type eq "ARRAY")
Branch: 4 branches
BranchTrueFalseTotal
if ($ref_type eq 'ARRAY') { } 343 607 950
elsif ($ref_type eq 'SCALAR') { } 344 263 607
elsif ($ref_type eq 'HASH') { } 215 48 263
elsif ($ref_type eq 'REF') { } 0 48 48
330 {
331 343 _print_array($r);
332 }
333 elsif ($ref_type eq "SCALAR")
334 {
335 344 _print_scalar($$r);
336 344 _print_str(" # Ref -> $r\n");
337 }
338 elsif ($ref_type eq "HASH")
339 {
340 215 _print_hash($r);
341 }
342 elsif ($ref_type eq "REF")
343 {
344 0 ++$level;
345 0 _print_indented("# Ref -> ($r)\n");
346 0 _print_ref($$r);
347 0 --$level;
348 }
349 else
350 {
351 48 _print_indented ("# OBJECT $ref_type\n");
352  
353 # If required (and we can) print out the object
354 48 ▶ if ($PRINT_OBJECTS)
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Framework::Base::Object::DumpObj::PRINT_OBJECTS) 48 0 48
355 {
356 48 my $obj_ref_str = "$r" ;
357 48 ▶ if ($obj_ref_str =~ /ARRAY/)
Branch: 2 branches
BranchTrueFalseTotal
if ($obj_ref_str =~ /ARRAY/) { } 0 48 48
elsif ($obj_ref_str =~ /HASH/) { } 11 37 48
358 {
359 0 _print_array($r);
360 }
361 elsif ($obj_ref_str =~ m/HASH/)
362 {
363 11 _print_hash($r);
364 }
365 }
366 }
367 }
368  
369 #---------------------------------------------------------------------------------------------------
370 sub _print_array
371 343 ▶{
Subroutine _print_array: called CC=3 SCAR=11.0
372 343 my ($r_array) = @_;
373  
374 343 ++$level;
375 343 _print_indented ("[ # $r_array\n");
376 343 foreach my $var (@$r_array)
377 {
378 1895 ▶ if (ref ($var))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var) { } 462 1433 1895
379 {
380 462 _print_ref($var);
381 }
382 else
383 {
384 1433 _print_scalar($var);
385 1433 _print_str(",\n");
386 }
387 }
388 343 _print_indented ("],\n");
389 343 --$level;
390 }
391  
392 #---------------------------------------------------------------------------------------------------
393 sub _print_hash
394 226 ▶{
Subroutine _print_hash: called CC=6 SCAR=17.9
395 226 my($r_hash) = @_;
396  
397 226 my($key, $val);
398 226 ++$level;
399  
400 226 _print_indented ("{ # $r_hash\n");
401  
402 # while (($key, $val) = each %$r_hash)
403 226 foreach my $key (sort keys %$r_hash)
404 {
405 #print "<< key <$key> r_hash <$r_hash> >>\n" ;
406 1718 my $val = $r_hash->{$key} ;
407 1718 ▶ if (defined($val))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 1494 224 1718
408 {
409 1494 ▶ $val = ($val ? $val : '0');
Branch: 1 branch
BranchTrueFalseTotal
$val ? : 1305 189 1494
410 }
411 else
412 {
413 224 $val = 'undef' ;
414 }
415  
416 1718 ++$level;
417  
418 1718 ▶ if (exists($excludes{$key}))
Branch: 1 branch
BranchTrueFalseTotal
if (exists $excludes{$key}) { } 10 1708 1718
419 {
420 10 _print_indented ("$key => ...\n");
421 }
422 else
423 {
424 1708 ▶ if (ref ($val))
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 438 1270 1708
425 {
426 438 _print_indented ("$key => \n");
427 438 _print_ref($val);
428 }
429 else
430 {
431 1270 _print_indented ("$key => $val,\n");
432 }
433 }
434 1718 --$level;
435 }
436 226 _print_indented ("},\n");
437 226 --$level;
438 }
439  
440 #---------------------------------------------------------------------------------------------------
441 sub _print_indented
442 6076 ▶{
Subroutine _print_indented: called CC=4 SCAR=13.9
443 6076 my $spaces = " " x $level;
444 6076 ▶ if ($PREFIX)
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Framework::Base::Object::DumpObj::PREFIX) 2427 3649 6076
445 {
446 # print prefix at start of a line
447 2427 ▶ if (!$prt_str)
Branch: 2 branches
BranchTrueFalseTotal
if (not $prt_str) { } 1170 1257 2427
elsif ($prt_str =~ /(.*)\n$/) { } 1209 48 1257
448 {
449 1170 _print_str("$PREFIX") ;
450 }
451 elsif ($prt_str =~ m/(.*)\n$/)
452 {
453 1209 _print_str("$PREFIX") ;
454 }
455 }
456 6076 _print_str("${spaces}") ;
457 6076 _print_val($_[0]) ;
458 # $prt_str .= "\n" ;
459 }
460  
461  
462 #---------------------------------------------------------------------------------------------------
463 sub _print_val
464 6076 ▶{
Subroutine _print_val: called CC=4 SCAR=13.9
465 6076 my ($val) = @_ ;
466
467 6076 ▶ if (defined($val))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 5854 222 6076
468 {
469 5854 _print_str("$val") ;
470  
471 # Print positive numerical value in hex too
472 5854 ▶ if ($val =~ m/(^|\s+)(\d+)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /(^|\s+)(\d+)$/) 3 5851 5854
473 {
474 3 ▶ _print_str(sprintf " # [0x%0x]", $2) if ($2 > 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $2 > 0 3 0 3
475 }
476 }
477 else
478 {
479 222 _print_str("undef") ;
480 }
481  
482 }
483  
484 #---------------------------------------------------------------------------------------------------
485 sub _print_str
486 16311 ▶{
Subroutine _print_str: called CC=1 SCAR=0.0
487 16311 my ($str) = @_ ;
488
489 16311 $prt_str .= $str ;
490 }
491  
492  
493  
494 # ============================================================================================
495 # END OF PACKAGE
496  
497 =back
498  
499 =cut
500  
501 35 1;
502  
503 __END__
« Framework/Base/Object.pm ↑ Summary Framework/Base/SearchPath.pm »