blib/lib/Object/InsideOut/Util.pm

filter: Statementstmt 41.4% 73 / 176 Branchbran 23.5% 24 / 102 Conditioncond 23.8% 5 / 21 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 7 totaltotal 34.7% 115 / 331 SCAR 71.2
CC
64
Cov
34%
make_shared
380.0
clone_shared
306.0
clone
45.1
CRAP 1235.5
« blib/lib/Object/InsideOut.pm ↑ Summary blib/lib/Object/InsideOut/Foreign.pm »
1 package Object::InsideOut::Util; {
2  
3 45 require 5.006;
4  
5 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 45 our $VERSION = '4.04';
9 45 $VERSION = eval $VERSION;
10  
11 45 ▶use Object::InsideOut::Metadata 4.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 ### Module Initialization ###
14  
15 BEGIN {
16 # 1. Install our own 'no-op' version of Internals::SvREADONLY for Perl < 5.8
17 45 ▶ if (! Internals->can('SvREADONLY')) {
Subroutine BEGIN: called CC=3 SCAR=15.1
Branch: 1 branch
BranchTrueFalseTotal
unless ("Internals"->can("SvREADONLY")) 0 45 45
18 0 ▶ *Internals::SvREADONLY = sub (\$;$) { return; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 }
20  
21 # Import 'share' and 'bless' if threads::shared
22 45 ▶ if ($threads::shared::threads_shared) {
Branch: 1 branch
BranchTrueFalseTotal
if ($threads::shared::threads_shared) 0 45 45
23 0 import threads::shared;
24 }
25 }
26  
27  
28 # 2. Export requested subroutines
29 sub import
30 {
31 46 ▶ my $class = shift; # Not used
Subroutine import: called CC=4 SCAR=14.0
32  
33 # Exportable subroutines
34 46 my %EXPORT_OK;
35 46 @EXPORT_OK{qw(create_object hash_re is_it make_shared shared_copy)} = undef;
36  
37 # Handle entries in the import list
38 46 my $caller = caller();
39 46 my %meta;
40 46 ▶ while (my $sym = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sym = shift()) 181 46 227
41 181 ▶ if (exists($EXPORT_OK{lc($sym)})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $EXPORT_OK{lc $sym}) { } 181 0 181
42 # Export subroutine name
43 45 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
44 181 *{$caller.'::'.$sym} = \&{lc($sym)};
45 181 $meta{$sym}{'hidden'} = 1;
46 } else {
47 0 OIO::Code->die(
48 'message' => "Symbol '$sym' is not exported by Object::InsideOut::Util",
49 'Info' => 'Exportable symbols: ' . join(' ', keys(%EXPORT_OK)),
50 'ignore_package' => 'Object::InsideOut::Util');
51 }
52 }
53 46 ▶ if (%meta) {
Branch: 1 branch
BranchTrueFalseTotal
if (%meta) 46 0 46
54 46 add_meta($caller, \%meta);
55 }
56 }
57  
58  
59 ### Subroutines ###
60  
61 # Returns a blessed (optional), readonly (Perl 5.8) anonymous scalar reference
62 # containing either:
63 # the value returned by a user-specified subroutine; or
64 # a user-supplied scalar
65 sub create_object
66 {
67 189 ▶ my ($class, $id) = @_;
Subroutine create_object: called CC=5 SCAR=17.2
Pod undocumented
68  
69 # Create the object from an anonymous scalar reference
70 189 my $obj = \do{ my $scalar; };
71  
72 # Set the scalar equal to ...
73 189 ▶ if (my $ref_type = ref($id)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref_type = ref $id) { } 189 0 189
74 189 ▶ if ($ref_type eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type eq 'CODE') { } 189 0 189
75 # ... the value returned by the user-specified subroutine
76 189 local $SIG{__DIE__} = 'OIO::trap';
77 189 $$obj = $id->($class);
78 } else {
79 # Complain if something other than code ref
80 0 OIO::Args->die(
81 'message' => q/2nd argument to create_object() is not a code ref or scalar/,
82 'Usage' => 'create_object($class, $scalar) or create_object($class, $code_ref, ...)',
83 'ignore_package' => 'Object::InsideOut::Util');
84 }
85  
86 } else {
87 # ... the user-supplied scalar
88 0 $$obj = $id;
89 }
90  
91 # Bless the object into the specified class (optional)
92 189 ▶ if ($class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class) 189 0 189
93 189 bless($obj, $class);
94 }
95  
96 # Make the object 'readonly' (Perl 5.8)
97 189 ▶ Internals::SvREADONLY($$obj, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 189 0 189
98  
99 # Done - return the object
100 189 return ($obj);
101 }
102  
103  
104 # Make a thread-shared version of a complex data structure or object
105 sub make_shared
106 {
107 0 ▶ my $in = shift;
Subroutine make_shared: not called CC=19 SCAR=59.4
Pod undocumented
108 0 ▶ my $cloned = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
109  
110 # If not sharing or already thread-shared, then just return the input
111 0 ▶ if (! ref($in) ||
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $in && $threads::threads && $threads::shared::threads_shared or threads::shared::is_shared($in)) 0 0 0
Condition0%0/9
not ref $in && $threads::threads && $threads::shared::threads_shared or threads::shared::is_shared($in)
l!l&&r!l&&!r
000
ref $in && $threads::threads && $threads::shared::threads_shared
!ll&&!rl&&r
000
ref $in and $threads::threads
!ll&&!rl&&r
000
MC/DC0%0/4
not ref $in && $threads::threads && $threads::shared::threads_shared or threads::shared::is_shared($in)
ref $in $threads::threads $threads::shared::threads_shared threads::shared::is_shared($in)
Truth table0%0/5
not(A and B && C) or D
A= ref $inB= $threads::threadsC= $threads::shared::threads_sharedD= threads::shared::is_shared($in)
ABCDresult
0XXX1
10XX1
110X1
11111
11100
112 ! $threads::threads ||
113 ! $threads::shared::threads_shared ||
114 threads::shared::is_shared($in))
115 {
116 0 return ($in);
117 }
118  
119 # Check for previously cloned references
120 # (this takes care of circular refs as well)
121 0 my $addr = Scalar::Util::refaddr($in);
122 0 ▶ if (exists($cloned->{$addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cloned->{$addr}) 0 0 0
123 # Return the already existing clone
124 0 return $cloned->{$addr};
125 }
126  
127 # Make copies of array, hash and scalar refs
128 0 my $out;
129 0 my $ref_type = Scalar::Util::reftype($in);
130  
131 # Copy an array ref
132 0 ▶ if ($ref_type eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref_type eq 'ARRAY') { } 0 0 0
elsif ($ref_type eq 'HASH') { } 0 0 0
elsif ($ref_type eq 'SCALAR') { } 0 0 0
elsif ($ref_type eq 'REF') { } 0 0 0
133 # Make empty shared array ref
134 0 $out = &threads::shared::share([]);
135 # Add to clone checking hash
136 0 $cloned->{$addr} = $out;
137 # Recursively copy and add contents
138 0 push(@$out, map { make_shared($_, $cloned) } @$in);
139 }
140  
141 # Copy a hash ref
142 elsif ($ref_type eq 'HASH') {
143 # Make empty shared hash ref
144 0 $out = &threads::shared::share({});
145 # Add to clone checking hash
146 0 $cloned->{$addr} = $out;
147 # Recursively copy and add contents
148 0 foreach my $key (keys(%{$in})) {
149 0 $out->{$key} = make_shared($in->{$key}, $cloned);
150 }
151 }
152  
153 # Copy a scalar ref
154 elsif ($ref_type eq 'SCALAR') {
155 0 $out = \do{ my $scalar = $$in; };
156 0 threads::shared::share($out);
157 # Add to clone checking hash
158 0 $cloned->{$addr} = $out;
159 }
160  
161 # Copy of a ref of a ref
162 elsif ($ref_type eq 'REF') {
163 # Special handling for $x = \$x
164 0 ▶ if ($addr == Scalar::Util::refaddr($$in)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr == builtin::refaddr($$in)) { } 0 0 0
165 0 $out = \$out;
166 0 threads::shared::share($out);
167 0 $cloned->{$addr} = $out;
168 } else {
169 0 my $tmp;
170 0 $out = \$tmp;
171 0 threads::shared::share($out);
172 # Add to clone checking hash
173 0 $cloned->{$addr} = $out;
174 # Recursively copy and add contents
175 0 $tmp = make_shared($$in, $cloned);
176 }
177  
178 } else {
179 # Just return anything else
180 # NOTE: This will end up generating an error
181 0 return ($in);
182 }
183  
184 # Return blessed copy, if applicable
185 0 ▶ if (my $class = Scalar::Util::blessed($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($in)) 0 0 0
186 0 bless($out, $class);
187 }
188  
189 # Clone READONLY flag
190 0 ▶ if ($ref_type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type eq "SCALAR") 0 0 0
191 0 ▶ if (Internals::SvREADONLY($$in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Internals::SvREADONLY(\$$in)) 0 0 0
192 0 ▶ Internals::SvREADONLY($$out, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 0 0 0
193 }
194 }
195 0 ▶ if (Internals::SvREADONLY($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Internals::SvREADONLY(\$in)) 0 0 0
196 0 ▶ Internals::SvREADONLY($out, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 0 0 0
197 }
198  
199 # Return clone
200 0 return ($out);
201 }
202  
203  
204 # Make a copy of a complex data structure or object.
205 # If thread-sharing, then make the copy thread-shared.
206 sub shared_copy
207 {
208 0 ▶ return (($threads::shared::threads_shared) ? clone_shared(@_) : clone(@_));
Subroutine shared_copy: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$threads::shared::threads_shared ? : 0 0 0
209 }
210  
211  
212 # Recursively make a copy of a complex data structure or object that is
213 # thread-shared
214 sub clone_shared
215 {
216 0 ▶ my $in = shift;
Subroutine clone_shared: not called CC=17 SCAR=57.2
Pod undocumented
217 0 ▶ my $cloned = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
218  
219 # Just return the item if not a ref or if it's an object
220 0 ▶ return $in if (! ref($in) || Scalar::Util::blessed($in));
Branch: 1 branch
BranchTrueFalseTotal
if not ref $in or builtin::blessed($in) 0 0 0
Condition0%0/3
not ref $in or builtin::blessed($in)
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $in or builtin::blessed($in)
not ref $in builtin::blessed($in)
Truth table0%0/3
not(A) or B
A= not ref $inB= builtin::blessed($in)
ABresult
1X1
011
000
221  
222 # Check for previously cloned references
223 # (this takes care of circular refs as well)
224 0 my $addr = Scalar::Util::refaddr($in);
225 0 ▶ if (exists($cloned->{$addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cloned->{$addr}) 0 0 0
226 # Return the already existing clone
227 0 return $cloned->{$addr};
228 }
229  
230 # Make copies of array, hash and scalar refs
231 0 my $out;
232 0 my $ref_type = Scalar::Util::reftype($in);
233  
234 # Copy an array ref
235 0 ▶ if ($ref_type eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref_type eq 'ARRAY') { } 0 0 0
elsif ($ref_type eq 'HASH') { } 0 0 0
elsif ($ref_type eq 'SCALAR') { } 0 0 0
elsif ($ref_type eq 'REF') { } 0 0 0
236 # Make empty shared array ref
237 0 $out = &threads::shared::share([]);
238 # Add to clone checking hash
239 0 $cloned->{$addr} = $out;
240 # Recursively copy and add contents
241 0 push(@$out, map { clone_shared($_, $cloned) } @$in);
242 }
243  
244 # Copy a hash ref
245 elsif ($ref_type eq 'HASH') {
246 # Make empty shared hash ref
247 0 $out = &threads::shared::share({});
248 # Add to clone checking hash
249 0 $cloned->{$addr} = $out;
250 # Recursively copy and add contents
251 0 foreach my $key (keys(%{$in})) {
252 0 $out->{$key} = clone_shared($in->{$key}, $cloned);
253 }
254 }
255  
256 # Copy a scalar ref
257 elsif ($ref_type eq 'SCALAR') {
258 0 $out = \do{ my $scalar = $$in; };
259 0 threads::shared::share($out);
260 # Add to clone checking hash
261 0 $cloned->{$addr} = $out;
262 }
263  
264 # Copy of a ref of a ref
265 elsif ($ref_type eq 'REF') {
266 # Special handling for $x = \$x
267 0 ▶ if ($addr == Scalar::Util::refaddr($$in)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr == builtin::refaddr($$in)) { } 0 0 0
268 0 $out = \$out;
269 0 threads::shared::share($out);
270 0 $cloned->{$addr} = $out;
271 } else {
272 0 my $tmp;
273 0 $out = \$tmp;
274 0 threads::shared::share($out);
275 # Add to clone checking hash
276 0 $cloned->{$addr} = $out;
277 # Recursively copy and add contents
278 0 $tmp = clone_shared($$in, $cloned);
279 }
280  
281 } else {
282 # Just return anything else
283 # NOTE: This will end up generating an error
284 0 return ($in);
285 }
286  
287 # Return blessed copy, if applicable
288 0 ▶ if (my $class = Scalar::Util::blessed($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = builtin::blessed($in)) 0 0 0
289 0 bless($out, $class);
290 }
291  
292 # Clone READONLY flag
293 0 ▶ if ($ref_type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type eq "SCALAR") 0 0 0
294 0 ▶ if (Internals::SvREADONLY($$in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Internals::SvREADONLY(\$$in)) 0 0 0
295 0 ▶ Internals::SvREADONLY($$out, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 0 0 0
296 }
297 }
298 0 ▶ if (Internals::SvREADONLY($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Internals::SvREADONLY(\$in)) 0 0 0
299 0 ▶ Internals::SvREADONLY($out, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 0 0 0
300 }
301  
302 # Return clone
303 0 return ($out);
304 }
305  
306  
307 # Recursively make a copy of a complex data structure or object
308 sub clone
309 {
310 46 ▶ my $in = shift;
Subroutine clone: called CC=16 SCAR=38.1
Pod undocumented
311 46 ▶ my $cloned = shift || {};
Condition100%2/2
shift() || {}
l!l
1036
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
312  
313 # Just return the item if not a ref or if it's an object
314 46 ▶ return $in if (! ref($in) || Scalar::Util::blessed($in));
Branch: 1 branch
BranchTrueFalseTotal
if not ref $in or builtin::blessed($in) 37 9 46
Condition100%3/3
not ref $in or builtin::blessed($in)
l!l&&r!l&&!r
3529
MC/DC100%2/2
not ref $in or builtin::blessed($in)
not ref $in builtin::blessed($in)
Truth table100%3/3
not(A) or B
A= not ref $inB= builtin::blessed($in)
ABresult
1X1
011
000
315  
316 # Check for previously cloned references
317 # (this takes care of circular refs as well)
318 9 my $addr = Scalar::Util::refaddr($in);
319 9 ▶ if (exists($cloned->{$addr})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $cloned->{$addr}) 0 9 9
320 # Return the already existing clone
321 0 return $cloned->{$addr};
322 }
323  
324 # Make copies of array, hash and scalar refs
325 9 my $out;
326 9 my $ref_type = Scalar::Util::reftype($in);
327  
328 # Copy an array ref
329 9 ▶ if ($ref_type eq 'ARRAY') {
Branch: 4 branches
BranchTrueFalseTotal
if ($ref_type eq 'ARRAY') { } 3 6 9
elsif ($ref_type eq 'HASH') { } 6 0 6
elsif ($ref_type eq 'SCALAR') { } 0 0 0
elsif ($ref_type eq 'REF') { } 0 0 0
330 # Make empty shared array ref
331 3 $out = [];
332 # Add to clone checking hash
333 3 $cloned->{$addr} = $out;
334 # Recursively copy and add contents
335 3 push(@$out, map { clone($_, $cloned) } @$in);
336 }
337  
338 # Copy a hash ref
339 elsif ($ref_type eq 'HASH') {
340 # Make empty shared hash ref
341 6 $out = {};
342 # Add to clone checking hash
343 6 $cloned->{$addr} = $out;
344 # Recursively copy and add contents
345 6 foreach my $key (keys(%{$in})) {
346 4 $out->{$key} = clone($in->{$key}, $cloned);
347 }
348 }
349  
350 # Copy a scalar ref
351 elsif ($ref_type eq 'SCALAR') {
352 0 $out = \do{ my $scalar = $$in; };
353 # Add to clone checking hash
354 0 $cloned->{$addr} = $out;
355 }
356  
357 # Copy of a ref of a ref
358 elsif ($ref_type eq 'REF') {
359 # Special handling for $x = \$x
360 0 ▶ if ($addr == Scalar::Util::refaddr($$in)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($addr == builtin::refaddr($$in)) { } 0 0 0
361 0 $out = \$out;
362 0 $cloned->{$addr} = $out;
363 } else {
364 0 my $tmp;
365 0 $out = \$tmp;
366 # Add to clone checking hash
367 0 $cloned->{$addr} = $out;
368 # Recursively copy and add contents
369 0 $tmp = clone($$in, $cloned);
370 }
371  
372 } else {
373 # Just return anything else
374 # NOTE: This will end up generating an error
375 0 return ($in);
376 }
377  
378 # Clone READONLY flag
379 9 ▶ if ($ref_type eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_type eq "SCALAR") 0 9 9
380 0 ▶ if (Internals::SvREADONLY($$in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Internals::SvREADONLY(\$$in)) 0 0 0
381 0 ▶ Internals::SvREADONLY($$out, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 0 0 0
382 }
383 }
384 9 ▶ if (Internals::SvREADONLY($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&Internals::SvREADONLY(\$in)) 0 9 9
385 0 ▶ Internals::SvREADONLY($out, 1) if ($] >= 5.008003);
Branch: 1 branch
BranchTrueFalseTotal
if $] >= "5.008003" 0 0 0
386 }
387  
388 # Return clone
389 9 return ($out);
390 }
391  
392  
393 # Access hash value using regex
394 sub hash_re
395 {
396 55 ▶ my $hash = $_[0]; # Hash ref to search through
Subroutine hash_re: called CC=4 SCAR=13.9
Pod undocumented
397 55 my $re = $_[1]; # Regex to match keys against
398  
399 55 foreach (keys(%{$hash})) {
400 56 ▶ if (/$re/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$re/) 36 20 56
401 36 ▶ return ($hash->{$_}, $_) if wantarray();
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 11 25 36
402 25 return ($hash->{$_});
403 }
404 }
405 19 return;
406 }
407  
408  
409 # Checks if a scalar is a specified type
410 sub is_it
411 {
412 58 ▶ my ($thing, $what) = @_;
Subroutine is_it: called CC=2 SCAR=6.9
Pod undocumented
413  
414 58 ▶ return ((Scalar::Util::blessed($thing))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($thing) ? : 33 25 58
415 ? $thing->isa($what)
416 : (ref($thing) eq $what));
417 }
418  
419 } # End of package's lexical scope
420  
421 45 1;
« blib/lib/Object/InsideOut.pm ↑ Summary blib/lib/Object/InsideOut/Foreign.pm »