blib/lib/OOB.pm

filter: Statementstmt 100% 154 / 154 Branchbran 100% 70 / 70 Conditioncond 100% 11 / 11 MC/DCmcdc 100% 7 / 7 Subroutinesub 100% 30 / 30 Podpod n/a totaltotal 100% 272 / 272 SCAR 37.8
CC
44
Cov
100%
import
8.0
__ANON__
5.0
OOB_set
5.0
CRAP 44.0
↑ Summary
1 package OOB; # fool various source parsers
2 package OOB::function; # keep OOB namespace as clean as possible
3  
4 # be as strict and verbose as possible
5 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # version
9 13 $OOB::VERSION = '1.00';
10  
11 # modules that we need
12 13 ▶use Scalar::Util qw( blessed refaddr reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Sub::Identify qw( sub_fullname );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # the actual out-of-band data
16 13 my %data;
17  
18 # set DEBUG constant if appropriate
19 BEGIN {
20 13 ▶ my $debug = 0 + ( $ENV{OOB_DEBUG} || 0 );
Subroutine BEGIN: called CC=3 SCAR=11.0
Condition100%2/2
$ENV{'OOB_DEBUG'} || 0
l!l
310
MC/DC100%1/1
$ENV{'OOB_DEBUG'} || 0
$ENV{'OOB_DEBUG'}
Truth table100%2/2
A
A= $ENV{'OOB_DEBUG'}
Aresult
11
00
21 13 eval "sub DEBUG () { $debug }";
22  
23 # we're debugging
24 13 ▶ if ($debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($debug) 3 10 13
25 3 warn "OOB debugging enabled\n";
26  
27 # create OOB::dump
28 13 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 *OOB::dump = sub {
30 7 ▶ require Data::Dumper;
Subroutine __ANON__: called CC=3 SCAR=11.0
31 7 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 6 1 7
32 6 ▶ return wantarray ? %data : \%data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 5 6
33 }
34 1 warn Data::Dumper::Dumper( \%data );
35 }
36 3 }
37  
38 # can't use __PACKAGE__, so we use __OOB__
39 13 eval "sub __OOB__ () { 'OOB' }";
40 } #BEGIN
41  
42 # coderefs of stolen DESTROY methods by class
43 13 my %stolen = ( __OOB__ . '' => \&DESTROY );
44  
45 # install cloaking functions
46 BEGIN {
47 13 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # cloak ourselves from "blessed"
50 *Scalar::Util::blessed = sub ($) {
51 30 ▶ my $blessed = blessed $_[0];
Subroutine __ANON__: called CC=3 SCAR=11.0
52 30 ▶ return if !$blessed;
Branch: 1 branch
BranchTrueFalseTotal
unless $blessed 9 21 30
53 21 ▶ return $blessed->isa(__OOB__) ? undef : $blessed;
Branch: 1 branch
BranchTrueFalseTotal
$blessed->isa('OOB') ? : 7 14 21
54 13 ▶ };
Subroutine BEGIN: called CC=3 SCAR=11.0
55  
56 # determine whether someone else already stole ref()
57 13 my $old_ref = \&CORE::GLOBAL::ref;
58 13 eval { $old_ref->() };
59 13 ▶ $old_ref = undef if $@ =~ m#CORE::GLOBAL::ref#;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /CORE::GLOBAL::ref/ 11 2 13
60 13 warn "CORE::ref function was already stolen\n"
61 if DEBUG and $old_ref;
62  
63 # cloak ourselves from "ref"
64 *CORE::GLOBAL::ref = sub {
65 1352 ▶ my $blessed = blessed $_[0];
Subroutine __ANON__: called CC=4 SCAR=13.9
66 1352 ▶ return reftype $_[0] if $blessed and $blessed->isa(__OOB__);
Branch: 1 branch
BranchTrueFalseTotal
if $blessed and $blessed->isa("OOB") 10 1342 1352
Condition100%3/3
$blessed and $blessed->isa("OOB")
!ll&&!rl&&r
60873410
MC/DC100%2/2
$blessed and $blessed->isa("OOB")
$blessed $blessed->isa("OOB")
Truth table100%3/3
A and B
A= $blessedB= $blessed->isa("OOB")
ABresult
0X0
100
111
67 1342 ▶ return $old_ref ? $old_ref->( $_[0] ) : CORE::ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$old_ref ? : 217 1125 1342
68 13 };
69  
70 # determine whether someone else already stole blessed()
71 13 my $old_bless = \&CORE::GLOBAL::bless;
72 13 eval { $old_bless->() };
73 13 ▶ $old_bless = undef if $@ =~ m#CORE::GLOBAL::bless#;
Branch: 1 branch
BranchTrueFalseTotal
if $@ =~ /CORE::GLOBAL::bless/ 9 4 13
74 13 warn "CORE::bless function was already stolen\n"
75 if DEBUG and $old_bless;
76  
77 # make sure reblessing OOB objects does the right thing
78 *CORE::GLOBAL::bless = sub {
79 114 ▶ my $blessed = blessed $_[0];
Subroutine __ANON__: called CC=5 SCAR=16.1
80 114 ▶ my $class = $_[1] ? $_[1] : caller();
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 101 13 114
81  
82 # make sure we can DESTROY if a new class
83 114 ▶ _register_DESTROY($class) if $blessed and $stolen{$blessed};
Branch: 1 branch
BranchTrueFalseTotal
if $blessed and $stolen{$blessed} 5 109 114
Condition100%3/3
$blessed and $stolen{$blessed}
!ll&&!rl&&r
10815
MC/DC100%2/2
$blessed and $stolen{$blessed}
$blessed $stolen{$blessed}
Truth table100%3/3
A and B
A= $blessedB= $stolen{$blessed}
ABresult
0X0
100
111
84  
85 114 ▶ return $old_bless
Branch: 1 branch
BranchTrueFalseTotal
$old_bless ? : 9 105 114
86 ? $old_bless->( $_[0], $class )
87 : CORE::bless $_[0], $class;
88 13 };
89 } #BEGIN
90  
91 # what we may export
92 13 my %export_ok;
93 13 @export_ok{ qw(
94 OOB_get
95 OOB_reset
96 OOB_set
97 ) } = ();
98  
99 # enable final debugger if necessary
100 END {
101 13 ▶ if (DEBUG) {
Subroutine END: called CC=1 SCAR=0.0
102 require Data::Dumper;
103 warn "Final state of OOB data:\n";
104 warn Data::Dumper::Dumper( \%data );
105 }
106 }
107  
108 # satisfy -require-
109 13 1;
110  
111 #-------------------------------------------------------------------------------
112 #
113 # Functional Interface
114 #
115 #-------------------------------------------------------------------------------
116 # OOB_get
117 #
118 # IN: 1 reference to value
119 # 2 key to fetch
120 # 3 package in which key lives (optional)
121 # OUT: 1 value or undef
122  
123 sub OOB_get {
124  
125 # we're debugging
126 135 ▶ if ( DEBUG > 1 ) {
Subroutine OOB_get: called CC=2 SCAR=6.9
127 my $id = _unique_id( $_[0] );
128 my $key = _generate_key( $_[1], $_[2] );
129 warn "OOB_get with @_: $id -> $key\n";
130 }
131  
132 # return value without autovivifying
133 135 ▶ if ( my $values = $data{ _generate_key( $_[1], $_[2] ) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $data{_generate_key($_[1], $_[2])}) 133 2 135
134 133 return $values->{ _unique_id( $_[0] ) };
135 }
136  
137 2 return;
138 } #OOB_get
139  
140 #-------------------------------------------------------------------------------
141 # OOB_reset
142 #
143 # IN: 1 reference to value
144 # 2 package in which key lives (optional)
145 # OUT: 1 hash ref with all values
146  
147 sub OOB_reset {
148  
149 # we're debugging
150 2 ▶ if ( DEBUG > 1 ) {
Subroutine OOB_reset: called CC=3 SCAR=11.0
151 my $id = _unique_id( $_[0] );
152 warn "OOB_reset with @_: $id\n";
153 }
154  
155 # which values to remove?
156 2 my $id = _unique_id( $_[0] );
157  
158 # need to tell the world what we removed
159 2 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 1 1 2
160 my %removed =
161 2 map { $_ => delete $data{$_}->{$id} }
162 1 grep { exists $data{$_}->{$id} }
163 keys %data;
164 1 return \%removed;
165 }
166  
167 # no need to tell what we deleted
168 1 delete $_->{$id} foreach values %data;
169  
170 1 return;
171 } #OOB_reset
172  
173 #-------------------------------------------------------------------------------
174 # OOB_set
175 #
176 # IN: 1 reference to value
177 # 2 key to set
178 # 3 value to set
179 # 4 package in which key lives (optional)
180 # OUT: 1 any old value
181 # 2 id of value (optional, refaddr derived)
182  
183 sub OOB_set {
184  
185 # scalar specified
186 124 ▶ if ( !reftype $_[0] ) {
Subroutine OOB_set: called CC=5 SCAR=16.1
Branch: 2 branches
BranchTrueFalseTotal
if (not builtin::reftype($_[0])) { } 8 116 124
elsif (my $blessed = builtin::blessed($_[0])) { } 85 31 116
187 8 CORE::bless \$_[0], __OOB__;
188 }
189  
190 # already blessed and not seen before
191 elsif ( my $blessed = blessed $_[0] ) {
192 85 _register_DESTROY($blessed);
193 }
194  
195 # not blessed yet, so bless it now
196 else {
197 31 CORE::bless $_[0], __OOB__;
198 }
199  
200 # we're debugging
201 124 if ( DEBUG > 1 ) {
202 my $id = _unique_id( $_[0] );
203 my $key = _generate_key( $_[1], $_[3] );
204 warn "OOB_set with @_: $id -> $key\n";
205 }
206  
207 # want to know old value
208 124 ▶ if ( defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) 34 90 124
209 34 my $id = _unique_id( $_[0] );
210 34 my $key = _generate_key( $_[1], $_[3] );
211 34 my $old = $data{$key}->{$id};
212 34 $data{$key}->{$id} = $_[2];
213 34 ▶ return wantarray ? ( $old, $id ) : $old;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 32 34
214 }
215  
216 # just set it
217 90 $data{ _generate_key( $_[1], $_[3] ) }->{ _unique_id( $_[0] ) } = $_[2];
218  
219 90 return;
220 } #OOB_set
221  
222 #-------------------------------------------------------------------------------
223 #
224 # Standard Perl features
225 #
226 #-------------------------------------------------------------------------------
227 # import
228 #
229 # Export any constants requested
230 #
231 # IN: 1 class (ignored)
232 # 2..N constants to be exported / attributes to be defined
233  
234 sub OOB::import {
235 13 ▶ my $class = shift;
Subroutine import: called CC=8 SCAR=20.8
236  
237 # nothing to export / defined
238 13 ▶ if (!@_) {
Branch: 3 branches
BranchTrueFalseTotal
if (not @_) { } 6 7 13
elsif (@_ == 1 and $_[0] eq ':all') { } 1 6 7
elsif (my(@attributes) = grep({not exists $export_ok{$_};} @_)) { } 4 2 6
239 6 return;
240 }
241  
242 # we want all constants
243 ▶ elsif ( @_ == 1 and $_[0] eq ':all' ) {
Condition100%3/3
@_ == 1 and $_[0] eq ":all"
!ll&&!rl&&r
241
MC/DC100%2/2
@_ == 1 and $_[0] eq ":all"
@_ == 1 $_[0] eq ":all"
Truth table100%3/3
A and B
A= @_ == 1B= $_[0] eq ":all"
ABresult
0X0
100
111
244 1 @_ = keys %export_ok;
245 }
246  
247 # assume none exportables are attributes
248 9 elsif ( my @attributes = grep { !exists $export_ok{$_} } @_ ) {
249 4 _register_attribute( $class, $_ ) foreach @attributes;
250  
251 # reduce to real exportables
252 4 @_ = grep { exists $export_ok{$_} } @_;
253 }
254  
255 # something to export
256 7 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 4 7
257  
258 # determine namespace to export to
259 3 my $namespace = caller() . '::';
260 3 warn "Exporting @_ to $namespace\n" if DEBUG;
261  
262 # export requested constants
263 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
264 3 *{$namespace.$_} = \&$_ foreach @_;
265 }
266  
267 7 return;
268 } #OOB::import
269  
270 #-------------------------------------------------------------------------------
271 # AUTOLOAD
272 #
273 # Manage auto-creation of missing methods
274 #
275 # IN: 1 class
276 # 2 key
277 # 3 value to set
278  
279 sub OOB::AUTOLOAD {
280  
281 # attempting to call debug when not debugging
282 40 ▶ return if $OOB::AUTOLOAD eq 'OOB::dump';
Subroutine AUTOLOAD: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
if $OOB::AUTOLOAD eq "OOB::dump" 2 38 40
283
284 # don't know what to do with it
285 38 my $class = shift;
286 38 ▶ if ( !$class->isa(__OOB__) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->isa("OOB")) 1 37 38
287 1 require Carp;
288 1 Carp::croak( "Undefined subroutine $OOB::AUTOLOAD" );
289 }
290  
291 # seems to be an attribute we don't know about
292 37 ▶ if ( @_ == 2 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 2) { } 16 21 37
elsif (not @_) { } 6 15 21
293 16 require Carp;
294 16 $OOB::AUTOLOAD =~ m#::(\w+)$#;
295 16 Carp::croak( "Attempt to set unregistered OOB attribute '$1'" );
296 }
297  
298 # registration
299 elsif ( !@_ ) {
300 6 _register_attribute( $OOB::AUTOLOAD =~ m#^(.*)::(\w+)$# );
301 }
302  
303 21 return;
304 } #OOB::AUTOLOAD
305  
306 #-------------------------------------------------------------------------------
307 # DESTROY
308 #
309 # IN: 1 instantiated object
310  
311 sub OOB::DESTROY {
312  
313 # what is the id?
314 52 ▶ my $id = _unique_id( $_[0] );
Subroutine DESTROY: called CC=2 SCAR=6.9
315  
316 # we're debugging
317 52 if (DEBUG) {
318 warn "OOB::DESTROY with @_: $id\n";
319 }
320  
321 # perform the deletion
322 52 delete $_->{$id} foreach values %data;
323  
324 52 return;
325 } #OOB::DESTROY
326
327 #-------------------------------------------------------------------------------
328 #
329 # Internal methods
330 #
331 #-------------------------------------------------------------------------------
332 # _generate_key
333 #
334 # Return the key of the given parameters
335 #
336 # IN: 1 basic key value
337 # 2 any package specification (default: 2 levels up)
338 # OUT: 1 key to be used in internal hash
339  
340 sub _generate_key {
341  
342 # fetch the namespace
343 268 ▶ my $namespace = defined $_[1]
Subroutine _generate_key: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
defined $_[1] ? : 142 117 259
"$_[1]" ? : 141 1 142
344 ? ( "$_[1]" ? "$_[1]--" : '' )
345 : ( caller(1) )[0] . '--';
346  
347 259 return $namespace . $_[0];
348 } #_generate_key
349  
350 #-------------------------------------------------------------------------------
351 # _register_attribute
352 #
353 # Register a new class method
354 #
355 # IN: 1 namespace
356 # 2 key
357  
358 sub _register_attribute {
359 10 ▶ my ( $namespace, $key ) = @_;
Subroutine _register_attribute: called CC=1 SCAR=0.0
360  
361 # install a method to handle it
362 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
363 10 *{ $namespace . '::' . $key } = sub {
364 142 ▶ return if @_ < 2; # another registration and huh?
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 1 141 142
365 141 ▶ return @_ == 3
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 ? : 73 68 141
366 ? OOB_set( $_[1], $key => $_[2], $namespace )
367 : OOB_get( $_[1], $key, $namespace );
368 10 };
369 } #_register_attribute
370  
371 #-------------------------------------------------------------------------------
372 # _register_DESTROY
373 #
374 # IN: 1 class to register DESTROY method for
375  
376 sub _register_DESTROY {
377 90 ▶ my $blessed = shift;
Subroutine _register_DESTROY: called CC=3 SCAR=11.0
378  
379 # already has DESTROY method installed
380 90 ▶ return if $stolen{$blessed};
Branch: 1 branch
BranchTrueFalseTotal
if $stolen{$blessed} 83 7 90
381  
382 # there is a DESTROY method, need to insert ours
383 7 ▶ if ( my $destroy = $blessed->can('DESTROY') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $destroy = $blessed->can('DESTROY')) { } 1 6 7
384 1 $stolen{$blessed} = $destroy;
385 1 my $fullname = sub_fullname($destroy);
386 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
387 13 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
388 1 ▶ *$fullname = sub { $destroy->( $_[0] ); &OOB::DESTROY( $_[0] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
389 }
390  
391 # no DESTROY method yet, to set one
392 else {
393 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
394 6 *{ $blessed . '::DESTROY' } = $stolen{$blessed} = \&OOB::DESTROY;
395 }
396 } #_register_DESTROY
397  
398 #-------------------------------------------------------------------------------
399 # _unique_id
400 #
401 # Return the key of the given parameters
402 #
403 # IN: 1 reference to value to work with
404 # OUT: 1 id to be used in internal hash
405  
406 sub _unique_id {
407 # no ref, make it!
408 311 ▶ my $reftype = reftype $_[0];
Subroutine _unique_id: called CC=4 SCAR=13.9
409 311 ▶ if ( !$reftype ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $reftype) { } 16 295 311
elsif ($reftype eq 'REF') { } 3 292 295
410 16 return refaddr \$_[0];
411 }
412  
413 # special handling for refs to refs
414 elsif ( $reftype eq 'REF' ) {
415 3 my $ref = ${$_[0]};
416 3 ▶ $ref = ${$ref} while reftype $ref eq 'REF';
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($ref) eq "REF" and do { $ref = ${$ref;} } 3 3 6
417 3 return refaddr $ref;
418 }
419  
420 # just use the refaddr
421 292 return refaddr $_[0];
422 } #_unique_id
423  
424  
425 #-------------------------------------------------------------------------------
426  
427 __END__
↑ Summary