recurse2txt

filter: Statementstmt 52.0% 38 / 73 Branchbran 47.5% 19 / 40 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 66.6% 6 / 9 Podpod n/a totaltotal 52.2% 71 / 136 SCAR 47.0
CC
27
Cov
52%
_dump
20.1
Dumper
14.0
hash_recurse
12.0
CRAP 109.5
↑ Summary blib/lib/Math/Base/Convert.pm »
1 #!/usr/bin/perl
2 #
3 # recurse2txt routines
4 #
5 # version 1.08, 12-20-12, michael@bizsystems.com
6 #
7 # 10-3-11 updated to bless into calling package
8 # 10-10-11 add SCALAR ref support
9 # 1.06 12-16-12 add hexDumper
10 # 1.07 12-19-12 added wantarray return of data and elements
11 # 1.08 12-20-12 add wantarray to hexDumper
12 #
13 #use strict;
14 #use diagnostics;
15  
16 7 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # generate a unique signature for a particular hash
19 #
20 # Data::Dumper actually does much more than this, however, it
21 # does not stringify hash's in a consistent manner. i.e. no SORT
22 #
23 # The routine below, while not covering recursion loops, non ascii
24 # characters, etc.... does produce text that can be eval'd and is
25 # consistent with each rendering.
26 #
27 sub hexDumper {
28 264 ▶ if (wantarray) {
Subroutine hexDumper: called CC=2 SCAR=9.2
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 264 264
29 0 ($data,$count) = Dumper($_[0]);
30 0 $data =~ s/(\b\d+)/sprintf("0x%x",$1)/ge;
31 0 return ($data,$count);
32 }
33 264 (my $x = Dumper($_[0])) =~ s/(\b\d+)/sprintf("0x%x",$1)/ge;
34 264 $x;
35 }
36  
37 sub Dumper {
38 277 ▶ unless (defined $_[0]) {
Subroutine Dumper: called CC=10 SCAR=26.4
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 277 277
39 0 ▶ return ("undef\n",'undef') if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
40 0 return "undef\n";
41 }
42 277 my $ref = ref $_[0];
43 277 ▶ return "not a reference\n" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 277 277
44 277 ▶ unless ($ref eq 'HASH' or $ref eq 'ARRAY' or $ref eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "HASH" or $ref eq "ARRAY" or $ref eq "SCALAR") 11 266 277
Condition66%4/6
$ref eq "HASH" or $ref eq "ARRAY" or $ref eq "SCALAR"
l!l&&r!l&&!r
266011
$ref eq "HASH" or $ref eq "ARRAY"
l!l&&r!l&&!r
026611
MC/DC33%1/3
$ref eq "HASH" or $ref eq "ARRAY" or $ref eq "SCALAR"
$ref eq "HASH" $ref eq "ARRAY" $ref eq "SCALAR"
Truth table50%2/4
A or B or C
A= $ref eq "HASH"B= $ref eq "ARRAY"C= $ref eq "SCALAR"
ABCresult
1XX1
01X1
0011
0000
45 11 ($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/);
46 }
47 277 my $p = {
48 depth => 0,
49 elements => 0,
50 };
51 277 (my $pkg = (caller(0))[3]) =~ s/(.+)::Dumper/$1/;
52 277 bless $p,$pkg;
53 277 my $data;
54 277 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 277 277
elsif ($ref eq 'ARRAY') { } 277 0 277
55 0 $data = $p->hash_recurse($_[0],"\n");
56 }
57 elsif ($ref eq 'ARRAY') {
58 277 $data = $p->array_recurse($_[0]);
59 } else {
60 # return $ref ." unsupported\n";
61 0 $data = $p->scalar_recurse($_[0]);
62 }
63 277 $data =~ s/,\n$/;\n/;
64 277 ▶ return ($data,$p->{elements}) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 277 277
65 277 return $p->{elements} ."\t= ". $data;
66 }
67
68 # input: pointer to scalar, terminator
69 # returns data
70 #
71 sub scalar_recurse {
72 0 ▶ my($p,$ptr,$n) = @_;
Subroutine scalar_recurse: not called CC=2 SCAR=17.9
73 0 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
74 0 my $data = "\\";
75 0 $data .= _dump($p,$$ptr);
76 0 $data .= "\n";
77 }
78  
79 # input: pointer to hash, terminator
80 # returns: data
81 #
82 sub hash_recurse {
83 0 ▶ my($p,$ptr,$n) = @_;
Subroutine hash_recurse: not called CC=3 SCAR=24.8
84 0 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
85 0 my $data = "{\n";
86 0 foreach my $key (sort keys %$ptr) {
87 0 $data .= "\t'". $key ."'\t=> ";
88 0 $data .= _dump($p,$ptr->{$key},"\n");
89 }
90 0 $data .= '},'.$n;
91 }
92  
93 # generate a unique signature for a particular array
94 #
95 # input: pointer to array, terminator
96 # returns: data
97 sub array_recurse {
98 277 ▶ my($p,$ptr,$n) = @_;
Subroutine array_recurse: called CC=3 SCAR=11.0
99 277 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 277 0 277
100 277 my $data = '[';
101 277 foreach my $item (@$ptr) {
102 1308 $data .= _dump($p,$item);
103 }
104 277 $data .= "],\n";
105 }
106  
107 # input: self, item, append
108 # return: data
109 #
110 sub _dump {
111 1308 ▶ my($p,$item,$n) = @_;
Subroutine _dump: called CC=9 SCAR=30.0
112 1308 $p->{elements}++;
113 1308 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 1308 0 1308
114 1308 my $ref = ref $item;
115 1308 ▶ if ($ref eq 'HASH') {
Branch: 7 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 0 1308 1308
elsif ($ref eq 'ARRAY') { } 0 1308 1308
elsif ($ref eq 'SCALAR') { } 0 1308 1308
elsif ($ref eq 'GLOB') { } 0 1308 1308
elsif (do { my $g = \$item; ref $g eq 'GLOB' }) { } 0 1308 1308
elsif ($ref eq 'CODE') { } 0 1308 1308
elsif (defined $item) { } 1308 0 1308
116 0 return tabout($p->hash_recurse($item,"\n"));
117 }
118 elsif($ref eq 'ARRAY') {
119 0 return $p->array_recurse($item,$n);
120 }
121 elsif($ref eq 'SCALAR') {
122 # return q|\$SCALAR,|.$n;
123 0 return($p->scalar_recurse($item,$n));
124 }
125 elsif ($ref eq 'GLOB') {
126 0 my $g = *{$item};
127 0 return "\\$g" .','.$n;
128 }
129 1308 elsif(do {my $g = \$item; ref $g eq 'GLOB'}) {
130 0 return "$item" .','.$n;
131 }
132 elsif($ref eq 'CODE') {
133 0 return q|sub {'DUMMY'},|.$n;
134 }
135 elsif (defined $item) {
136 1308 return wrap_data($item) .','.$n;
137 }
138 else {
139 0 return 'undef,'.$n;
140 }
141 }
142  
143 sub tabout {
144 0 ▶ my @data = split(/\n/,shift);
Subroutine tabout: not called CC=2 SCAR=17.9
145 0 my $data = shift @data;
146 0 $data .= "\n";
147 0 foreach(@data) {
148 0 $data .= "\t$_\n";
149 }
150 0 $data;
151 }
152  
153 sub wrap_data {
154 1308 ▶ my $data = shift;
Subroutine wrap_data: called CC=3 SCAR=11.1
155 1308 ▶ return ($data =~ /\D/ || $data =~ /^$/)
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /\D/ || $data =~ /^$/ ? : 6 1302 1308
Condition66%2/3
$data =~ /\D/ or $data =~ /^$/
l!l&&r!l&&!r
601302
MC/DC50%1/2
$data =~ /\D/ or $data =~ /^$/
$data =~ /\D/ $data =~ /^$/
Truth table66%2/3
A or B
A= $data =~ /\D/B= $data =~ /^$/
ABresult
1X1
011
000
156 ? q|'|. $data .q|'|
157 : $data;
158 }
159  
160 7 1;
↑ Summary blib/lib/Math/Base/Convert.pm »