Shared.pm

filter: Statementstmt 65.5% 173 / 264 Branchbran 45.0% 90 / 200 Conditioncond 35.6% 36 / 101 MC/DCmcdc 17.7% 11 / 62 Subroutinesub 85.1% 23 / 27 Podpod 100% 2 / 2 totaltotal 51.0% 335 / 656 CC 135 SCAR 44.7
CC
135
Cov
53%
share
228.9
TIEHANDLE
132.0
TIEHASH
77.8
CRAP 87.0
« Shared/Ordhash.pm ↑ Summary Shared/Handle.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## MCE extension for sharing data supporting threads and processes.
4 ##
5 ###############################################################################
6  
7 package MCE::Shared;
8  
9 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 40 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 40 ▶no warnings qw( threads recursion uninitialized once );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 our $VERSION = '1.894';
17  
18 ## no critic (BuiltinFunctions::ProhibitStringyEval)
19 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
20 ## no critic (TestingAndDebugging::ProhibitNoStrict)
21  
22 40 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 40 $Carp::Internal{ (__PACKAGE__) }++;
25  
26 40 ▶no overloading;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 40 ▶use MCE::Mutex ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 40 ▶use MCE::Shared::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 40 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 40 our @CARP_NOT = qw(
33 MCE::Shared::Array MCE::Shared::Hash MCE::Shared::Queue
34 MCE::Shared::Cache MCE::Shared::Minidb MCE::Shared::Scalar
35 MCE::Shared::Condvar MCE::Shared::Object MCE::Shared::Sequence
36 MCE::Shared::Handle MCE::Shared::Ordhash MCE::Shared::Server
37 );
38  
39 31 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
40 40 ▶ no strict 'refs'; no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
41 31 *{ caller().'::mce_open' } = \&open;
42  
43 31 return;
44 }
45  
46 40 my $_share_deeply = 0;
47  
48 ###############################################################################
49 ## ----------------------------------------------------------------------------
50 ## Share function.
51 ##
52 ###############################################################################
53  
54 151 ▶sub share {
Subroutine share: called CC=34 SCAR=54.3
Pod documented
55 151 ▶ shift if (defined $_[0] && $_[0] eq 'MCE::Shared');
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Shared" 12 139 151
Condition66%2/3
defined $_[0] and $_[0] eq "MCE::Shared"
!ll&&!rl&&r
013912
MC/DC50%1/2
defined $_[0] and $_[0] eq "MCE::Shared"
defined $_[0] $_[0] eq "MCE::Shared"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Shared"
ABresult
0X0
100
111
56  
57 # construction via module option
58 151 ▶ if ( ref $_[0] eq 'HASH' && $_[0]->{module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "HASH" and $_[0]{"module"}) 117 34 151
Condition100%3/3
ref $_[0] eq "HASH" and $_[0]{"module"}
!ll&&!rl&&r
331117
MC/DC100%2/2
ref $_[0] eq "HASH" and $_[0]{"module"}
ref $_[0] eq "HASH" $_[0]{"module"}
Truth table100%3/3
A and B
A= ref $_[0] eq "HASH"B= $_[0]{"module"}
ABresult
0X0
100
111
59 117 my $_params = shift;
60 117 my $_class = $_params->{module};
61  
62 117 ▶ return MCE::Shared->condvar(@_) if ( $_class eq 'MCE::Shared::Condvar' );
Branch: 1 branch
BranchTrueFalseTotal
if $_class eq "MCE::Shared::Condvar" 0 117 117
63 117 ▶ return MCE::Shared->handle(@_) if ( $_class eq 'MCE::Shared::Handle' );
Branch: 1 branch
BranchTrueFalseTotal
if $_class eq "MCE::Shared::Handle" 0 117 117
64 117 ▶ return MCE::Shared->queue(@_) if ( $_class eq 'MCE::Shared::Queue' );
Branch: 1 branch
BranchTrueFalseTotal
if $_class eq "MCE::Shared::Queue" 0 117 117
65  
66 117 $_params->{class} = ':construct_module:';
67  
68 my $_obj = MCE::Shared::Server::_new(
69 117 ▶ $_params, [ @_, delete $_params->{new} || 'new' ]
Condition50%1/2
delete $_params->{'new'} || 'new'
l!l
0117
MC/DC0%0/1
delete $_params->{'new'} || 'new'
delete $_params->{'new'}
Truth table50%1/2
A
A= delete $_params->{'new'}
Aresult
11
00
70 );
71  
72 $_obj->[6] = MCE::Mutex->new( impl => 'Channel' ) unless (
73 caller->isa('MCE::Hobo::_hash') || exists( $_params->{_DEEPLY_} )
74 117 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless (caller)->isa("MCE::Hobo::_hash") or exists $_params->{"_DEEPLY_"} 103 14 117
Condition100%3/3
(caller)->isa("MCE::Hobo::_hash") or exists $_params->{"_DEEPLY_"}
l!l&&r!l&&!r
122103
MC/DC100%2/2
(caller)->isa("MCE::Hobo::_hash") or exists $_params->{"_DEEPLY_"}
(caller)->isa("MCE::Hobo::_hash") exists $_params->{"_DEEPLY_"}
Truth table100%3/3
A or B
A= (caller)->isa("MCE::Hobo::_hash")B= exists $_params->{"_DEEPLY_"}
ABresult
1X1
011
000
75  
76 117 return $_obj;
77 }
78  
79 34 ▶ my $_params = ref $_[0] eq 'HASH' && ref $_[1] ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' && ref $_[1] ? : 31 3 34
Condition66%2/3
ref $_[0] eq "HASH" and ref $_[1]
!ll&&!rl&&r
3031
MC/DC50%1/2
ref $_[0] eq "HASH" and ref $_[1]
ref $_[0] eq "HASH" ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0] eq "HASH"B= ref $_[1]
ABresult
0X0
100
111
80 34 my $_class = blessed($_[0]);
81 34 my $_obj;
82  
83 # class construction failed: e.g. share( class->new(...) )
84 34 ▶ return '' if @_ && !$_[0] && $!;
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not $_[0] and $! 0 34 34
Condition33%2/6
@_ and not $_[0] and $!
!ll&&!rl&&r
3400
@_ and not $_[0]
!ll&&!rl&&r
0340
MC/DC0%0/3
@_ and not $_[0] and $!
@_ not $_[0] $!
Truth table25%1/4
A and not(B) and C
A= @_B= not $_[0]C= $!
ABCresult
0XX0
10X0
1100
1111
85  
86 34 ▶ $_share_deeply = 1 if $_params->{_DEEPLY_};
Branch: 1 branch
BranchTrueFalseTotal
if $_params->{"_DEEPLY_"} 2 32 34
87  
88 # blessed object, \@array, \%hash, or \$scalar
89 34 ▶ if ( $_class ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($_class) { } 32 2 34
elsif (ref $_[0] eq 'ARRAY') { } 2 0 2
elsif (ref $_[0] eq 'HASH') { } 0 0 0
elsif (ref $_[0] eq 'SCALAR' and not ref ${$_[0];}) { } 0 0 0
elsif (ref $_[0] eq 'REF') { } 0 0 0
90 32 ▶ _incr_count($_[0]), return $_[0] if $_[0]->can('SHARED_ID');
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->can("SHARED_ID") 0 32 32
91 32 $_params->{'class'} = $_class;
92  
93 32 $_obj = MCE::Shared::Server::_new($_params, $_[0]);
94  
95 $_obj->[6] = MCE::Mutex->new( impl => 'Channel' )
96 32 ▶ unless ( exists $_params->{_DEEPLY_} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_params->{"_DEEPLY_"} 32 0 32
97 }
98 elsif ( ref $_[0] eq 'ARRAY' ) {
99 0 ▶ if ( tied(@{ $_[0] }) && tied(@{ $_[0] })->can('SHARED_ID') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied @{$_[0];} and tied(@{$_[0];})->can("SHARED_ID")) 0 2 2
Condition33%1/3
tied @{$_[0];} and tied(@{$_[0];})->can("SHARED_ID")
!ll&&!rl&&r
200
MC/DC0%0/2
tied @{$_[0];} and tied(@{$_[0];})->can("SHARED_ID")
tied @{$_[0];} tied(@{$_[0];})->can("SHARED_ID")
Truth table33%1/3
A and B
A= tied @{$_[0];}B= tied(@{$_[0];})->can("SHARED_ID")
ABresult
0X0
100
111
100 0 _incr_count(tied(@{ $_[0] })), return tied(@{ $_[0] });
101 }
102 2 $_obj = MCE::Shared->array($_params, @{ $_[0] });
103 2 @{ $_[0] } = (); tie @{ $_[0] }, 'MCE::Shared::Object', $_obj;
104 }
105 elsif ( ref $_[0] eq 'HASH' ) {
106 0 ▶ if ( tied(%{ $_[0] }) && tied(%{ $_[0] })->can('SHARED_ID') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied %{$_[0];} and tied(%{$_[0];})->can("SHARED_ID")) 0 0 0
Condition0%0/3
tied %{$_[0];} and tied(%{$_[0];})->can("SHARED_ID")
!ll&&!rl&&r
000
MC/DC0%0/2
tied %{$_[0];} and tied(%{$_[0];})->can("SHARED_ID")
tied %{$_[0];} tied(%{$_[0];})->can("SHARED_ID")
Truth table0%0/3
A and B
A= tied %{$_[0];}B= tied(%{$_[0];})->can("SHARED_ID")
ABresult
0X0
100
111
107 0 _incr_count(tied(%{ $_[0] })), return tied(%{ $_[0] });
108 }
109 0 $_obj = MCE::Shared->hash($_params, %{ $_[0] });
110 0 %{ $_[0] } = (); tie %{ $_[0] }, 'MCE::Shared::Object', $_obj;
111 }
112 0 ▶ elsif ( ref $_[0] eq 'SCALAR' && !ref ${ $_[0] } ) {
Condition0%0/3
ref $_[0] eq "SCALAR" and not ref ${$_[0];}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] eq "SCALAR" and not ref ${$_[0];}
ref $_[0] eq "SCALAR" not ref ${$_[0];}
Truth table0%0/3
A and not(B)
A= ref $_[0] eq "SCALAR"B= not ref ${$_[0];}
ABresult
0X0
100
111
113 0 ▶ if ( tied(${ $_[0] }) && tied(${ $_[0] })->can('SHARED_ID') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (tied ${$_[0];} and tied(${$_[0];})->can("SHARED_ID")) 0 0 0
Condition0%0/3
tied ${$_[0];} and tied(${$_[0];})->can("SHARED_ID")
!ll&&!rl&&r
000
MC/DC0%0/2
tied ${$_[0];} and tied(${$_[0];})->can("SHARED_ID")
tied ${$_[0];} tied(${$_[0];})->can("SHARED_ID")
Truth table0%0/3
A and B
A= tied ${$_[0];}B= tied(${$_[0];})->can("SHARED_ID")
ABresult
0X0
100
111
114 0 _incr_count(tied(${ $_[0] })), return tied(${ $_[0] });
115 }
116 0 $_obj = MCE::Shared->scalar($_params, ${ $_[0] });
117 0 undef ${ $_[0] }; tie ${ $_[0] }, 'MCE::Shared::Object', $_obj;
118 }
119  
120 # synopsis
121 elsif ( ref $_[0] eq 'REF' ) {
122 0 _croak('A "REF" type is not supported');
123 }
124 else {
125 0 ▶ if ( ref $_[0] eq 'GLOB' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "GLOB") 0 0 0
126 0 _incr_count(tied(*{ $_[0] })), return $_[0] if (
127 0 ▶ tied(*{ $_[0] }) && tied(*{ $_[0] })->can('SHARED_ID')
Branch: 1 branch
BranchTrueFalseTotal
if tied *{$_[0];} and tied(*{$_[0];})->can("SHARED_ID") 0 0 0
Condition0%0/3
tied *{$_[0];} and tied(*{$_[0];})->can("SHARED_ID")
!ll&&!rl&&r
000
MC/DC0%0/2
tied *{$_[0];} and tied(*{$_[0];})->can("SHARED_ID")
tied *{$_[0];} tied(*{$_[0];})->can("SHARED_ID")
Truth table0%0/3
A and B
A= tied *{$_[0];}B= tied(*{$_[0];})->can("SHARED_ID")
ABresult
0X0
100
111
128 );
129 }
130 0 _croak('Synopsis: blessed object, \@array, \%hash, or \$scalar');
131 }
132  
133 34 return $_obj;
134 }
135  
136 ###############################################################################
137 ## ----------------------------------------------------------------------------
138 ## Public functions.
139 ##
140 ###############################################################################
141  
142 40 our $AUTOLOAD; # MCE::Shared::<method_name>
143  
144 217 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=30 SCAR=37.0
145 217 my $_fcn = $AUTOLOAD; substr($_fcn, 0, rindex($_fcn,':') + 1, '');
146  
147 217 ▶ shift if ( defined $_[0] && $_[0] eq 'MCE::Shared' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Shared" 192 25 217
Condition66%2/3
defined $_[0] and $_[0] eq "MCE::Shared"
!ll&&!rl&&r
250192
MC/DC50%1/2
defined $_[0] and $_[0] eq "MCE::Shared"
defined $_[0] $_[0] eq "MCE::Shared"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Shared"
ABresult
0X0
100
111
148  
149 217 ▶ return MCE::Shared::Object::_init(@_) if $_fcn eq 'init';
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn eq "init" 25 192 217
150 192 ▶ return MCE::Shared::Server::_start() if $_fcn eq 'start';
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn eq "start" 57 135 192
151 135 ▶ return MCE::Shared::Server::_stop() if $_fcn eq 'stop';
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn eq "stop" 0 135 135
152 135 ▶ return MCE::Shared::Server::_pid() if $_fcn eq 'pid';
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn eq "pid" 0 135 135
153  
154 135 ▶ if ( $_fcn eq 'array' || $_fcn eq 'hash' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_fcn eq 'array' or $_fcn eq 'hash') { } 16 119 135
elsif ($_fcn eq 'handle') { } 1 118 119
elsif ($_fcn eq 'pdl' or $_fcn =~ /^pdl_(s?byte|u?short|u?long|indx|u?longlong|float|l?double|sequence|zeroe?s|ones|g?random)$/) { } 0 118 118
Condition100%3/3
$_fcn eq "array" or $_fcn eq "hash"
l!l&&r!l&&!r
106119
MC/DC100%2/2
$_fcn eq "array" or $_fcn eq "hash"
$_fcn eq "array" $_fcn eq "hash"
Truth table100%3/3
A or B
A= $_fcn eq "array"B= $_fcn eq "hash"
ABresult
1X1
011
000
155 16 _use( 'MCE::Shared::'.ucfirst($_fcn) );
156 16 ▶ my $_params = ref $_[0] eq 'HASH' ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 14 16
157  
158 16 ▶ $_params->{module} = ( $_fcn eq 'array' )
Branch: 1 branch
BranchTrueFalseTotal
$_fcn eq 'array' ? : 10 6 16
159 ? 'MCE::Shared::Array' : 'MCE::Shared::Hash';
160  
161 16 my $_obj = &share($_params);
162 16 delete $_params->{module};
163  
164 16 ▶ if ( scalar @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 16 0 16
165 16 ▶ if ( $_share_deeply ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_share_deeply) 9 7 16
166 9 $_params->{_DEEPLY_} = 1;
167 9 ▶ if ( $_fcn eq 'array' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_fcn eq 'array') { } 6 3 9
168 6 ▶ for ( my $i = 0; $i <= $#_; $i += 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 16 6 22
169 16 ▶ &_share($_params, $_obj, $_[$i]) if ref($_[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$i] 0 16 16
170 }
171 } else {
172 3 ▶ for ( my $i = 1; $i <= $#_; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 9 3 12
173 9 ▶ &_share($_params, $_obj, $_[$i]) if ref($_[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$i] 0 9 9
174 }
175 }
176 }
177 16 $_obj->assign(@_);
178 }
179  
180 16 $_share_deeply = 0;
181  
182 16 return $_obj;
183 }
184 elsif ( $_fcn eq 'handle' ) {
185 1 ▶ require MCE::Shared::Handle unless $INC{'MCE/Shared/Handle.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"MCE/Shared/Handle.pm"} 0 1 1
186  
187 1 my $_obj = &share( MCE::Shared::Handle->new([]) );
188 40 ▶ my $_fh = \do { no warnings 'once'; local *FH };
Subroutine BEGIN: called CC=1 SCAR=0.0
189  
190 1 tie *{ $_fh }, 'MCE::Shared::Object', $_obj;
191 1 ▶ if ( @_ ) { $_obj->OPEN(@_) or return ''; }
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 1 0 1
$_obj->OPEN(@_) or return "" 1 0 1
192  
193 1 return $_fh;
194 }
195 ▶ elsif ( $_fcn eq 'pdl' || $_fcn =~ /^pdl_(s?byte|u?short|u?long|indx|u?longlong|float|l?double|sequence|zeroe?s|ones|g?random)$/ ) {
Condition33%1/3
$_fcn eq "pdl" or $_fcn =~ /^pdl_(s?byte|u?short|u?long|indx|u?longlong|float|l?double|sequence|zeroe?s|ones|g?random)$/
l!l&&r!l&&!r
00118
MC/DC0%0/2
$_fcn eq "pdl" or $_fcn =~ /^pdl_(s?byte|u?short|u?long|indx|u?longlong|float|l?double|sequence|zeroe?s|ones|g?random)$/
$_fcn eq "pdl" $_fcn =~ /^pdl_(s?byte|u?short|u?long|indx|u?longlong|float|l?double|sequence|zeroe?s|ones|g?random)$/
Truth table33%1/3
A or B
A= $_fcn eq "pdl"B= $_fcn =~ /^pdl_(s?byte|u?short|u?long|indx|u?longlong|float|l?double|sequence|zeroe?s|ones|g?random)$/
ABresult
1X1
011
000
196  
197 0 ▶ $_fcn = $1 if ( $_fcn ne 'pdl' );
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn ne "pdl" 0 0 0
198 0 ▶ push @_, $_fcn; _use('PDL') or _croak($@);
Branch: 1 branch
BranchTrueFalseTotal
_use("PDL") or _croak($@) 0 0 0
199  
200 0 my $_obj = MCE::Shared::Server::_new(
201 { 'class' => ':construct_pdl:' }, [ @_ ]
202 );
203  
204 0 $_obj->[6] = MCE::Mutex->new( impl => 'Channel' );
205  
206 0 return $_obj;
207 }
208  
209 # cache, condvar, minidb, ordhash, queue, scalar, sequence, et cetera
210 118 ▶ $_fcn = 'sequence' if $_fcn eq 'num_sequence';
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn eq "num_sequence" 0 118 118
211 118 my $_pkg = ucfirst( lc $_fcn ); local $@;
212  
213 118 ▶ if ( $INC{"MCE/Shared/$_pkg.pm"} || eval "use MCE::Shared::$_pkg (); 1" ) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"MCE/Shared/$_pkg.pm"} or eval "use MCE::Shared::$_pkg (); 1") 118 0 118
Condition66%2/3
$INC{"MCE/Shared/$_pkg.pm"} or eval "use MCE::Shared::$_pkg (); 1"
l!l&&r!l&&!r
105130
MC/DC0%0/2
$INC{"MCE/Shared/$_pkg.pm"} or eval "use MCE::Shared::$_pkg (); 1"
$INC{"MCE/Shared/$_pkg.pm"} eval "use MCE::Shared::$_pkg (); 1"
Truth table66%2/3
A or B
A= $INC{"MCE/Shared/$_pkg.pm"}B= eval "use MCE::Shared::$_pkg (); 1"
ABresult
1X1
011
000
214 118 $_pkg = "MCE::Shared::$_pkg";
215  
216 118 ▶ return &share({}, $_pkg->new(@_)) if ( $_fcn =~ /^(?:condvar|queue)$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $_fcn =~ /^(?:condvar|queue)$/ 29 89 118
217 89 return &share({ module => $_pkg }, @_);
218 }
219  
220 0 _croak("Can't locate object method \"$_fcn\" via package \"MCE::Shared\"");
221 }
222  
223 8 ▶sub open (@) {
Subroutine open: called CC=14 SCAR=29.0
Pod documented
224 8 ▶ shift if ( defined $_[0] && $_[0] eq 'MCE::Shared' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0] and $_[0] eq "MCE::Shared" 0 8 8
Condition66%2/3
defined $_[0] and $_[0] eq "MCE::Shared"
!ll&&!rl&&r
260
MC/DC0%0/2
defined $_[0] and $_[0] eq "MCE::Shared"
defined $_[0] $_[0] eq "MCE::Shared"
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] eq "MCE::Shared"
ABresult
0X0
100
111
225 8 ▶ require MCE::Shared::Handle unless $INC{'MCE/Shared/Handle.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"MCE/Shared/Handle.pm"} 0 8 8
226  
227 8 my $_obj;
228 8 ▶ if ( ref $_[0] eq 'GLOB' && tied *{ $_[0] } &&
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'GLOB' and tied *{$_[0];} and ref tied *{$_[0];} eq 'MCE::Shared::Object') { } 6 2 8
elsif (@_) { } 2 0 2
Condition66%4/6
ref $_[0] eq "GLOB" and tied *{$_[0];} and ref tied *{$_[0];} eq "MCE::Shared::Object"
!ll&&!rl&&r
206
ref $_[0] eq "GLOB" and tied *{$_[0];}
!ll&&!rl&&r
206
MC/DC33%1/3
ref $_[0] eq "GLOB" and tied *{$_[0];} and ref tied *{$_[0];} eq "MCE::Shared::Object"
ref $_[0] eq "GLOB" tied *{$_[0];} ref tied *{$_[0];} eq "MCE::Shared::Object"
Truth table50%2/4
A and B and C
A= ref $_[0] eq "GLOB"B= tied *{$_[0];}C= ref tied *{$_[0];} eq "MCE::Shared::Object"
ABCresult
0XX0
10X0
1100
1111
229 6 ref tied(*{ $_[0] }) eq 'MCE::Shared::Object' ) {
230  
231 6 $_obj = tied *{ $_[0] };
232 }
233 elsif ( @_ ) {
234 0 ▶ if ( ref $_[0] eq 'GLOB' && tied *{ $_[0] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "GLOB" and tied *{$_[0];}) 0 2 2
Condition33%1/3
ref $_[0] eq "GLOB" and tied *{$_[0];}
!ll&&!rl&&r
200
MC/DC0%0/2
ref $_[0] eq "GLOB" and tied *{$_[0];}
ref $_[0] eq "GLOB" tied *{$_[0];}
Truth table33%1/3
A and B
A= ref $_[0] eq "GLOB"B= tied *{$_[0];}
ABresult
0X0
100
111
235 0 ▶ close $_[0] if defined ( fileno $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $_[0] 0 0 0
236 }
237 2 $_obj = &share( MCE::Shared::Handle->new([]) );
238 40 ▶ $_[0] = \do { no warnings 'once'; local *FH };
Subroutine BEGIN: called CC=1 SCAR=0.0
239 2 tie *{ $_[0] }, 'MCE::Shared::Object', $_obj;
240 }
241  
242 8 ▶ shift; _croak("Not enough arguments for open") unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
243  
244 8 ▶ if ( !defined wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined wantarray) { } 1 7 8
245 1 ▶ $_obj->OPEN(@_) or _croak("open error: $!");
Branch: 1 branch
BranchTrueFalseTotal
$_obj->OPEN(@_) or _croak("open error: $!") 1 0 1
246 } else {
247 7 $_obj->OPEN(@_);
248 }
249 }
250  
251 ###############################################################################
252 ## ----------------------------------------------------------------------------
253 ## TIE support.
254 ##
255 ###############################################################################
256  
257 4 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=3 SCAR=12.5
258 4 shift; $_share_deeply = 1;
259  
260 4 ▶ ( ref($_[0]) eq 'HASH' && exists $_[0]->{'module'} )
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' && exists $_[0]{'module'} ? : 0 4 4
Condition33%1/3
ref $_[0] eq "HASH" and exists $_[0]{"module"}
!ll&&!rl&&r
400
MC/DC0%0/2
ref $_[0] eq "HASH" and exists $_[0]{"module"}
ref $_[0] eq "HASH" exists $_[0]{"module"}
Truth table33%1/3
A and B
A= ref $_[0] eq "HASH"B= exists $_[0]{"module"}
ABresult
0X0
100
111
261 ? _tie('TIEARRAY', @_) : MCE::Shared->array(@_);
262 }
263  
264 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=11 SCAR=48.8
265 0 ▶ shift; require MCE::Shared::Handle unless $INC{'MCE/Shared/Handle.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"MCE/Shared/Handle.pm"} 0 0 0
266  
267 # Tie *FH, 'MCE::Shared', { module => 'MCE::Shared::Handle' }, '>>', \*STDOUT
268 # doesn't work on the Windows platform. We'd let OPEN handle the ref instead.
269  
270 0 ▶ shift if ref($_[0]) eq 'HASH' && $_[0]->{'module'} eq 'MCE::Shared::Handle';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" and $_[0]{"module"} eq "MCE::Shared::Handle" 0 0 0
Condition0%0/3
ref $_[0] eq "HASH" and $_[0]{"module"} eq "MCE::Shared::Handle"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] eq "HASH" and $_[0]{"module"} eq "MCE::Shared::Handle"
ref $_[0] eq "HASH" $_[0]{"module"} eq "MCE::Shared::Handle"
Truth table0%0/3
A and B
A= ref $_[0] eq "HASH"B= $_[0]{"module"} eq "MCE::Shared::Handle"
ABresult
0X0
100
111
271  
272 0 ▶ if ( ref($_[0]) eq 'HASH' && exists $_[0]->{'module'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH' and exists $_[0]{'module'}) { } 0 0 0
Condition0%0/3
ref $_[0] eq "HASH" and exists $_[0]{"module"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] eq "HASH" and exists $_[0]{"module"}
ref $_[0] eq "HASH" exists $_[0]{"module"}
Truth table0%0/3
A and B
A= ref $_[0] eq "HASH"B= exists $_[0]{"module"}
ABresult
0X0
100
111
273 0 ▶ if ( @_ == 3 && ref $_[2] && defined( my $_fd = fileno($_[2]) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3 and ref $_[2] and defined(my $_fd = fileno $_[2])) { } 0 0 0
Condition0%0/6
@_ == 3 and ref $_[2] and defined(my $_fd = fileno $_[2])
!ll&&!rl&&r
000
@_ == 3 and ref $_[2]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ == 3 and ref $_[2] and defined(my $_fd = fileno $_[2])
@_ == 3 ref $_[2] defined(my $_fd = fileno $_[2])
Truth table0%0/4
A and B and C
A= @_ == 3B= ref $_[2]C= defined(my $_fd = fileno $_[2])
ABCresult
0XX0
10X0
1100
1111
274 0 _tie('TIEHANDLE', $_[0], $_[1]."&=$_fd");
275 } else {
276 0 _tie('TIEHANDLE', @_);
277 }
278 }
279 else {
280 0 my $_obj = &share( MCE::Shared::Handle->new([]) );
281 0 ▶ if ( @_ ) { $_obj->OPEN(@_) or return ''; }
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 0 0 0
$_obj->OPEN(@_) or return "" 0 0 0
282  
283 0 $_obj;
284 }
285 }
286  
287 9 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=16 SCAR=43.5
288 9 shift; $_share_deeply = 1;
289  
290 return _tie('TIEHASH', @_) if (
291 9 ▶ ref($_[0]) eq 'HASH' && exists $_[0]->{'module'}
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" and exists $_[0]{"module"} 6 3 9
Condition66%2/3
ref $_[0] eq "HASH" and exists $_[0]{"module"}
!ll&&!rl&&r
306
MC/DC50%1/2
ref $_[0] eq "HASH" and exists $_[0]{"module"}
ref $_[0] eq "HASH" exists $_[0]{"module"}
Truth table66%2/3
A and B
A= ref $_[0] eq "HASH"B= exists $_[0]{"module"}
ABresult
0X0
100
111
292 );
293  
294 3 my ($_cache, $_ordered);
295  
296 3 ▶ if ( ref $_[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 3 3
297 0 ▶ if ( $_[0]->{'ordered'} || $_[0]->{'ordhash'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0]{'ordered'} or $_[0]{'ordhash'}) { } 0 0 0
elsif (exists $_[0]{'max_age'} or exists $_[0]{'max_keys'}) { } 0 0 0
Condition0%0/3
$_[0]{"ordered"} or $_[0]{"ordhash"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0]{"ordered"} or $_[0]{"ordhash"}
$_[0]{"ordered"} $_[0]{"ordhash"}
Truth table0%0/3
A or B
A= $_[0]{"ordered"}B= $_[0]{"ordhash"}
ABresult
1X1
011
000
298 0 $_ordered = 1; shift;
299 ▶ } elsif ( exists $_[0]->{'max_age'} || exists $_[0]->{'max_keys'} ) {
Condition0%0/3
exists $_[0]{"max_age"} or exists $_[0]{"max_keys"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $_[0]{"max_age"} or exists $_[0]{"max_keys"}
exists $_[0]{"max_age"} exists $_[0]{"max_keys"}
Truth table0%0/3
A or B
A= exists $_[0]{"max_age"}B= exists $_[0]{"max_keys"}
ABresult
1X1
011
000
300 0 $_cache = 1;
301 }
302 }
303 else {
304 3 ▶ if ( @_ < 3 && ( $_[0] eq 'ordered' || $_[0] eq 'ordhash' ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ < 3 and $_[0] eq 'ordered' || $_[0] eq 'ordhash') { } 0 3 3
elsif (@_ < 5 and $_[0] eq 'max_age' || $_[0] eq 'max_keys') { } 0 6 6
Condition16%1/6
@_ < 3 and $_[0] eq "ordered" || $_[0] eq "ordhash"
!ll&&!rl&&r
300
$_[0] eq "ordered" or $_[0] eq "ordhash"
l!l&&r!l&&!r
000
MC/DC0%0/3
@_ < 3 and $_[0] eq "ordered" || $_[0] eq "ordhash"
@_ < 3 $_[0] eq "ordered" $_[0] eq "ordhash"
Truth table25%1/4
A and B or C
A= @_ < 3B= $_[0] eq "ordered"C= $_[0] eq "ordhash"
ABCresult
0XX0
1000
11X1
1011
305 0 $_ordered = $_[1]; splice(@_, 0, 2);
306 ▶ } elsif ( @_ < 5 && ( $_[0] eq 'max_age' || $_[0] eq 'max_keys' ) ) {
Condition16%1/6
@_ < 5 and $_[0] eq "max_age" || $_[0] eq "max_keys"
!ll&&!rl&&r
300
$_[0] eq "max_age" or $_[0] eq "max_keys"
l!l&&r!l&&!r
000
MC/DC0%0/3
@_ < 5 and $_[0] eq "max_age" || $_[0] eq "max_keys"
@_ < 5 $_[0] eq "max_age" $_[0] eq "max_keys"
Truth table25%1/4
A and B or C
A= @_ < 5B= $_[0] eq "max_age"C= $_[0] eq "max_keys"
ABCresult
0XX0
1000
11X1
1011
307 0 $_cache = 1;
308 }
309 }
310  
311 3 ▶ return MCE::Shared->cache(@_) if $_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $_cache 0 3 3
312 3 ▶ return MCE::Shared->ordhash(@_) if $_ordered;
Branch: 1 branch
BranchTrueFalseTotal
if $_ordered 0 3 3
313 3 return MCE::Shared->hash(@_);
314 }
315  
316 73 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=3 SCAR=13.1
317 73 shift;
318  
319 73 ▶ ( ref($_[0]) eq 'HASH' && exists $_[0]->{'module'} )
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' && exists $_[0]{'module'} ? : 0 73 73
Condition33%1/3
ref $_[0] eq "HASH" and exists $_[0]{"module"}
!ll&&!rl&&r
7300
MC/DC0%0/2
ref $_[0] eq "HASH" and exists $_[0]{"module"}
ref $_[0] eq "HASH" exists $_[0]{"module"}
Truth table33%1/3
A and B
A= ref $_[0] eq "HASH"B= exists $_[0]{"module"}
ABresult
0X0
100
111
320 ? _tie('TIESCALAR', @_) : MCE::Shared->scalar(@_);
321 }
322  
323 ###############################################################################
324 ## ----------------------------------------------------------------------------
325 ## Private functions.
326 ##
327 ###############################################################################
328  
329 0 ▶sub _croak {
Subroutine _croak: not called CC=3 SCAR=24.8
330 0 ▶ if ( $INC{'MCE.pm'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{'MCE.pm'}) { } 0 0 0
331 0 goto &MCE::_croak;
332 } else {
333 0 ▶ require MCE::Shared::Base unless $INC{'MCE/Shared/Base.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"MCE/Shared/Base.pm"} 0 0 0
334 0 goto &MCE::Shared::Base::_croak;
335 }
336 }
337  
338 0 ▶sub _incr_count {
Subroutine _incr_count: not called CC=1 SCAR=6.9
339 # increments counter for safety during destroy
340 0 MCE::Shared::Server::_incr_count($_[0]->SHARED_ID);
341 }
342  
343 0 ▶sub _share {
Subroutine _share: not called CC=1 SCAR=6.9
344 0 $_[2] = &share($_[0], $_[2]);
345  
346 0 MCE::Shared::Object::_req2(
347 'M~DEE', $_[1]->SHARED_ID()."\n", $_[2]->SHARED_ID()."\n"
348 );
349 }
350  
351 6 ▶sub _tie {
Subroutine _tie: called CC=13 SCAR=41.0
352 6 my ( $_fcn, $_params ) = ( shift, shift );
353  
354 6 ▶ _use( my $_module = $_params->{'module'} ) or _croak("$@\n");
Branch: 1 branch
BranchTrueFalseTotal
_use(my $_module = $_params->{"module"}) or _croak("$@\n") 6 0 6
355  
356 6 ▶ _croak("Can't locate object method \"$_fcn\" via package \"$_module\"")
Branch: 1 branch
BranchTrueFalseTotal
unless eval " $_module->can('${_fcn}') " 0 6 6
357 unless eval qq{ $_module->can('$_fcn') };
358  
359 6 $_params->{class} = ':construct_module:';
360 6 $_params->{tied } = 1;
361  
362 6 my $_obj;
363  
364 6 ▶ if ( $_params->{'module'}->isa('MCE::Shared::Array') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_params->{'module'}->isa('MCE::Shared::Array')) { } 0 6 6
elsif ($_params->{'module'}->isa('MCE::Shared::Hash')) { } 0 6 6
365 0 $_obj = MCE::Shared::Server::_new($_params, [ (), $_fcn ]);
366 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
367 0 $_params->{_DEEPLY_} = 1; delete $_params->{module};
368 0 ▶ for ( my $i = 0; $i <= $#_; $i += 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 0 0 0
369 0 ▶ &_share($_params, $_obj, $_[$i]) if ref($_[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$i] 0 0 0
370 }
371 0 $_obj->assign(@_);
372 }
373 }
374 elsif ( $_params->{'module'}->isa('MCE::Shared::Hash') ) {
375 0 $_obj = MCE::Shared::Server::_new($_params, [ (), $_fcn ]);
376 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
377 0 $_params->{_DEEPLY_} = 1; delete $_params->{module};
378 0 ▶ for ( my $i = 1; $i <= $#_; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#_) 0 0 0
379 0 ▶ &_share($_params, $_obj, $_[$i]) if ref($_[$i]);
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[$i] 0 0 0
380 }
381 0 $_obj->assign(@_);
382 }
383 }
384 else {
385 6 $_obj = MCE::Shared::Server::_new($_params, [ @_, $_fcn ]);
386 }
387  
388 6 ▶ if ( $_obj && $_obj->[2] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_obj and $_obj->[2]) 0 6 6
Condition33%1/3
$_obj and $_obj->[2]
!ll&&!rl&&r
060
MC/DC0%0/2
$_obj and $_obj->[2]
$_obj $_obj->[2]
Truth table33%1/3
A and B
A= $_objB= $_obj->[2]
ABresult
0X0
100
111
389 ##
390 # Set encoder/decoder automatically for supported DB modules.
391 # - AnyDBM_File, DB_File, GDBM_File, NDBM_File, ODBM_File, SDBM_File,
392 # - CDB_File, SQLite_File, Tie::Array::DBD, Tie::Hash::DBD,
393 # - BerkeleyDB::*, KyotoCabinet::DB, TokyoCabinet::*
394 ##
395 0 $_obj->[2] = MCE::Shared::Server::_get_freeze(),
396 $_obj->[3] = MCE::Shared::Server::_get_thaw();
397 }
398  
399 6 $_obj->[6] = MCE::Mutex->new( impl => 'Channel' );
400  
401 6 $_share_deeply = 0;
402  
403 6 return $_obj;
404 }
405  
406 22 ▶sub _use {
Subroutine _use: called CC=17 SCAR=43.2
407 22 my $_class = $_[0];
408  
409 22 ▶ return 1 if $_class eq 'main';
Branch: 1 branch
BranchTrueFalseTotal
if $_class eq "main" 0 22 22
410  
411 22 ▶ if ( $_class =~ /(.*)::_/ ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($_class =~ /(.*)::_/) { } 0 22 22
elsif ($_class =~ /^(BerkeleyDB)::(?:Btree|Hash|Queue|Recno)$/) { } 0 22 22
elsif ($_class =~ /^(TokyoCabinet|KyotoCabinet)::[ABH]?DB$/) { } 0 22 22
elsif ($_class =~ /^Tie::(?:Std|Extra)Hash$/) { } 0 22 22
elsif ($_class eq 'Tie::StdArray') { } 0 22 22
elsif ($_class eq 'Tie::StdScalar') { } 0 22 22
412 # e.g. MCE::Hobo::_hash
413 0 ▶ eval "require $1" unless $INC{ join('/',split(/::/,$1)).'.pm' };
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{join("/", split(/::/, $1, 0)) . '.pm'} 0 0 0
414 }
415 elsif ( $_class =~ /^(BerkeleyDB)::(?:Btree|Hash|Queue|Recno)$/ ) {
416 0 ▶ eval "require $1" unless $INC{"$1.pm"};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"$1.pm"} 0 0 0
417 }
418 elsif ( $_class =~ /^(TokyoCabinet|KyotoCabinet)::[ABH]?DB$/ ) {
419 0 ▶ eval "require $1" unless $INC{"$1.pm"};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"$1.pm"} 0 0 0
420 }
421 elsif ( $_class =~ /^Tie::(?:Std|Extra)Hash$/ ) {
422 0 ▶ eval "require Tie::Hash" unless $INC{'Tie/Hash.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"Tie/Hash.pm"} 0 0 0
423 }
424 elsif ( $_class eq 'Tie::StdArray' ) {
425 0 ▶ eval "require Tie::Array" unless $INC{'Tie/Array.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"Tie/Array.pm"} 0 0 0
426 }
427 elsif ( $_class eq 'Tie::StdScalar' ) {
428 0 ▶ eval "require Tie::Scalar" unless $INC{'Tie/Scalar.pm'};
Branch: 1 branch
BranchTrueFalseTotal
unless $INC{"Tie/Scalar.pm"} 0 0 0
429 }
430  
431 22 ▶ return 1 if eval q{
Branch: 1 branch
BranchTrueFalseTotal
if eval "\n \$_class->can('new') ||\n \$_class->can('TIEARRAY') || \$_class->can('TIEHANDLE') ||\n \$_class->can('TIEHASH') || \$_class->can('TIESCALAR')\n " 21 1 22
432 $_class->can('new') ||
433 $_class->can('TIEARRAY') || $_class->can('TIEHANDLE') ||
434 $_class->can('TIEHASH') || $_class->can('TIESCALAR')
435 };
436  
437 1 ▶ if ( !exists $INC{ join('/',split(/::/,$_class)).'.pm' } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $INC{join("/", split(/::/, $_class, 0)) . '.pm'}) 1 0 1
438 # remove tainted'ness from $_class
439 1 ($_class) = $_class =~ /(.*)/;
440  
441 1 ▶ eval "use $_class (); 1" or return '';
Branch: 1 branch
BranchTrueFalseTotal
eval "use $_class (); 1" or return "" 1 0 1
442 }
443  
444 1 return 1;
445 }
446  
447 40 1;
448  
449 __END__
« Shared/Ordhash.pm ↑ Summary Shared/Handle.pm »