MCE/Candy.pm

filter: Statementstmt 35.3% 47 / 133 Branchbran 21.6% 13 / 60 Conditioncond 40.0% 6 / 15 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 57.1% 8 / 14 Podpod 100% 6 / 6 totaltotal 35.2% 84 / 238 CC 36 SCAR 29.4
CC
36
Cov
32%
foreach
42.0
forchunk
30.0
forseq
30.0
CRAP 19.0
« MCE/Mutex/Channel2.pm ↑ Summary MCE/Channel/Simple.pm »
1 ###############################################################################
2 ## ----------------------------------------------------------------------------
3 ## Sugar methods and output iterators.
4 ##
5 ###############################################################################
6  
7 package MCE::Candy;
8  
9 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶no warnings qw( threads recursion uninitialized );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 our $VERSION = '1.904';
15  
16 10 our @CARP_NOT = qw( MCE );
17  
18 ###############################################################################
19 ## ----------------------------------------------------------------------------
20 ## Import routine.
21 ##
22 ###############################################################################
23  
24 10 my $_imported;
25  
26 10 ▶sub import {
Subroutine import: called CC=3 SCAR=14.2
27  
28 10 ▶ return if ($_imported++);
Branch: 1 branch
BranchTrueFalseTotal
if $_imported++ 0 10 10
29  
30 10 ▶ unless ($INC{'MCE.pm'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INC{"MCE.pm"}) 0 10 10
31 0 $\ = undef; require Carp;
32 0 Carp::croak(
33 "MCE::Candy requires MCE. Please see the MCE::Candy documentation\n".
34 "for more information.\n\n"
35 );
36 }
37  
38 10 return;
39 }
40  
41 ###############################################################################
42 ## ----------------------------------------------------------------------------
43 ## Forchunk, foreach, and forseq sugar methods.
44 ##
45 ###############################################################################
46  
47 0 ▶sub forchunk {
Subroutine forchunk: not called CC=5 SCAR=34.0
Pod documented
48  
49 0 ▶ my $x = shift; my $self = ref($x) ? $x : $MCE::MCE;
Branch: 1 branch
BranchTrueFalseTotal
ref $x ? : 0 0 0
50 0 my $_input_data = $_[0];
51  
52 0 MCE::_validate_runstate($self, 'MCE::forchunk');
53  
54 0 my ($_user_func, $_params_ref);
55  
56 0 ▶ if (ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH') { } 0 0 0
57 0 $_user_func = $_[2]; $_params_ref = $_[1];
58 } else {
59 0 $_user_func = $_[1]; $_params_ref = {};
60 }
61  
62 0 @_ = ();
63  
64 0 ▶ MCE::_croak('MCE::forchunk: (input_data) is not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_input_data 0 0 0
65 unless (defined $_input_data);
66 0 ▶ MCE::_croak('MCE::forchunk: (code_block) is not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_user_func 0 0 0
67 unless (defined $_user_func);
68  
69 0 $_params_ref->{input_data} = $_input_data;
70 0 $_params_ref->{user_func} = $_user_func;
71  
72 0 $self->run(1, $_params_ref);
73  
74 0 return $self;
75 }
76  
77 0 ▶sub foreach {
Subroutine foreach: not called CC=6 SCAR=37.4
Pod documented
78  
79 0 ▶ my $x = shift; my $self = ref($x) ? $x : $MCE::MCE;
Branch: 1 branch
BranchTrueFalseTotal
ref $x ? : 0 0 0
80 0 my $_input_data = $_[0];
81  
82 0 MCE::_validate_runstate($self, 'MCE::foreach');
83  
84 0 my ($_user_func, $_params_ref);
85  
86 0 ▶ if (ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH') { } 0 0 0
87 0 $_user_func = $_[2]; $_params_ref = $_[1];
88 } else {
89 0 $_user_func = $_[1]; $_params_ref = {};
90 }
91  
92 0 @_ = ();
93  
94 0 ▶ MCE::_croak('MCE::foreach: (HASH) not allowed as input by this method')
Branch: 1 branch
BranchTrueFalseTotal
if ref $_input_data eq "HASH" 0 0 0
95 if (ref $_input_data eq 'HASH');
96 0 ▶ MCE::_croak('MCE::foreach: (input_data) is not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_input_data 0 0 0
97 unless (defined $_input_data);
98 0 ▶ MCE::_croak('MCE::foreach: (code_block) is not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_user_func 0 0 0
99 unless (defined $_user_func);
100  
101 0 $_params_ref->{chunk_size} = 1;
102 0 $_params_ref->{input_data} = $_input_data;
103 0 $_params_ref->{user_func} = $_user_func;
104  
105 0 $self->run(1, $_params_ref);
106  
107 0 return $self;
108 }
109  
110 0 ▶sub forseq {
Subroutine forseq: not called CC=5 SCAR=34.0
Pod documented
111  
112 0 ▶ my $x = shift; my $self = ref($x) ? $x : $MCE::MCE;
Branch: 1 branch
BranchTrueFalseTotal
ref $x ? : 0 0 0
113 0 my $_sequence = $_[0];
114  
115 0 MCE::_validate_runstate($self, 'MCE::forseq');
116  
117 0 my ($_user_func, $_params_ref);
118  
119 0 ▶ if (ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq 'HASH') { } 0 0 0
120 0 $_user_func = $_[2]; $_params_ref = $_[1];
121 } else {
122 0 $_user_func = $_[1]; $_params_ref = {};
123 }
124  
125 0 @_ = ();
126  
127 0 ▶ MCE::_croak('MCE::forseq: (sequence) is not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_sequence 0 0 0
128 unless (defined $_sequence);
129 0 ▶ MCE::_croak('MCE::forseq: (code_block) is not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_user_func 0 0 0
130 unless (defined $_user_func);
131  
132 0 $_params_ref->{sequence} = $_sequence;
133 0 $_params_ref->{user_func} = $_user_func;
134  
135 0 $self->run(1, $_params_ref);
136  
137 0 return $self;
138 }
139  
140 ###############################################################################
141 ## ----------------------------------------------------------------------------
142 ## Output iterators for preserving output order.
143 ##
144 ###############################################################################
145  
146 9 ▶sub out_iter_array {
Subroutine out_iter_array: called CC=4 SCAR=14.1
Pod documented
147  
148 9 my $_aref = shift; my %_tmp; my $_order_id = 1;
149  
150 9 ▶ if (ref $_aref eq 'MCE::Shared::Object') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_aref eq 'MCE::Shared::Object') { } 0 9 9
151 0 my $_pkg = $_aref->blessed;
152 0 ▶ MCE::_croak('The argument to (out_iter_array) is not valid.')
Branch: 1 branch
BranchTrueFalseTotal
unless $_pkg->can("TIEARRAY") 0 0 0
153 unless $_pkg->can('TIEARRAY');
154 }
155 else {
156 9 ▶ MCE::_croak('The argument to (out_iter_array) is not an array ref.')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_aref eq "ARRAY" 0 9 9
157 unless (ref $_aref eq 'ARRAY');
158 }
159  
160 20 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
161 20 my $_chunk_id = shift;
162  
163 20 ▶ if ($_chunk_id == $_order_id && keys %_tmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_chunk_id == $_order_id and keys %_tmp == 0) { } 10 10 20
Condition100%3/3
$_chunk_id == $_order_id and keys %_tmp == 0
!ll&&!rl&&r
5510
MC/DC100%2/2
$_chunk_id == $_order_id and keys %_tmp == 0
$_chunk_id == $_order_id keys %_tmp == 0
Truth table100%3/3
A and B
A= $_chunk_id == $_order_idB= keys %_tmp == 0
ABresult
0X0
100
111
164 ## already orderly
165 10 $_order_id++, push @{ $_aref }, @_;
166 }
167 else {
168 ## hold temporarily otherwise until orderly
169 10 @{ $_tmp{ $_chunk_id } } = @_;
170  
171 10 while (1) {
172 20 ▶ last unless exists $_tmp{ $_order_id };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_tmp{$_order_id} 10 10 20
173 10 push @{ $_aref }, @{ delete $_tmp{ $_order_id++ } };
174 }
175 }
176 9 };
177 }
178  
179 5 ▶sub out_iter_callback {
Subroutine out_iter_callback: called CC=2 SCAR=6.9
Pod documented
180  
181 5 my $_cref = shift; my %_tmp; my $_order_id = 1;
182  
183 5 ▶ MCE::_croak('The argument to (out_iter_callback) is not a CODE ref.')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_cref eq "CODE" 0 5 5
184 unless (ref $_cref eq 'CODE');
185  
186 4 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
187 4 my $_chunk_id = shift;
188  
189 4 ▶ if ($_chunk_id == $_order_id && keys %_tmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_chunk_id == $_order_id and keys %_tmp == 0) { } 1 3 4
Condition100%3/3
$_chunk_id == $_order_id and keys %_tmp == 0
!ll&&!rl&&r
211
MC/DC100%2/2
$_chunk_id == $_order_id and keys %_tmp == 0
$_chunk_id == $_order_id keys %_tmp == 0
Truth table100%3/3
A and B
A= $_chunk_id == $_order_idB= keys %_tmp == 0
ABresult
0X0
100
111
190 ## already orderly
191 1 $_order_id++, $_cref->(@_);
192 }
193 else {
194 ## hold temporarily otherwise until orderly
195 3 @{ $_tmp{ $_chunk_id } } = @_;
196  
197 3 while (1) {
198 6 ▶ last unless exists $_tmp{ $_order_id };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_tmp{$_order_id} 3 3 6
199 3 $_cref->(@{ delete $_tmp{ $_order_id++ } });
200 }
201 }
202 5 };
203 }
204  
205 0 ▶sub out_iter_fh {
Subroutine out_iter_fh: not called CC=5 SCAR=34.0
Pod documented
206  
207 0 my $_fh = $_[0]; my %_tmp; my $_order_id = 1;
208 0 ▶ $_fh = \$_[0] if (!ref $_fh && ref \$_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if not ref $_fh and ref \$_[0] 0 0 0
Condition0%0/3
not ref $_fh and ref \$_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_fh and ref \$_[0]
not ref $_fh ref \$_[0]
Truth table0%0/3
not(A) and B
A= not ref $_fhB= ref \$_[0]
ABresult
0X0
100
111
209  
210 0 ▶ MCE::_croak('The argument to (out_iter_fh) is not a supported file handle.')
Branch: 1 branch
BranchTrueFalseTotal
unless ref($_fh) =~ /^(?:GLOB|FileHandle|IO::)/ 0 0 0
211 unless (ref($_fh) =~ /^(?:GLOB|FileHandle|IO::)/);
212  
213 0 ▶ if ($_fh->can('print')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_fh->can('print')) { } 0 0 0
214 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
215 0 my $_chunk_id = shift;
216  
217 0 ▶ if ($_chunk_id == $_order_id && keys %_tmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_chunk_id == $_order_id and keys %_tmp == 0) { } 0 0 0
Condition0%0/3
$_chunk_id == $_order_id and keys %_tmp == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_chunk_id == $_order_id and keys %_tmp == 0
$_chunk_id == $_order_id keys %_tmp == 0
Truth table0%0/3
A and B
A= $_chunk_id == $_order_idB= keys %_tmp == 0
ABresult
0X0
100
111
218 ## already orderly
219 0 $_order_id++, $_fh->print(@_);
220 }
221 else {
222 ## hold temporarily otherwise until orderly
223 0 @{ $_tmp{ $_chunk_id } } = @_;
224  
225 0 while (1) {
226 0 ▶ last unless exists $_tmp{ $_order_id };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_tmp{$_order_id} 0 0 0
227 0 $_fh->print(@{ delete $_tmp{ $_order_id++ } });
228 }
229 }
230 0 };
231 }
232 else {
233 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
234 0 my $_chunk_id = shift;
235  
236 0 ▶ if ($_chunk_id == $_order_id && keys %_tmp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_chunk_id == $_order_id and keys %_tmp == 0) { } 0 0 0
Condition0%0/3
$_chunk_id == $_order_id and keys %_tmp == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$_chunk_id == $_order_id and keys %_tmp == 0
$_chunk_id == $_order_id keys %_tmp == 0
Truth table0%0/3
A and B
A= $_chunk_id == $_order_idB= keys %_tmp == 0
ABresult
0X0
100
111
237 ## already orderly
238 0 $_order_id++, print {$_fh} @_;
239 }
240 else {
241 ## hold temporarily otherwise until orderly
242 0 @{ $_tmp{ $_chunk_id } } = @_;
243  
244 0 while (1) {
245 0 ▶ last unless exists $_tmp{ $_order_id };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_tmp{$_order_id} 0 0 0
246 0 print {$_fh} @{ delete $_tmp{ $_order_id++ } };
247 }
248 }
249 0 };
250 }
251 }
252  
253 10 1;
254  
255 __END__
« MCE/Mutex/Channel2.pm ↑ Summary MCE/Channel/Simple.pm »