recurse2txt

filter: Statementstmt 87.5% 49 / 56 Branchbran 71.8% 23 / 32 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 81.9% 86 / 105 CC 22 SCAR 18.5
CC
22
Cov
82%
_dump
10.2
Dumper
7.9
array_recurse
3.0
CRAP 6.4
« blib/lib/Mail/SMTP/Honeypot.pm ↑ Summary
1 #!/usr/bin/perl
2 #
3 # recurse2txt routines
4 #
5 # version 1.02, 8-13-05, michael@bizsystems.com
6 #
7 #use strict;
8 #use diagnostics;
9 3 ▶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 129 ▶sub Dumper {
Subroutine Dumper: called CC=7 SCAR=20.6
21 129 ▶ return "undef\n" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 129 129
22 129 my $ref = ref $_[0];
23 129 ▶ return "not a reference\n" unless $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 129 129
24 129 ▶ unless ($ref eq 'HASH' or $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref eq "HASH" or $ref eq "ARRAY") 0 129 129
Condition66%2/3
$ref eq "HASH" or $ref eq "ARRAY"
l!l&&r!l&&!r
112170
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 129 my $p = {
28 depth => 0,
29 elements => 0,
30 };
31 129 bless $p,__PACKAGE__;
32 129 my $data;
33 129 ▶ if ($ref eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 112 17 129
elsif ($ref eq 'ARRAY') { } 17 0 17
34 112 $data = $p->hash_recurse($_[0],"\n");
35 }
36 elsif ($ref eq 'ARRAY') {
37 17 $data = $p->array_recurse($_[0]);
38 } else {
39 0 return $ref ." unsupported\n";
40 }
41 129 $data =~ s/,\n$/;\n/;
42 129 return $p->{elements} ."\t = ". $data;
43 }
44
45 # input: pointer to hash, terminator
46 # returns: data
47 #
48 220 ▶sub hash_recurse {
Subroutine hash_recurse: called CC=3 SCAR=11.0
49 220 my($p,$ptr,$n) = @_;
50 220 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 220 220
51 220 my $data = "{\n";
52 220 foreach my $key (sort keys %$ptr) {
53 1034 $data .= "\t'". $key ."'\t=> ";
54 1034 $data .= _dump($p,$ptr->{$key},"\n");
55 }
56 220 $data .= '},'.$n;
57 }
58  
59 # generate a unique signature for a particular array
60 #
61 # input: pointer to array, terminator
62 # returns: data
63 17 ▶sub array_recurse {
Subroutine array_recurse: called CC=3 SCAR=11.0
64 17 my($p,$ptr,$n) = @_;
65 17 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 17 0 17
66 17 my $data = '[';
67 17 foreach my $item (@$ptr) {
68 10 $data .= _dump($p,$item);
69 }
70 17 $data .= "],\n";
71 }
72  
73 # input: self, item, append
74 # return: data
75 #
76 1044 ▶sub _dump {
Subroutine _dump: called CC=9 SCAR=23.2
77 1044 my($p,$item,$n) = @_;
78 1044 $p->{elements}++;
79 1044 ▶ $n = '' unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 10 1034 1044
80 1044 my $ref = ref $item;
81 1044 ▶ if ($ref eq 'HASH') {
Branch: 7 branches
BranchTrueFalseTotal
if ($ref eq 'HASH') { } 108 936 1044
elsif ($ref eq 'ARRAY') { } 0 936 936
elsif ($ref eq 'SCALAR') { } 0 936 936
elsif ($ref eq 'GLOB') { } 0 936 936
elsif (do { my $g = \$item; ref $g eq 'GLOB' }) { } 30 906 936
elsif ($ref eq 'CODE') { } 130 776 906
elsif (defined $item) { } 726 50 776
82 108 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 936 elsif(do {my $g = \$item; ref $g eq 'GLOB'}) {
95 30 return "$item" .','.$n;
96 }
97 elsif($ref eq 'CODE') {
98 130 return q|sub {'DUMMY'},|.$n;
99 }
100 elsif (defined $item) {
101 726 return wrap_data($item) .','.$n;
102 }
103 else {
104 50 return 'undef,'.$n;
105 }
106 }
107  
108 108 ▶sub tabout {
Subroutine tabout: called CC=2 SCAR=6.9
109 108 my @data = split(/\n/,shift);
110 108 my $data = shift @data;
111 108 $data .= "\n";
112 108 foreach(@data) {
113 366 $data .= "\t$_\n";
114 }
115 108 $data;
116 }
117  
118 726 ▶sub wrap_data {
Subroutine wrap_data: called CC=3 SCAR=11.0
119 726 my $data = shift;
120 726 ▶ return ($data =~ /\D/ || $data =~ /^$/)
Branch: 1 branch
BranchTrueFalseTotal
$data =~ /\D/ || $data =~ /^$/ ? : 610 116 726
Condition100%3/3
$data =~ /\D/ or $data =~ /^$/
l!l&&r!l&&!r
59614116
MC/DC100%2/2
$data =~ /\D/ or $data =~ /^$/
$data =~ /\D/ $data =~ /^$/
Truth table100%3/3
A or B
A= $data =~ /\D/B= $data =~ /^$/
ABresult
1X1
011
000
121 ? q|'|. $data .q|'|
122 : $data;
123 }
124  
125 3 1;
« blib/lib/Mail/SMTP/Honeypot.pm ↑ Summary