blib/lib/Data/Abridge.pm

filter: Statementstmt 96.7% 120 / 124 Branchbran 70.4% 31 / 44 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 96.6% 29 / 30 Podpod 100% 4 / 4 totaltotal 91.3% 189 / 207 CC 26 SCAR 12.7
CC
26
Cov
90%
_abridge_recursive
7.0
_process_object
6.1
abridge_item
6.0
CRAP 3.6
↑ Summary
1 package Data::Abridge;
2 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 $Data::Abridge::VERSION = '0.03.01';
4 }
5  
6 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util qw( blessed reftype refaddr );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 our @EXPORT_OK = qw(
16 abridge_item abridge_items
17 abridge_recursive abridge_items_recursive
18 );
19  
20  
21 5 ▶use constant BLESSED_REGEXP => blessed qr/foo/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use constant REFTYPE_REGEXP => reftype qr/foo/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # Munge a thing for nice serialization
25  
26 # Object -> { 'Package::Name' => <unblessed copy> };
27 # Code Ref -> '\&subname'
28 # -> '\&__ANON__'
29 # Scalar Ref -> { SCALAR => $scalar }
30 # Glob Ref -> '\*main:glob'
31  
32 5 my %SLOB_DISPATCH = (
33 SCALAR => \&_process_scalar,
34 REF => \&_process_ref,
35 HASH => \&_passthrough,
36 ARRAY => \&_passthrough,
37 GLOB => \&_process_glob,
38 CODE => \&_process_code,
39 BLESSED => \&_process_object,
40 REGEXP => \&_process_regexp,
41 );
42  
43 5 my %COPY_DISPATCH = (
44 SCALAR => \&_process_scalar,
45 REF => \&_process_ref,
46 HASH => \&_process_hash,
47 ARRAY => \&_process_array,
48 GLOB => \&_process_glob,
49 CODE => \&_process_code,
50 REGEXP => \&_process_regexp,
51 );
52  
53 5 my %RECURSE_DISPATCH = (
54 REF => \&_recurse_ref,
55 HASH => \&_recurse_hash,
56 ARRAY => \&_recurse_array,
57 BLESSED => \&_recurse_object,
58 );
59  
60 5 our $HONOR_STRINGIFY = 1;
61  
62 5 our %SEEN; # Global hash for tracking self-referential structures.
63 # Should be localized by entry to recursive abridge functions.
64 5 our @PATH; # Also localized for tracking the current path to any given entry
65 # in the abridged structure.
66  
67 37 ▶sub _passthrough { return $_ }
Subroutine _passthrough: called CC=1 SCAR=0.0
68 10 ▶sub _process_ref { return { SCALAR => $$_ } }
Subroutine _process_ref: called CC=1 SCAR=0.0
69 5 ▶sub _process_glob { return { GLOB => '\\'.*$_ } }
Subroutine _process_glob: called CC=1 SCAR=0.0
70 14 ▶sub _process_hash { return {%$_} }
Subroutine _process_hash: called CC=1 SCAR=0.0
71 3 ▶sub _process_array { return [@$_] }
Subroutine _process_array: called CC=1 SCAR=0.0
72 8 ▶sub _process_scalar { return { SCALAR => $$_} }
Subroutine _process_scalar: called CC=1 SCAR=0.0
73 2 ▶sub _process_regexp { return { Regexp => "$_" } }
Subroutine _process_regexp: called CC=1 SCAR=0.0
74  
75 29 ▶sub _process_object {
Subroutine _process_object: called CC=6 SCAR=18.2
76 29 my $obj = $_;
77  
78 29 my $class = blessed $obj;
79 29 ▶ return unless defined $class;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 29 29
80  
81 29 ▶ if( $HONOR_STRINGIFY && overload::Method($obj, '""') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::Abridge::HONOR_STRINGIFY and overload::Method($obj, '""')) { } 3 26 29
Condition100%3/3
$Data::Abridge::HONOR_STRINGIFY and overload::Method($obj, "\"\"")
!ll&&!rl&&r
1253
MC/DC100%2/2
$Data::Abridge::HONOR_STRINGIFY and overload::Method($obj, "\"\"")
$Data::Abridge::HONOR_STRINGIFY overload::Method($obj, "\"\"")
Truth table100%3/3
A and B
A= $Data::Abridge::HONOR_STRINGIFYB= overload::Method($obj, "\"\"")
ABresult
0X0
100
111
82 # overloads String ?
83 3 return "$obj";
84 }
85 else {
86 # Shallow Copy
87 26 ▶ my $type = _is_Regexp( $obj )
Branch: 1 branch
BranchTrueFalseTotal
_is_Regexp($obj) ? : 0 26 26
88 ? 'REGEXP'
89 : reftype $obj;
90  
91 my $value = exists $COPY_DISPATCH{$type}
92 26 ▶ ? $COPY_DISPATCH{$type}->()
Branch: 1 branch
BranchTrueFalseTotal
exists $COPY_DISPATCH{$type} ? : 26 0 26
93 : _unsupported_type( $obj );
94  
95 26 return { $class => $value };
96 }
97 }
98  
99 6 ▶sub _process_code {
Subroutine _process_code: called CC=3 SCAR=11.2
100 6 require B;
101 6 my $cv = B::svref_2object($_);
102 6 ▶ $cv->isa('B::CV') or return;
Branch: 1 branch
BranchTrueFalseTotal
$cv->isa("B::CV") or return 6 0 6
103  
104 # bail out if GV is undefined
105 6 ▶ $cv->GV->isa('B::SPECIAL') and return;
Branch: 1 branch
BranchTrueFalseTotal
$cv->GV->isa("B::SPECIAL") and return 0 6 6
106  
107 6 my $subname = join "::", $cv->GV->STASH->NAME, $cv->GV->NAME;
108 6 return {CODE => "\\&$subname"};
109 }
110  
111 0 ▶sub _unsupported_type {
Subroutine _unsupported_type: not called CC=1 SCAR=6.9
112 0 my $item = shift;
113 0 my $type = reftype $item;
114  
115 0 return "Unsupported type: '$type' for $item";
116 }
117  
118 26 ▶sub _is_Regexp {
Subroutine _is_Regexp: called CC=3 SCAR=12.0
119 26 require B;
120 26 my $sv = B::svref_2object($_);
121 26 ▶ $sv->isa('B::PVMG') or return;
Branch: 1 branch
BranchTrueFalseTotal
$sv->isa("B::PVMG") or return 26 0 26
122 26 ▶ my $m = $sv->MAGIC or return;
Branch: 1 branch
BranchTrueFalseTotal
my $m = $sv->MAGIC or return 0 26 26
123  
124 0 return $m->TYPE eq 'r';
125 }
126  
127 4 ▶sub abridge_items {
Subroutine abridge_items: called CC=1 SCAR=0.0
Pod documented
128 4 return [ map abridge_item($_), @_ ];
129 }
130  
131 109 ▶sub abridge_item {
Subroutine abridge_item: called CC=6 SCAR=17.9
Pod documented
132 109 my $item = shift;
133  
134 109 my $type = reftype $item;
135  
136 109 ▶ return $item unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 21 88 109
137  
138 88 my $blessed = blessed $item;
139 88 ▶ if( $blessed ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed) 30 58 88
140 30 ▶ $type = $blessed eq BLESSED_REGEXP ? 'REGEXP' : 'BLESSED';
Branch: 1 branch
BranchTrueFalseTotal
$blessed eq 'Regexp' ? : 1 29 30
141 }
142  
143 88 my $slobd = $SLOB_DISPATCH{$type};
144 88 ▶ $slobd = \&_unsupported_type unless defined $slobd;;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slobd 0 88 88
145  
146 88 return $slobd->($_) for $item;
147 }
148  
149  
150 5 ▶sub _recurse_ref {
Subroutine _recurse_ref: called CC=1 SCAR=0.0
151 5 my $processed_ref = shift;
152  
153 5 my $val = $processed_ref->{SCALAR};
154  
155 5 push @PATH, 'SCALAR';
156 5 $processed_ref->{SCALAR} = _abridge_recursive($val);
157 5 pop @PATH;
158  
159 5 return $processed_ref;
160 }
161  
162 16 ▶sub _recurse_array {
Subroutine _recurse_array: called CC=1 SCAR=0.0
163 16 my $processed_array = shift;
164  
165 my @result = map {
166 16 push @PATH, $_;
167 29 my @a = _abridge_recursive($processed_array->[$_]);
168 29 pop @PATH;
169 29 @a;
170 } 0 .. $#$processed_array;
171  
172 16 return \@result;
173 }
174  
175 20 ▶sub _recurse_hash {
Subroutine _recurse_hash: called CC=2 SCAR=6.9
176 20 my $processed_hash = shift;
177  
178 20 my %new_hash;
179 20 for my $k ( keys %$processed_hash ) {
180 24 push @PATH, $k;
181 24 $new_hash{$k} = _abridge_recursive( $processed_hash->{$k} );
182 24 pop @PATH;
183 }
184  
185 20 return \%new_hash;
186 }
187  
188 13 ▶sub _recurse_object {
Subroutine _recurse_object: called CC=5 SCAR=16.6
189 13 my $processed_object = shift;
190  
191 13 ▶ return unless ref $processed_object;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $processed_object 0 13 13
192 13 ▶ return unless reftype $processed_object eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::reftype($processed_object) eq "HASH" 0 13 13
193  
194 13 my ( $key, $value ) = each %$processed_object;
195 13 my $type = reftype $value;
196 13 ▶ $type = '' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 0 13 13
197  
198  
199 13 push @PATH, $key;
200  
201 $value = $RECURSE_DISPATCH{$type}->( $value )
202 13 ▶ if exists $RECURSE_DISPATCH{$type};
Branch: 1 branch
BranchTrueFalseTotal
if exists $RECURSE_DISPATCH{$type} 13 0 13
203  
204 13 pop @PATH;
205  
206 13 my %new_obj = ( $key => $value );
207  
208 13 return \%new_obj;
209 }
210  
211  
212 25 ▶sub abridge_recursive {
Subroutine abridge_recursive: called CC=1 SCAR=0.0
Pod documented
213 25 local %SEEN;
214 25 local @PATH;
215 25 &_abridge_recursive;
216 }
217  
218 88 ▶sub _abridge_recursive {
Subroutine _abridge_recursive: called CC=7 SCAR=19.5
219 88 my $item = shift;
220  
221 88 my $type = reftype $item;
222 88 ▶ $type = 'BLESSED' if blessed $item;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($item) 21 67 88
223 88 ▶ $type = '' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 20 68 88
224  
225 88 my $repl = abridge_item($item);
226  
227 # repl may have become a plain old scalar.
228 # Can't recurse that.
229 88 my $repl_type = reftype $repl;
230 88 ▶ return $repl unless defined $repl_type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $repl_type 22 66 88
231  
232 66 ▶ if ( exists $RECURSE_DISPATCH{$type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $RECURSE_DISPATCH{$type}) 59 7 66
233 59 my $id = refaddr $item;
234 59 ▶ $id = '' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 59 59
235  
236 18 return { SEEN => [ @{$SEEN{$id}} ] }
237 59 ▶ if exists $SEEN{$id};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Data::Abridge::SEEN{$id} 18 41 59
238  
239 41 $SEEN{$id} = [@PATH];
240 41 $repl = $RECURSE_DISPATCH{$type}->($repl);
241  
242 }
243  
244 48 return $repl;
245 }
246  
247 5 ▶sub abridge_items_recursive {
Subroutine abridge_items_recursive: called CC=1 SCAR=0.0
Pod documented
248 5 local %SEEN;
249 5 &_abridge_items_recursive;
250 }
251  
252 5 ▶sub _abridge_items_recursive {
Subroutine _abridge_items_recursive: called CC=1 SCAR=0.0
253 5 return _abridge_recursive([@_]);
254 }
255  
256 5 1;
257  
258 # ABSTRACT: Simplify data structures for naive serialization.
259  
260 __END__
↑ Summary