blib/lib/Data/TreeDumper/Utils.pm

filter: Statementstmt 94.2% 131 / 139 Branchbran 70.0% 42 / 60 Conditioncond 59.0% 13 / 22 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 19 / 19 Podpod 87.5% 7 / 8 totaltotal 81.6% 214 / 262 CC 44 SCAR 18.0
CC
44
Cov
84%
get_caller_stack
10.5
first_nsort_last
9.1
hash_keys_sorter
7.2
CRAP 6.1
↑ Summary
1  
2 package Data::TreeDumper::Utils ;
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(carp croak confess) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN
9 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Sub::Exporter -setup =>
Subroutine BEGIN: called CC=1 SCAR=0.0
11 {
12 exports => [ qw(first_nsort_last_filter keys_order no_sort_filter hash_keys_sorter filter_class_keys get_caller_stack) ],
13 groups =>
14 {
15 all => [ qw(first_nsort_last_filter keys_order no_sort_filter hash_keys_sorter filter_class_keys get_caller_stack) ],
16 }
17 2 };
18
19 2 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 $VERSION = '0.04';
21 }
22  
23 #-------------------------------------------------------------------------------
24  
25 2 ▶use Sort::Naturally;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Check::ISA ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 2 ▶use Readonly ;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 Readonly my $EMPTY_STRING => q{} ;
30  
31 #-------------------------------------------------------------------------------
32  
33 =head1 NAME
34  
35 Data::TreeDumper::Utils - A selection of utilities to use with Data::TreeDumper
36  
37 =head1 SYNOPSIS
38  
39 use Data::TreeDumper::Utils qw(:all) ;
40
41 DumpTree
42 (
43 $requirements_structure,
44 'Requirements structure:',
45 FILTER => \&first_nsort_last_filter,
46 FILTER_ARGUMENT => {...},
47 ) ;
48
49 DumpTree #shorthand for the call to first_nsort_last_filter
50 (
51 $requirements_structure,
52 'Requirements structure:',
53 keys_order(...),
54 ) ;
55
56 DumpTree
57 (
58 $ixhash_hash_ref,
59 'An IxHash hash',
60 FILTER => \&no_sort_filter,
61 ) ;
62
63 DumpTree
64 (
65 $structure,
66 'sorted',
67 FILTER =>
68 CreateChainingFilter
69 (
70 \&remove_keys_starting_with_A,
71 \&hash_keys_sorter
72 ),
73 ) ;
74
75 DumpTree
76 (
77 $structure,
78 'filter_class_keys example:',
79 FILTER => filter_class_keys(T1 => ['A'], 'HASH' => [qr/./],),
80 ) ;
81
82 DumpTree(get_caller_stack(), 'Stack dump:') ;
83  
84 =head1 DESCRIPTION
85  
86 A collection useful sorting filters and utilities that can be used with L<Data::TreeDumper>. You can also
87 study the source for examples of how to write filters.
88  
89 =head1 SUBROUTINES/METHODS
90  
91 =cut
92  
93 #-------------------------------------------------------------------------------
94  
95 sub first_nsort_last_filter ## no critic Subroutines::ProhibitManyArgs
96 3 ▶{
Subroutine first_nsort_last_filter: called CC=4 SCAR=15.5
Pod documented
97  
98 =head2 first_nsort_last_filter()
99  
100 This filter will apply to all hashes and object derived from hashes, it allows you to change the order in
101 which the keys are rendered.
102  
103 my $dump = DumpTree
104 (
105 {
106 AXC => 1,
107 ZZZ => 1,
108 A => 1,
109 B2 => 1,
110 B => 1,
111 REMOVE => 1,
112 EVOMER => 1,
113 C => 1,
114 D => 1,
115 E => 1,
116 },
117 'structure:',
118  
119 FILTER => \&first_nsort_last_filter,
120 FILTER_ARGUMENT =>
121 {
122 REMOVE => ['REMOVE', qr/EVO/],
123 AT_START_FIXED => ['ZZZ', qr/B/],
124 AT_START => ['ZZZ'], # already taken by AT_START_FIXED
125 AT_END => ['C', 'A'],
126 AT_END_FIXED => [qr/AX/],
127 },
128 ) ;
129  
130 generates:
131 structure:
132 |- ZZZ = 1 [S1]
133 |- B = 1 [S2]
134 |- B2 = 1 [S3]
135 |- D = 1 [S4]
136 |- E = 1 [S5]
137 |- A = 1 [S6]
138 |- C = 1 [S7]
139 `- AXC = 1 [S8]
140  
141 B<Arguments>
142  
143 The arguments are passed through the call to L<Data::TreeDumper> in the B<FILTER_ARGUMENT>
144 option. B<FILTER_ARGUMENT> points to a hash reference with the possible following keys. All the keys are optional.
145  
146 Each key is an array reference containing a list of regexes or strings. Keys matching the regexes or string will be
147 sorted in the category in which the matching regex or string was declared. The categories are, in priority order:
148  
149 =over 2
150  
151 =item * REMOVE - the keys that should not be rendered
152  
153 =item * AT_START_FIXED - the keys that should be rendered at the start, will not be sorted
154  
155 =item * AT_START - the keys that should be rendered next, will be sorted
156  
157 =item * AT_END - the keys that should be rendered last, will be sorted
158  
159 =item * AT_END_FIXED - the keys that should be rendered at the end, will not be sorted
160  
161 =back
162  
163 Any key that doesn't match a regex or a string will automatically be in this category.
164  
165 Keys are sorted by L<Sort::Naturally>.
166  
167 B<Returns> - the keys sorted according to the defined categories.
168  
169 B<See> - I<Filters> in L<Data::TreeDumper>.
170  
171 =cut
172  
173 3 my ($structure, undef, undef, $nodes_to_display, undef, $filter_argument) = @_ ;
174  
175 3 ▶if('HASH' eq ref $structure || obj($structure, 'HASH'))
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $structure or &obj($structure, "HASH")) 3 0 3
Condition33%1/3
"HASH" eq ref $structure or &obj($structure, "HASH")
l!l&&r!l&&!r
300
MC/DC0%0/2
"HASH" eq ref $structure or &obj($structure, "HASH")
"HASH" eq ref $structure &obj($structure, "HASH")
Truth table33%1/3
A or B
A= "HASH" eq ref $structureB= &obj($structure, "HASH")
ABresult
1X1
011
000
176 {
177 3 ▶ my $keys = defined $nodes_to_display ? $nodes_to_display : [keys %{$structure}] ;
Branch: 1 branch
BranchTrueFalseTotal
defined $nodes_to_display ? : 0 3 3
178 return
179 (
180 'HASH',
181 undef,
182 first_nsort_last
183 (
184 3 %{$filter_argument},
185 KEYS => $keys,
186 )
187 ) ;
188 }
189
190 0 return(Data::TreeDumper::DefaultNodesToDisplay($structure)) ;
191 }
192  
193 sub keys_order
194 1 ▶{
Subroutine keys_order: called CC=1 SCAR=0.0
Pod documented
195  
196 =head2 keys_order(@filtering_categories)ma
197  
198 See L<first_nsort_last_filter()>
199  
200 DumpTree($structure, 'title:', keys_order(REMOVE => [], AT_START => [], ...)) ;
201
202 =cut
203  
204 return
205 1 FILTER => \&first_nsort_last_filter,
206 FILTER_ARGUMENT => { @_ } ;
207 }
208  
209 sub first_nsort_last
210 3 ▶{
Subroutine first_nsort_last: called CC=9 SCAR=22.0
Pod documented
211  
212 =head2 [p] first_nsort_last(AT_START => [regex, string, ...], AT_END => [regex, string, ...], ..., KEYS => [keys to sort] )
213  
214 Implementation of I<first_nsort_last_filter> key sorting.
215  
216 B<Arguments>
217  
218 =over 2
219  
220 =item * REMOVE - a reference to an array containing regexes or strings, keys matching will be removed from display
221  
222 =item * AT_START_FIXED - a reference to an array containing regexes or strings, won't be sorted, multiple matches to regex are sorted
223  
224 =item * AT_START - a reference to an array containing regexes or strings, will be sorted
225  
226 =item * AT_END - a reference to an array containing regexes or strings, will be sorted
227  
228 =item * AT_END_FIXED - a reference to an array containing regexes or strings, won't be sorted
229  
230 =item * KEYS - a reference to an array containing the keys to sort
231  
232 =back
233  
234 B<Returns> - the sorted keys
235  
236 =cut
237  
238 3 my (%argument_hash) = @_ ;
239  
240 3 ▶my @keys =@{ $argument_hash{KEYS} || []} ;
Condition50%1/2
$argument_hash{'KEYS'} || []
l!l
30
MC/DC0%0/1
$argument_hash{'KEYS'} || []
$argument_hash{'KEYS'}
Truth table50%1/2
A
A= $argument_hash{'KEYS'}
Aresult
11
00
241
242 3 ▶if(exists $argument_hash{REMOVE})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $argument_hash{"REMOVE"}) 2 1 3
243 {
244 2 my @remove_regex = map { {regexp => $_} } @{ $argument_hash{REMOVE}} ;
245 2 @keys = grep {! match_regexes($_, @remove_regex)} @keys ;
246 }
247  
248  
249 3 ▶my @at_start_fixed = map { {regexp => $_} } @{ $argument_hash{AT_START_FIXED} } if exists $argument_hash{AT_START_FIXED} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $argument_hash{"AT_START_FIXED"} 2 1 3
250  
251 3 my $at_start_matches = [] ; #all the matches must be in the same array so we can do an nsort on them
252 3 ▶my @at_start = map { {regexp => $_, matches => $at_start_matches } } @{ $argument_hash{AT_START} } if exists $argument_hash{AT_START} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $argument_hash{"AT_START"} 3 0 3
253  
254 3 my $at_end_matches = [] ;
255 3 ▶my @at_end = map { {regexp => $_, matches => $at_end_matches } } @{ $argument_hash{AT_END} } if exists $argument_hash{AT_END} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $argument_hash{"AT_END"} 3 0 3
256  
257 3 ▶my @at_end_fixed = map { {regexp => $_} } @{ $argument_hash{AT_END_FIXED} } if exists $argument_hash{AT_END_FIXED} ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $argument_hash{"AT_END_FIXED"} 2 1 3
258  
259 3 my @in_the_middle = {regexp => qr//} ; # matches everything that the other have not
260  
261 3 match_regexes($_, @at_start_fixed, @at_start, @at_end, @at_end_fixed, @in_the_middle) for(@keys) ;
262  
263 return
264 map
265 {
266 3 exists $_->{matches} ?
267 15 ▶ nsort( @{ $_->{matches} } ) :
Branch: 1 branch
BranchTrueFalseTotal
exists $_->{'matches'} ? : 15 0 15
268 () ;
269 }
270 @at_start_fixed, { matches => $at_start_matches }, @in_the_middle, { matches => $at_end_matches }, @at_end_fixed ;
271 }
272  
273  
274 sub match_regexes
275 40 ▶{
Subroutine match_regexes: called CC=5 SCAR=16.1
Pod undocumented
276 =head2 [p] match_regexes($key, @regexes)
277  
278 matches the key to a set of filterring regexps
279  
280 B<Arguments>
281  
282 =over 2
283  
284 =item * $key - a string, the hash key to be matched to the sorting regexes
285  
286 =item * @regexes - an array, each element contains a Regexp or a stig to match to I<$key>, matches are added to the regexp element
287  
288 =back
289  
290 B<Returns> - the sorted keys
291  
292 =cut
293  
294 40 my ($key, @regexes) = @_ ;
295  
296 40 my $match ;
297  
298 40 for my $regexp (@regexes)
299 {
300 115 ▶ if('Regexp' eq ref $regexp->{regexp})
Branch: 1 branch
BranchTrueFalseTotal
if ('Regexp' eq ref $regexp->{'regexp'}) { } 47 68 115
301 {
302 47 ▶ if ($key =~ $regexp->{regexp})
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /$regexp->{"regexp"}/) 15 32 47
303 {
304 15 push @{ $regexp->{matches} }, $key ;
305 15 $match++ ;
306 15 last ;
307 }
308 }
309 else
310 {
311 68 ▶ if ($key eq $regexp->{regexp})
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $regexp->{"regexp"}) 9 59 68
312 {
313 9 push @{ $regexp->{matches} }, $key ;
314 9 $match++ ;
315 9 last ;
316 }
317 }
318 }
319
320 40 return $match ;
321 }
322  
323 #-------------------------------------------------------------------------------
324  
325 sub no_sort_filter
326 2 ▶{
Subroutine no_sort_filter: called CC=4 SCAR=16.6
Pod documented
327  
328 =head2 no_sort_filter()
329  
330 A hash filter to replace the default L<Data::TreeDumper> filter which sorts hash keys. This is useful if you have a
331 hash based on L<Tie::IxHash>, or equivalent, that keep the key order internally.
332  
333 print DumpTree
334 (
335 $ixhash_hash_ref,
336 'An IxHash hash',
337 FILTER => \&no_sort_filter,
338 ) ;
339  
340 B<Arguments> - none
341  
342 B<Returns> - hash keys unsorted
343  
344 =cut
345  
346 2 my ($structure, undef, undef, $keys) = @_ ;
347  
348 2 ▶if('HASH' eq ref $structure|| obj($_, 'HASH'))
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $structure or &obj($_, 'HASH')) { } 2 0 2
Condition33%1/3
"HASH" eq ref $structure or &obj($_, "HASH")
l!l&&r!l&&!r
200
MC/DC0%0/2
"HASH" eq ref $structure or &obj($_, "HASH")
"HASH" eq ref $structure &obj($_, "HASH")
Truth table33%1/3
A or B
A= "HASH" eq ref $structureB= &obj($_, "HASH")
ABresult
1X1
011
000
349 {
350 0 ▶ return('HASH', undef, @{$keys}) if(defined $keys) ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $keys 0 2 2
351 2 return('HASH', undef, keys %{$structure}) ;
352 }
353 else
354 {
355 0 return Data::TreeDumper::DefaultNodesToDisplay(@_) ;
356 }
357 }
358  
359 #-------------------------------------------------------------------------------
360  
361 sub hash_keys_sorter
362 6 ▶{
Subroutine hash_keys_sorter: called CC=6 SCAR=19.7
Pod documented
363  
364 =head2 hash_keys_sorter()
365  
366 When no filter is given to L<Data::TreeDumper>, it will sort hash keys using L<Sort::Naturally>. If you create your
367 own filter or have chaining filters, you will have to do the sorting yourself (if you want keys to be sorted) or you can
368 use this filter to do the sorting.
369  
370 # Remove keys starting with A, return in keys in the order the hash returns them
371 DumpTree($s, 'not sorted', FILTER => \&remove_keys_starting_with_A,) ;
372
373 # Remove keys starting with A, sort keys
374 DumpTree
375 (
376 $s,
377 'sorted',
378 FILTER => CreateChainingFilter(\&remove_keys_starting_with_A, \&hash_keys_sorter),
379 ) ;
380
381  
382 B<Arguments> - none
383  
384 B<Returns> - the sorted keys
385  
386 =cut
387  
388 6 my ($structure, undef, undef, $nodes_to_display) = @_ ;
389  
390 6 ▶if('HASH' eq ref $structure || obj($structure, 'HASH'))
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $structure or &obj($structure, "HASH")) 6 0 6
Condition66%2/3
"HASH" eq ref $structure or &obj($structure, "HASH")
l!l&&r!l&&!r
240
MC/DC0%0/2
"HASH" eq ref $structure or &obj($structure, "HASH")
"HASH" eq ref $structure &obj($structure, "HASH")
Truth table66%2/3
A or B
A= "HASH" eq ref $structureB= &obj($structure, "HASH")
ABresult
1X1
011
000
391 {
392 0 ▶ my $keys = defined $nodes_to_display ? $nodes_to_display : [keys %{$structure}] ;
Branch: 1 branch
BranchTrueFalseTotal
defined $nodes_to_display ? : 6 0 6
393
394 6 my %keys ;
395
396 6 for my $key (@{$keys})
397 {
398 14 ▶ if('ARRAY' eq ref $key)
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $key) { } 0 14 14
399 {
400 0 $keys{$key->[0]} = $key ;
401 }
402 else
403 {
404 14 $keys{$key} = $key ;
405 }
406 }
407
408 6 return('HASH', undef, map{$keys{$_}} nsort keys %keys) ;
409 }
410  
411 0 return(Data::TreeDumper::DefaultNodesToDisplay($structure)) ;
412 }
413  
414 #----------------------------------------------------------------------
415  
416 sub filter_class_keys
417 1 ▶{
Subroutine filter_class_keys: called CC=5 SCAR=16.3
Pod documented
418  
419 =head2 filter_class_keys($class => \@keys, $class => \@keys,, ...)
420  
421 A filter that allows you select which keys to render depending on the type of the structure elements. This lets you
422 filter out data you don't want to render.
423  
424 Note: this filter does not sort the keys!
425  
426 package Potatoe ;
427
428 package BlueCongo;
429 @ISA = ("Potatoe");
430
431 package main ;
432
433 use strict ;
434 use warnings ;
435
436 use Data::TreeDumper ;
437
438 my $data_1 = bless({ A => 1, B => 2, C => 3}, 'T1') ;
439 my $data_2 = bless({ A => 1, B => 2, C => 3}, 'T2') ;
440 my $data_3 = bless({ A => 1, B => 2, C => 3}, 'T3') ;
441 my $blue_congo = bless({IAM => 'A_BLUE_CONGO', COLOR => 'blue'}, 'BlueCongo') ;
442
443 print DumpTree
444 (
445 {D1 => $data_1, D2 => $data_2, D3 => $data_3, Z => $blue_congo,},
446 'filter_class_keys example:',
447
448 FILTER => filter_class_keys
449 (
450 # match class containing 'T1' in its name, show the 'A' key
451 T1 => ['A'],
452
453 # match T2 class, show all the key that don't contain 'C'
454 qr/2/ => [qr/[^C]/],
455
456 # match BlueCongo class via regex
457 # qr/congo/i => [qr/I/],
458
459 # match BlueCongo class
460 # BlueCongo => [qr/I/],
461
462 # match any Potatoe, can't use a regex for class
463 Potatoe => [qr/I/],
464
465 # mach any hash or hash based object, displays all the keys
466 'HASH' => [qr/./],
467 ),
468 ) ;
469  
470 generates:
471  
472 filter_class_keys example:
473 |- Z = blessed in 'BlueCongo' [OH1]
474 | `- IAM = A_BLUE_CONGO [S2]
475 |- D2 = blessed in 'T2' [OH3]
476 | |- A = 1 [S4]
477 | `- B = 2 [S5]
478 |- D3 = blessed in 'T3' [OH6]
479 | |- A = 1 [S7]
480 | |- C = 3 [S8]
481 | `- B = 2 [S9]
482 `- D1 = blessed in 'T1' [OH10]
483 `- A = 1 [S11]
484  
485 B<Arguments>
486  
487 A list of:
488  
489 =over 2
490  
491 =item * $class - either a regex or a string.
492  
493 =item * \@keys - a reference to an array containing the keys to display. The keys can be a string or a regex.
494  
495 =back
496  
497 B<Returns> - the keys to render
498  
499 =cut
500  
501 1 my (@class_to_key) = @_ ;
502 1 my @classes ;
503  
504 1 ▶for(my $index = 0 ; $index < $#class_to_key ; $index += 2) ## no critic ControlStructures::ProhibitCStyleForLoops
Branch: 1 branch
BranchTrueFalseTotal
if ($index < $#class_to_key) 4 1 5
505 {
506 4 ▶ croak 'class must be a string or a regex!' unless $EMPTY_STRING eq ref $class_to_key[$index] || 'Regexp' eq ref $class_to_key[$index] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $EMPTY_STRING eq ref $class_to_key[$index] or "Regexp" eq ref $class_to_key[$index] 0 4 4
Condition66%2/3
$EMPTY_STRING eq ref $class_to_key[$index] or "Regexp" eq ref $class_to_key[$index]
l!l&&r!l&&!r
310
MC/DC0%0/2
$EMPTY_STRING eq ref $class_to_key[$index] or "Regexp" eq ref $class_to_key[$index]
$EMPTY_STRING eq ref $class_to_key[$index] "Regexp" eq ref $class_to_key[$index]
Truth table66%2/3
A or B
A= $EMPTY_STRING eq ref $class_to_key[$index]B= "Regexp" eq ref $class_to_key[$index]
ABresult
1X1
011
000
507 4 ▶ croak 'keys must be passed in an array reference!' unless 'ARRAY' eq ref $class_to_key[$index + 1] ;
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $class_to_key[$index + 1] 0 4 4
508
509 4 my @regexes = @{$class_to_key[$index + 1]} ;
510
511 push @classes,
512 [
513 $class_to_key[$index],
514 sub
515 15 ▶ {
Subroutine __ANON__: called CC=3 SCAR=11.0
516 15 my ($value) = @_ ;
517
518 15 for my $regex (@regexes)
519 {
520 15 ▶ return 1 if $value =~ $regex ;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /$regex/ 11 4 15
521 }
522
523 4 return 0 ;
524 },
525 4 ] ;
526 }
527  
528 return sub
529 5 ▶ {
Subroutine __ANON__: called CC=6 SCAR=18.4
530 5 my ($s) = @_ ;
531 5 my $ref_s = ref $s ;
532
533 5 ▶ if($ref_s eq 'HASH' || obj($s, 'HASH'))
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_s eq 'HASH' or &obj($s, 'HASH')) { } 5 0 5
Condition66%2/3
$ref_s eq "HASH" or &obj($s, "HASH")
l!l&&r!l&&!r
140
MC/DC0%0/2
$ref_s eq "HASH" or &obj($s, "HASH")
$ref_s eq "HASH" &obj($s, "HASH")
Truth table66%2/3
A or B
A= $ref_s eq "HASH"B= &obj($s, "HASH")
ABresult
1X1
011
000
534 {
535 5 for my $class (@classes)
536 {
537 14 ▶ if($ref_s =~ $class->[0] || obj($s, $class->[0]))
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_s =~ /$class->[0]/ or &obj($s, $class->[0])) 5 9 14
Condition100%3/3
$ref_s =~ /$class->[0]/ or &obj($s, $class->[0])
l!l&&r!l&&!r
329
MC/DC100%2/2
$ref_s =~ /$class->[0]/ or &obj($s, $class->[0])
$ref_s =~ /$class->[0]/ &obj($s, $class->[0])
Truth table100%3/3
A or B
A= $ref_s =~ /$class->[0]/B= &obj($s, $class->[0])
ABresult
1X1
011
000
538 {
539 5 return('HASH', undef, grep {$class->[1]->($_)} keys %{$s}) ;
540 }
541 }
542
543 0 return('HASH', {},) ;
544 }
545 else
546 {
547 0 return(Data::TreeDumper::DefaultNodesToDisplay($s)) ;
548 }
549 1 } ;
550 }
551  
552 #-------------------------------------------------------------------------------
553  
554 sub get_caller_stack
555 1 ▶{
Subroutine get_caller_stack: called CC=10 SCAR=23.5
Pod documented
556  
557 =head2 get_caller_stack($levels_to_dump)
558  
559 Creates a data structure containing information about the call stack.
560  
561 s1() ;
562
563 sub s1 { my $x = eval {package xxx ; main::s2() ;} ; }
564 sub s2 { s3('a', [1, 2, 3]) ; }
565 sub s3 { print DumpTree(get_caller_stack(), 'Stack dump:') ; }
566
567 will generate this stack dump:
568
569 Stack dump:
570 |- 0
571 | `- main::s1
572 | |- ARGS (no elements)
573 | |- AT = try_me.pl:20
574 | |- CALLERS_PACKAGE = main
575 | `- CONTEXT = void
576 |- 1
577 | `- (eval)
578 | |- AT = try_me.pl:24
579 | |- CALLERS_PACKAGE = main
580 | |- CONTEXT = scalar
581 | `- EVAL = yes
582 |- 2
583 | `- main::s2
584 | |- ARGS (no elements)
585 | |- AT = try_me.pl:24
586 | |- CALLERS_PACKAGE = xxx
587 | `- CONTEXT = scalar
588 `- 3
589 `- main::s3
590 |- ARGS
591 | |- 0 = a
592 | `- 1
593 | |- 0 = 1
594 | |- 1 = 2
595 | `- 2 = 3
596 |- AT = try_me.pl:29
597 |- CALLERS_PACKAGE = main
598 `- CONTEXT = scalar
599
600  
601 B<Arguments>
602  
603 =over 2
604  
605 =item * $levels_to_dump - the number of level that should be included in the call stack
606  
607 =back
608  
609 B<Returns> - the call stack structure
610  
611 =cut
612  
613 1 ▶my $level_to_dump = shift || 1_000_000 ; ## no critic ValuesAndExpressions::ProhibitMagicNumbers
Condition50%1/2
shift() || 1000000
l!l
01
MC/DC0%0/1
shift() || 1000000
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
614 1 my $current_level = 2 ; # skip this function
615  
616 1 $level_to_dump += $current_level ; #
617  
618 1 my @stack_dump ;
619  
620 1 ▶while ($current_level < $level_to_dump)
Branch: 1 branch
BranchTrueFalseTotal
if ($current_level < $level_to_dump) 5 0 5
621 {
622 5 my ($package, $filename, $line, $subroutine, $has_args, $wantarray,
623 $evaltext, $is_require, $hints, $bitmask) = eval " package DB ; caller($current_level) ;" ; ## no critic BuiltinFunctions::ProhibitStringyEval
624
625 5 ▶ last unless defined $package;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 1 4 5
626
627 4 my %stack ;
628 4 ▶ $stack{$subroutine}{EVAL} = 'yes' if($subroutine eq '(eval)') ;
Branch: 1 branch
BranchTrueFalseTotal
if $subroutine eq "(eval)" 1 3 4
629 4 ▶ $stack{$subroutine}{EVAL} = $evaltext if defined $evaltext ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $evaltext 0 4 4
630 4 ▶ $stack{$subroutine}{ARGS} = [@DB::args] if($has_args) ; ## no critic Variables::ProhibitPackageVars
Branch: 1 branch
BranchTrueFalseTotal
if $has_args 3 1 4
631 4 ▶ $stack{$subroutine}{'REQUIRE-USE'} = 'yes' if $is_require ;
Branch: 1 branch
BranchTrueFalseTotal
if $is_require 0 4 4
632 4 ▶ $stack{$subroutine}{CONTEXT} = defined $wantarray ? $wantarray ? 'list' : 'scalar' : 'void' ;
Branch: 2 branches
BranchTrueFalseTotal
defined $wantarray ? : 4 0 4
$wantarray ? : 0 4 4
633 4 $stack{$subroutine}{CALLERS_PACKAGE} = $package ;
634 4 $stack{$subroutine}{AT} = "$filename:$line" ;
635
636 4 unshift @stack_dump, \%stack ;
637 4 $current_level++;
638 }
639  
640 1 return(\@stack_dump);
641 }
642  
643 #---------------------------------------------------------------------------------
644  
645 2 1 ;
646  
647 =head1 BUGS AND LIMITATIONS
648  
649 None so far.
650  
651 =head1 AUTHOR
652  
653 Nadim ibn hamouda el Khemir
654 CPAN ID: NH
655 mailto: nadim@cpan.org
656  
657 =head1 LICENSE AND COPYRIGHT
658  
659 This program is free software; you can redistribute
660 it and/or modify it under the same terms as Perl itself.
661  
662 =head1 SUPPORT
663  
664 You can find documentation for this module with the perldoc command.
665  
666 perldoc Data::TreeDumper::Utils
667  
668 You can also look for information at:
669  
670 =over 4
671  
672 =item * AnnoCPAN: Annotated CPAN documentation
673  
674 L<http://annocpan.org/dist/Data-TreeDumper-Utils>
675  
676 =item * RT: CPAN's request tracker
677  
678 Please report any bugs or feature requests to L <bug-data-treedumper-utils@rt.cpan.org>.
679  
680 We will be notified, and then you'll automatically be notified of progress on
681 your bug as we make changes.
682  
683 =item * Search CPAN
684  
685 L<http://search.cpan.org/dist/Data-TreeDumper-Utils>
686  
687 =back
688  
689 =head1 SEE ALSO
690  
691 =cut
↑ Summary