Object/InsideOut/Util.pm

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