blib/lib/Data/Reuse.pm

filter: Statementstmt 96.9% 127 / 131 Branchbran 82.2% 51 / 62 Conditioncond 41.6% 5 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 93.7% 15 / 16 Podpod 75.0% 3 / 4 totaltotal 87.1% 203 / 233 CC 39 SCAR 25.2
CC
39
Cov
89%
reuse
22.5
spread
10.8
fixate
7.1
CRAP 12.5
↑ Summary
1 package Data::Reuse;
2  
3 # runtime settings we need
4 4 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # set up version info
9 4 our $VERSION= '0.13';
10  
11 # we need this otherwise nothing works
12 4 ▶use Data::Alias qw( alias copy );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # other modules we need
15 4 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Digest::MD5 qw( md5 );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Encode qw( encode_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =for Explanation:
21 Since Data::Alias uses Exporter, we might as well do that also. Otherwise
22 we'd probably hack an import method ourselves
23  
24 =cut
25  
26 4 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 our @EXPORT= qw();
28 4 our @EXPORT_OK= qw( alias fixate forget reuse spread );
29 4 our %EXPORT_TAGS= ( all => \@EXPORT_OK );
30  
31 # ID prefixes
32 4 my $U= "\1U\1";
33 4 my $S= "\1S\1";
34 4 my $A= "\1A\1";
35 4 my $H= "\1H\1";
36 52  
37 # set up data store with predefined ro undef value
38 4 my %reuse;
39 4 forget();
40  
41 # mark constants as read only
42 4 Internals::SvREADONLY $_, 1 foreach ( $U, $S, $A, $H );
43  
44 # recursion level
45 4 my $level= 0;
46  
47 # references being handled
48 4 my %handling;
49  
50 # satisfy -require-
51 4 1;
52  
53 #---------------------------------------------------------------------------
54 # reuse
55 #
56 # Make given values, store them in the constant hash and return them
57 #
58 # IN: 1..N values to be folded into constants
59 # OUT: 1..N same as input, but read-only and folded
60  
61 sub reuse (@); # needed because of recursion
62 96 ▶sub reuse (@) {
Subroutine reuse: called CC=22 SCAR=31.1
Pod documented
63  
64 6 # being called with return values
65 96 alias my $not_void = defined wantarray;
66 # we're one level deeper
67 96 $level++;
68  
69 # there are values specified that haven't been folded yet or are undef
70 96 ▶ if ( alias my @special= grep { !defined or !exists $reuse{$_} } @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (alias(scalar(my(@special) = grep({not defined $_ && exists $reuse{$_};} @_)))) 70 26 96
Condition100%3/3
defined $_ && exists $reuse{$_}
!ll&&!rl&&r
48059
MC/DC100%2/2
defined $_ && exists $reuse{$_}
defined $_ exists $reuse{$_}
Truth table100%3/3
A && B
A= defined $_B= exists $reuse{$_}
ABresult
0X0
100
111
71  
72 # values specified that haven't been folded yet
73 70 ▶ if ( alias my @new= grep { defined() } @special ) {
Branch: 1 branch
BranchTrueFalseTotal
if (alias(scalar(my(@new) = grep({defined $_;} @special)))) 66 4 70
74 66 foreach (@new) { # natural aliasing
75  
76 # reference being handled, make sure it is known
77 80 ▶ if ( exists $handling{$_} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $handling{$_}) { } 3 77 80
elsif (my $reftype = builtin::reftype($_)) { } 52 25 77
elsif (exists $reuse{$_}) { } 2 23 25
78 3 ▶ alias $reuse{$_} ||= $_;
Condition0%0/3
$reuse{$_} ||= $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$reuse{$_} ||= $_
$reuse{$_} $_
Truth table0%0/3
A ||= B
A= $reuse{$_}B= $_
ABresult
1X1
011
000
79 }
80  
81 # handle references
82 elsif ( my $reftype= reftype $_ ) {
83 52 $handling{$_}= $level;
84 52 my $id;
85  
86 # aliasing everything in here
87 alias {
88  
89 # all elements of list
90 52 ▶ if ( $reftype eq 'ARRAY' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 34 18 52
elsif ($reftype eq 'HASH') { } 12 6 18
elsif ($reftype eq 'SCALAR') { } 6 0 6
91 34 $id= _list_id( $_ );
92  
93 # not seen, recurse
94 34 ▶ if ( !exists $reuse{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $reuse{$id}) 25 9 34
95 25 my @list= @{$_};
96 25 (@list)= reuse @list;
97  
98 =for Explanation:
99 We need to use copy semantics, because aliasing semantics segfaults.
100 Or as Matthijs put it:
101 =
102 Hah, het is een combinatie van:
103 1. bug in perl m.b.t. \-prototypes (\[$%@] in dit geval)
104 2. de refgen operator van Data::Alias (de impliciete \)
105 3. het in void context zijn van dit blok
106 4. totaal gebrek aan argument-checking in Internals::SvREADONLY
107 het prototype maakt van:
108 =
109 Internals::SvREADONLY my @array, 1
110 =
111 zoiets als:
112 =
113 &Internals::SvREADONLY(\my @array, 1);
114 =
115 Echter hij markeert de \ zonder enige context. Dit hoort normaal alleen
116 de gebeuren voor de laatste expressie in een block, en betekent "evalueer
117 dit in de context van deze block". In dit geval is het omliggende block
118 de top-level code, en die is altijd in void context. Perl evalueert dus
119 de \ in void context.
120 =
121 Hier ben je echter in perl zelf geen last van, omdat perl's ingebouwde
122 refgen op (\) alleen maar test op list-context, en gaat er van uit dat
123 het anders scalar context is. D::A's refgen onderscheid alle drie de
124 contexts, en produceert dus niets omdat het in void context is.
125 =
126 Hierdoor wordt de call dus:
127 =
128 &Internals::SvREADONLY(1);
129 =
130 En zoals ik al zei, Internals::SvREADONLY heeft geen argument validatie
131 en probeert dus fijn op adres 0x00000001 te lezen.. SEGV
132  
133 =cut
134  
135 # mark readonly, see above
136 25 copy Internals::SvREADONLY @list, 1;
137  
138 # recursive structures may be replaced
139 25 $id= _list_id( $_ );
140 }
141 }
142  
143 # all values of hash
144 elsif ( $reftype eq 'HASH' ) {
145 12 $id= _hash_id( $_ );
146  
147 # not seen, recurse, set result if first
148 12 ▶ if ( !$reuse{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reuse{$id}) 6 6 12
149 6 my %hash= %{$_};
150 6 ( @hash{ keys %hash } )= reuse values %hash;
151  
152 # mark readonly, see above
153 6 copy Internals::SvREADONLY %hash, 1;
154  
155 # recursive structures may be replaced
156 6 $id= _hash_id( $_ );
157 }
158 }
159  
160 # the value of a scalar ref
161 elsif ( $reftype eq 'SCALAR' ) {
162 6 my $scalar= ${$_};
163  
164 # may be reused
165 6 ▶ if ( defined $scalar ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scalar) { } 5 1 6
166 5 $id= md5( $S . $scalar );
167  
168 # not seen, recurse, set result if first
169 5 ▶ if ( !$reuse{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reuse{$id}) 2 3 5
170 2 ($scalar) = reuse $scalar;
171 2 copy Internals::SvREADONLY $scalar, 1;
172 # recursive structures may be replaced
173 2 $id= md5( $S . $scalar );
174 }
175 }
176  
177 # always reuse the default undef value
178 else {
179 1 $id= $U;
180 }
181 }
182 # huh?
183 else {
184 0 $id = md5( $S . $_ );
185 }
186  
187 =for Explanation:
188 When called in void context, perl may actually have used a memory location
189 for a temporary data structure that may return later with a different
190 content. As we don't want to equate those two different structures, we
191 are not going to save this reference if called in void context. And we
192 are also not going to overwrite anything that's there already.
193  
194 =cut
195 52 ▶ $reuse{$id} ||= $_ if $not_void;
Branch: 1 branch
BranchTrueFalseTotal
if $not_void 38 14 52
Condition0%0/3
$reuse{$id} ||= $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$reuse{$id} ||= $_
$reuse{$id} $_
Truth table0%0/3
A ||= B
A= $reuse{$id}B= $_
ABresult
1X1
011
000
196  
197 # store in data store
198 52 ▶ $reuse{$_} = $reuse{$id} || $_;
Condition66%2/3
$reuse{$id} || $_
l!l&&r!l&&!r
38140
MC/DC0%0/2
$reuse{$id} || $_
$reuse{$id} $_
Truth table66%2/3
A || B
A= $reuse{$id}B= $_
ABresult
1X1
011
000
199 }; #alias
200  
201 # done handling this ref
202 52 delete $handling{$_};
203 }
204
205 # not a ref, but now already in store
206 elsif ( exists $reuse{$_} ) {
207 }
208  
209 # not a ref, and not in store either
210 else {
211  
212 # not readonly already, make a read only copy
213 23 ▶ $_= $_, Internals::SvREADONLY $_, 1
Branch: 1 branch
BranchTrueFalseTotal
unless &Internals::SvREADONLY(\$_) 8 15 23
214 if !Internals::SvREADONLY $_;
215  
216 # store in data store
217 23 alias $reuse{$_}= $_;
218 }
219 }
220 }
221 }
222  
223 # done on this level
224 96 $level--;
225  
226 # return aliases of the specified values if needed
227 96 ▶ alias return @reuse{ map { defined() ? $_ : $U } @_ } if $not_void;
Branch: 2 branches
BranchTrueFalseTotal
if $not_void 82 14 96
defined $_ ? : 123 4 127
228 } #reuse
229  
230 #---------------------------------------------------------------------------
231 # fixate
232 #
233 # Fixate the values of the given hash / array ref
234 #
235 # IN: 1 hash / array ref
236 # 2..N values for fixation
237  
238 9 ▶sub fixate (\[@%]@) {
Subroutine fixate: called CC=7 SCAR=19.7
Pod documented
239  
240 # fetch structure
241 9 alias my $struct= shift;
242 9 ▶ croak "Must specify a hash or array as first parameter to fixate"
Branch: 1 branch
BranchTrueFalseTotal
unless my $reftype = builtin::reftype($struct) 1 8 9
243 unless my $reftype= reftype $struct;
244  
245 # just fixate existing structure
246 8 ▶ reuse($struct), return if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 2 6 8
247  
248 # alias semantices from here on
249 alias {
250  
251 # it's a hash
252 6 ▶ if ( $reftype eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 1 5 6
elsif ($reftype eq 'ARRAY') { } 5 0 5
253 1 my %hash= %{$struct};
254 1 ▶ croak "Can only fixate specific values on an empty hash"
Branch: 1 branch
BranchTrueFalseTotal
if keys %hash 0 1 1
255 if keys %hash;
256  
257 # fill the hash and make sure only its values are reused
258 1 (%hash)= @_;
259 1 reuse \%hash; # also makes hash ro
260 }
261  
262 # it's is an array
263 elsif ( $reftype eq 'ARRAY' ) {
264 5 my @array= @{$struct};
265 5 ▶ croak "Can only fixate specific values on an empty array"
Branch: 1 branch
BranchTrueFalseTotal
if @array 0 5 5
266 if @array;
267  
268 # fill the array and make sure its values are reused
269 5 (@array)= reuse @_;
270 5 copy Internals::SvREADONLY @array, 1; # must copy, see above
271 }
272  
273 # huh?
274 else {
275 0 croak "Don't know how to fixate '$reftype' references";
276 }
277 };
278  
279 6 return;
280 } #fixate
281  
282 #---------------------------------------------------------------------------
283 # spread
284 #
285 # Spread a shared constant value in a data structure
286 #
287 # IN: 1 data structure (hash / array ref)
288 # 2 value to be set (default: undef )
289 # 3..N keys / indexes to set
290  
291 3 ▶sub spread (\[@%]@) {
Subroutine spread: called CC=10 SCAR=23.8
Pod documented
292  
293 # find out where to spread
294 3 alias my $struct= shift;
295 3 ▶ croak "Must specify a hash or array as first parameter to spread"
Branch: 1 branch
BranchTrueFalseTotal
unless my $reftype = builtin::reftype($struct) 0 3 3
296 unless my $reftype= reftype $struct;
297  
298 # huh? no value?
299 3 ▶ croak "Must specify a value as second parameter to spread"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 2 3
300 if !@_;
301  
302 # fetch proper constant alias
303 2 alias my $value= reuse shift;
304  
305 # nothing to be done
306 2 ▶ return if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
307  
308 # alias semantics from here on
309 alias {
310  
311 # it's a hash, but can we do it?
312 2 ▶ if ( $reftype eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'HASH') { } 1 1 2
elsif ($reftype eq 'ARRAY') { } 1 0 1
313 1 my %hash= %{$struct};
314 1 ▶ croak "Cannot spread values in a restricted hash"
Branch: 1 branch
BranchTrueFalseTotal
if &Internals::SvREADONLY(\%hash) 0 1 1
315 if Internals::SvREADONLY %hash;
316  
317 # spread the values in the hash
318 1 $hash{$_}= $value foreach @_;
319 }
320  
321 # it's an array, but can we do it?
322 elsif ( $reftype eq 'ARRAY' ) {
323 1 my @array= @{$struct};
324 1 ▶ croak "Cannot spread values in a restricted array"
Branch: 1 branch
BranchTrueFalseTotal
if &Internals::SvREADONLY(\@array) 0 1 1
325 if Internals::SvREADONLY @array;
326  
327 # spread the values in the list
328 1 $array[$_]= $value foreach @_;
329 }
330  
331 # huh?
332 else {
333 0 croak "Don't know how to spread values in '$reftype' references";
334 }
335 };
336  
337 2 return;
338 } #spread
339  
340 #---------------------------------------------------------------------------
341 # forget
342 #
343 # Forget about the values that have been reused so far, or since the last
344 # time "forget" was called.
345  
346 5 ▶sub forget {
Subroutine forget: called CC=1 SCAR=0.0
Pod undocumented
347  
348 # copy a fresh undef value (shouldn't alias the system undef!)
349 5 %reuse= ( $U => undef );
350  
351 # make sure this undef can't be changed
352 5 Internals::SvREADONLY $reuse{$U}, 1;
353  
354 5 return;
355 } #forget
356  
357 #---------------------------------------------------------------------------
358 #
359 # Internal methods
360 #
361 #---------------------------------------------------------------------------
362 # _hash_id
363 #
364 # Return the ID for a hash ref
365 #
366 # IN: 1 hash ref
367 # OUT: 1 id
368  
369 18 ▶sub _hash_id {
Subroutine _hash_id: called CC=2 SCAR=7.0
370 18 alias my %hash= %{ $_[0] };
371  
372 return md5( encode_utf8( $H . join $;, map {
373 18 ▶ $_ => ( defined $hash{$_} ? $hash{$_} : $U )
Branch: 1 branch
BranchTrueFalseTotal
defined $hash{$_} ? : 35 0 35
374 } sort keys %hash ) );
375 } #_hash_id
376  
377 #---------------------------------------------------------------------------
378 # _list_id
379 #
380 # Return the ID for a list ref
381 #
382 # IN: 1 list ref
383 # OUT: 1 id
384  
385 59 ▶sub _list_id {
Subroutine _list_id: called CC=2 SCAR=7.0
386 59 alias my @list= @{ $_[0] };
387  
388 59 ▶ return md5( $A . join $;, map { defined() ? $_ : $U } @list );
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 118 0 118
389 } #_list_id
390  
391 #---------------------------------------------------------------------------
392 #
393 # Debug methods
394 #
395 #---------------------------------------------------------------------------
396 # _constants
397 #
398 # Return hash ref of hash containing the constant values
399 #
400 # OUT: 1 hash ref
401  
402 0 ▶sub _constants { return \%reuse } #_constants
Subroutine _constants: not called CC=1 SCAR=6.9
403  
404 #---------------------------------------------------------------------------
405  
406 __END__
↑ Summary