Data/Printer/Filter/HASH.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 91.1% 31 / 34 Conditioncond 72.7% 24 / 33 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 85.9% 147 / 171 CC 31 SCAR 29.8
CC
31
Cov
90%
parse
26.4
_needs_quote
6.6
BEGIN
1.0
CRAP 19.8
« Data/Printer/Object.pm ↑ Summary Data/Printer/Filter/ARRAY.pm »
1 package Data::Printer::Filter::HASH;
2 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use Data::Printer::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 35 ▶use Data::Printer::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 35 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 35 filter 'HASH' => \&parse;
9  
10  
11 83 ▶sub parse {
Subroutine parse: called CC=26 SCAR=32.7
Pod undocumented
12 83 my ($hash_ref, $ddp) = @_;
13 83 my $tied = '';
14 83 ▶ if ($ddp->show_tied and my $tie = ref tied %$hash_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ddp->show_tied and my $tie = ref tied %$hash_ref) 2 81 83
Condition100%3/3
$ddp->show_tied and my $tie = ref tied %$hash_ref
!ll&&!rl&&r
3782
MC/DC100%2/2
$ddp->show_tied and my $tie = ref tied %$hash_ref
$ddp->show_tied my $tie = ref tied %$hash_ref
Truth table100%3/3
A and B
A= $ddp->show_tiedB= my $tie = ref tied %$hash_ref
ABresult
0X0
100
111
15 2 $tied = " (tied to $tie)";
16 }
17 83 ▶ return $ddp->maybe_colorize('{', 'brackets')
Branch: 1 branch
BranchTrueFalseTotal
if $ddp->max_depth and $ddp->current_depth >= $ddp->max_depth 0 83 83
Condition33%1/3
$ddp->max_depth and $ddp->current_depth >= $ddp->max_depth
!ll&&!rl&&r
8300
MC/DC0%0/2
$ddp->max_depth and $ddp->current_depth >= $ddp->max_depth
$ddp->max_depth $ddp->current_depth >= $ddp->max_depth
Truth table33%1/3
A and B
A= $ddp->max_depthB= $ddp->current_depth >= $ddp->max_depth
ABresult
0X0
100
111
18 . ' ' . $ddp->maybe_colorize('...', 'hash')
19 . ' ' . $ddp->maybe_colorize('}', 'brackets')
20 . $tied
21 if $ddp->max_depth && $ddp->current_depth >= $ddp->max_depth;
22  
23 83 my @src_keys = keys %$hash_ref;
24 83 ▶ return $ddp->maybe_colorize('{}', 'brackets') . $tied unless @src_keys;
Branch: 1 branch
BranchTrueFalseTotal
unless @src_keys 12 71 83
25 71 ▶ @src_keys = Data::Printer::Common::_nsort(@src_keys) if $ddp->sort_keys;
Branch: 1 branch
BranchTrueFalseTotal
if $ddp->sort_keys 69 2 71
26  
27 71 my $len = 0;
28 71 ▶ my $align_keys = $ddp->multiline && $ddp->align_hash;
Condition100%3/3
$ddp->multiline && $ddp->align_hash
!ll&&!rl&&r
15551
MC/DC100%2/2
$ddp->multiline && $ddp->align_hash
$ddp->multiline $ddp->align_hash
Truth table100%3/3
A && B
A= $ddp->multilineB= $ddp->align_hash
ABresult
0X0
100
111
29  
30 71 my @i = Data::Printer::Common::_fetch_indexes_for(\@src_keys, 'hash', $ddp);
31  
32 71 my %processed_keys;
33 # first pass, preparing keys and getting largest key size:
34 71 foreach my $idx (@i) {
35 160 ▶ next if ref $idx;
Branch: 1 branch
BranchTrueFalseTotal
if ref $idx 7 153 160
36 153 my $raw_key = $src_keys[$idx];
37 153 my $colored_key = Data::Printer::Common::_process_string($ddp, $raw_key, 'hash');
38 153 my $new_key = Data::Printer::Common::_colorstrip($colored_key);
39  
40 153 ▶ if (_needs_quote($ddp, $raw_key, $new_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_needs_quote($ddp, $raw_key, $new_key)) 2 151 153
41 2 my $quote_char = $ddp->scalar_quotes;
42 # foo'bar ==> 'foo\'bar'
43 2 ▶ if (index($new_key, $quote_char) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($new_key, $quote_char) != -1) 0 2 2
44 0 $new_key =~ s{$quote_char}{\\$quote_char}g;
45 0 $colored_key =~ s{$quote_char}{\\$quote_char}g;
46 }
47 2 $new_key = $quote_char . $new_key . $quote_char;
48 2 $colored_key = $ddp->maybe_colorize($quote_char, 'quotes')
49 . $colored_key
50 . $ddp->maybe_colorize($quote_char, 'quotes')
51 ;
52 }
53 153 $processed_keys{$idx} = {
54 raw => $raw_key,
55 colored => $colored_key,
56 nocolor => $new_key,
57 };
58 153 ▶ if ($align_keys) {
Branch: 1 branch
BranchTrueFalseTotal
if ($align_keys) 99 54 153
59 99 my $l = length $new_key;
60 99 ▶ $len = $l if $l > $len;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $len 60 39 99
61 }
62 }
63 # second pass, traversing and rendering:
64 71 $ddp->indent;
65 71 my $total_keys = scalar @i; # yes, counting messages so ',' appear in between.
66 #keys %processed_keys;
67 71 my $string = $ddp->maybe_colorize('{', 'brackets');
68 71 foreach my $idx (@i) {
69 160 $total_keys--;
70 # $idx is a message to display, not a real index
71 160 ▶ if (ref $idx) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $idx) 7 153 160
72 7 $string .= $ddp->newline . $$idx;
73 7 next;
74 }
75 153 my $key = $processed_keys{$idx};
76  
77 153 my $original_varname = $ddp->current_name;
78 # update 'var' to 'var{key}':
79 $ddp->current_name(
80 $original_varname
81 . ($ddp->arrows eq 'all' || ($ddp->arrows eq 'first' && $ddp->current_depth == 1) ? '->' : '')
82 153 ▶ . '{' . $key->{nocolor} . '}'
Branch: 1 branch
BranchTrueFalseTotal
$ddp->arrows eq 'all' || $ddp->arrows eq 'first' && $ddp->current_depth == 1 ? : 1 152 153
Condition66%4/6
$ddp->arrows eq "all" or $ddp->arrows eq "first" and $ddp->current_depth == 1
l!l&&r!l&&!r
01152
$ddp->arrows eq "first" and $ddp->current_depth == 1
!ll&&!rl&&r
15201
MC/DC33%1/3
$ddp->arrows eq "all" or $ddp->arrows eq "first" and $ddp->current_depth == 1
$ddp->arrows eq "all" $ddp->arrows eq "first" $ddp->current_depth == 1
Truth table50%2/4
A or B and C
A= $ddp->arrows eq "all"B= $ddp->arrows eq "first"C= $ddp->current_depth == 1
ABCresult
1XX1
0111
00X0
0100
83 );
84  
85 153 my $padding = $len - length($key->{nocolor});
86 153 ▶ $padding = 0 if $padding < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $padding < 0 54 99 153
87 $string .= $ddp->newline
88 . $key->{colored}
89 153 . (' ' x $padding)
90 . $ddp->maybe_colorize($ddp->hash_separator, 'separator')
91 ;
92  
93 # scalar references should be re-referenced to gain
94 # a '\' in front of them.
95 153 my $ref = ref $hash_ref->{$key->{raw}};
96 153 ▶ if ( $ref && $ref eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref and $ref eq 'SCALAR') { } 2 151 153
elsif ($ref and $ref ne 'REF') { } 45 106 151
Condition100%3/3
$ref and $ref eq "SCALAR"
!ll&&!rl&&r
105462
MC/DC100%2/2
$ref and $ref eq "SCALAR"
$ref $ref eq "SCALAR"
Truth table100%3/3
A and B
A= $refB= $ref eq "SCALAR"
ABresult
0X0
100
111
97 2 $string .= $ddp->parse(\$hash_ref->{ $key->{raw} }, tied_parent => !!$tied);
98 }
99 ▶ elsif ( $ref && $ref ne 'REF' ) {
Condition100%3/3
$ref and $ref ne "REF"
!ll&&!rl&&r
105145
MC/DC100%2/2
$ref and $ref ne "REF"
$ref $ref ne "REF"
Truth table100%3/3
A and B
A= $refB= $ref ne "REF"
ABresult
0X0
100
111
100 45 $string .= $ddp->parse( $hash_ref->{ $key->{raw} }, tied_parent => !!$tied);
101 } else {
102 106 $string .= $ddp->parse(\$hash_ref->{ $key->{raw} }, tied_parent => !!$tied);
103 }
104  
105 153 ▶ $string .= $ddp->maybe_colorize($ddp->separator, 'separator')
Branch: 1 branch
BranchTrueFalseTotal
if $total_keys > 0 or $ddp->end_separator 86 67 153
Condition66%2/3
$total_keys > 0 or $ddp->end_separator
l!l&&r!l&&!r
86067
MC/DC50%1/2
$total_keys > 0 or $ddp->end_separator
$total_keys > 0 $ddp->end_separator
Truth table66%2/3
A or B
A= $total_keys > 0B= $ddp->end_separator
ABresult
1X1
011
000
106 if $total_keys > 0 || $ddp->end_separator;
107  
108 # restore var name back to "var"
109 153 $ddp->current_name($original_varname);
110 }
111 71 $ddp->outdent;
112 71 $string .= $ddp->newline . $ddp->maybe_colorize('}', 'brackets');
113 71 return $string . $tied;
114 };
115  
116 #######################################
117 ### Private auxiliary helpers below ###
118 #######################################
119  
120 153 ▶sub _needs_quote {
Subroutine _needs_quote: called CC=6 SCAR=18.8
121 153 my ($ddp, $raw_key, $new_key) = @_;
122 153 my $quote_keys = $ddp->quote_keys;
123 153 my $scalar_quotes = $ddp->scalar_quotes;
124 153 ▶ return 0 unless defined $quote_keys && defined $scalar_quotes;;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $quote_keys and defined $scalar_quotes 0 153 153
Condition33%1/3
defined $quote_keys and defined $scalar_quotes
!ll&&!rl&&r
00153
MC/DC0%0/2
defined $quote_keys and defined $scalar_quotes
defined $quote_keys defined $scalar_quotes
Truth table33%1/3
A and B
A= defined $quote_keysB= defined $scalar_quotes
ABresult
0X0
100
111
125 153 ▶ if ($quote_keys eq 'auto'
Branch: 1 branch
BranchTrueFalseTotal
if ($quote_keys eq "auto" and $raw_key eq $new_key and not $new_key =~ /\s|\r|\n|\t|\f/) 151 2 153
Condition66%4/6
$quote_keys eq "auto" and $raw_key eq $new_key and not $new_key =~ /\s|\r|\n|\t|\f/
!ll&&!rl&&r
20151
$quote_keys eq "auto" and $raw_key eq $new_key
!ll&&!rl&&r
20151
MC/DC33%1/3
$quote_keys eq "auto" and $raw_key eq $new_key and not $new_key =~ /\s|\r|\n|\t|\f/
$quote_keys eq "auto" $raw_key eq $new_key not $new_key =~ /\s|\r|\n|\t|\f/
Truth table50%2/4
A and B and not(C)
A= $quote_keys eq "auto"B= $raw_key eq $new_keyC= not $new_key =~ /\s|\r|\n|\t|\f/
ABCresult
0XX0
10X0
1100
1111
126 && $raw_key eq $new_key
127 && $new_key !~ /\s|\r|\n|\t|\f/) {
128 151 return 0;
129 }
130 2 return 1;
131 }
132  
133 35 1;
« Data/Printer/Object.pm ↑ Summary Data/Printer/Filter/ARRAY.pm »