recurse2txt

filter: Statementstmt 82.1% 46 / 56 Branchbran 62.5% 20 / 32 Conditioncond 66.6% 4 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 74.2% 78 / 105 SCAR 34.0
CC
22
Cov
74%
_dump
15.2
Dumper
7.9
wrap_data
3.0
CRAP 30.1
« blib/lib/Net/DNSBL/Statistics.pm ↑ Summary local/test.conf »
1 #!/usr/bin/perl
2 #
3 # recurse2txt routines
4 #
5 # version 1.03, 5-19-06, michael@bizsystems.com
6 #
7 #use strict;
8 #use diagnostics;
9 6 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # generate a unique signature for a particular hash
12 #
13 # Data::Dumper actually does much more than this, however, it
14 # does not stringify hash's in a consistent manner. i.e. no SORT
15 #
16 # The routine below, while not covering recursion loops, non ascii
17 # characters, etc.... does produce text that can be eval'd and is
18 # consistent with each rendering.
19 #
20 sub Dumper {
21 17 ▶ return "undef\n" unless defined $_[0];
Subroutine Dumper: called CC=7 SCAR=20.6
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 17 17
22 17 my $ref = ref $_[0];
23 17 ▶ return "not a reference\n" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 17 17
24 17 ▶ unless ($ref eq 'HASH' or $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "HASH" or $ref eq "ARRAY") 0 17 17
Condition66%2/3
$ref eq "HASH" or $ref eq "ARRAY"
l!l&&r!l&&!r
980
MC/DC0%0/2
$ref eq "HASH" or $ref eq "ARRAY"
$ref eq "HASH" $ref eq "ARRAY"
Truth table66%2/3
A or B
A= $ref eq "HASH"B= $ref eq "ARRAY"
ABresult
1X1
011
000
25 0 ($ref) = (overload::StrVal($_[0]) =~ /^(?:.*\=)?([^=]*)\(/);
26 }
27 17 my $p = {
28 depth => 0,
29 elements => 0,
30 };
31 17 bless $p,__PACKAGE__;
32 17 my $data;
33 17 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 9 8 17
elsif ($ref eq 'ARRAY') { } 8 0 8
34 9 $data = $p->hash_recurse($_[0],"\n");
35 }
36 elsif ($ref eq 'ARRAY') {
37 8 $data = $p->array_recurse($_[0]);
38 } else {
39 0 return $ref ." unsupported\n";
40 }
41 17 $data =~ s/,\n$/;\n/;
42 17 return $p->{elements} ."\t= ". $data;
43 }
44
45 # input: pointer to hash, terminator
46 # returns: data
47 #
48 sub hash_recurse {
49 40 ▶ my($p,$ptr,$n) = @_;
Subroutine hash_recurse: called CC=3 SCAR=11.0
50 40 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 40 40
51 40 my $data = "{\n";
52 40 foreach my $key (sort keys %$ptr) {
53 106 $data .= "\t'". $key ."'\t=> ";
54 106 $data .= _dump($p,$ptr->{$key},"\n");
55 }
56 40 $data .= '},'.$n;
57 }
58  
59 # generate a unique signature for a particular array
60 #
61 # input: pointer to array, terminator
62 # returns: data
63 sub array_recurse {
64 8 ▶ my($p,$ptr,$n) = @_;
Subroutine array_recurse: called CC=3 SCAR=11.0
65 8 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 8 0 8
66 8 my $data = '[';
67 8 foreach my $item (@$ptr) {
68 184 $data .= _dump($p,$item);
69 }
70 8 $data .= "],\n";
71 }
72  
73 # input: self, item, append
74 # return: data
75 #
76 sub _dump {
77 290 ▶ my($p,$item,$n) = @_;
Subroutine _dump: called CC=9 SCAR=27.2
78 290 $p->{elements}++;
79 290 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 184 106 290
80 290 my $ref = ref $item;
81 290 ▶ if ($ref eq 'HASH') {
Branch: 7 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 31 259 290
elsif ($ref eq 'ARRAY') { } 0 259 259
elsif ($ref eq 'SCALAR') { } 0 259 259
elsif ($ref eq 'GLOB') { } 0 259 259
elsif (do { my $g = \$item; ref $g eq 'GLOB' }) { } 0 259 259
elsif ($ref eq 'CODE') { } 0 259 259
elsif (defined $item) { } 259 0 259
82 31 return tabout($p->hash_recurse($item,"\n"));
83 }
84 elsif($ref eq 'ARRAY') {
85 0 return $p->array_recurse($item,$n);
86 }
87 elsif($ref eq 'SCALAR') {
88 0 return q|\$SCALAR,|.$n;
89 }
90 elsif ($ref eq 'GLOB') {
91 0 my $g = *{$item};
92 0 return "\\$g" .','.$n;
93 }
94 259 elsif(do {my $g = \$item; ref $g eq 'GLOB'}) {
95 0 return "$item" .','.$n;
96 }
97 elsif($ref eq 'CODE') {
98 0 return q|sub {'DUMMY'},|.$n;
99 }
100 elsif (defined $item) {
101 259 return wrap_data($item) .','.$n;
102 }
103 else {
104 0 return 'undef,'.$n;
105 }
106 }
107  
108 sub tabout {
109 31 ▶ my @data = split(/\n/,shift);
Subroutine tabout: called CC=2 SCAR=6.9
110 31 my $data = shift @data;
111 31 $data .= "\n";
112 31 foreach(@data) {
113 82 $data .= "\t$_\n";
114 }
115 31 $data;
116 }
117  
118 sub wrap_data {
119 259 ▶ my $data = shift;
Subroutine wrap_data: called CC=3 SCAR=11.1
120 259 ▶ return ($data =~ /\D/ || $data =~ /^$/)
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /\D/ || $data =~ /^$/ ? : 184 75 259
Condition66%2/3
$data =~ /\D/ or $data =~ /^$/
l!l&&r!l&&!r
184075
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
121 ? q|'|. $data .q|'|
122 : $data;
123 }
124  
125 6 1;
« blib/lib/Net/DNSBL/Statistics.pm ↑ Summary local/test.conf »