Catmandu/Iterable.pm

filter: Statementstmt 96.0% 288 / 300 Branchbran 82.8% 106 / 128 Conditioncond 83.8% 26 / 31 MC/DCmcdc 61.1% 11 / 18 Subroutinesub 97.2% 71 / 73 Podpod 97.2% 35 / 36 totaltotal 91.6% 537 / 586 CC 80 SCAR 14.8
CC
80
Cov
92%
__ANON__
30.0
__ANON__
12.0
__ANON__
9.7
CRAP 4.4
« Catmandu/Fix/sleep.pm ↑ Summary Catmandu/Exporter/Text.pm »
1 package Catmandu::Iterable;
2  
3 65 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 65 our $VERSION = '1.2025';
6  
7 65 ▶use Catmandu::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
8 is_number
9 is_value
10 is_string
11 is_array_ref
12 is_hash_ref
13 is_regex_ref
14 is_same
15 check_positive
16 65 );
17 65 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 65 ▶use Hash::Util::FieldHash qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 65 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 65 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # delay loading these because of circular dependency
23 65 require Catmandu::Iterator;
24 65 require Catmandu::ArrayIterator;
25 65 require Catmandu;
26  
27 65 requires 'generator';
28  
29 {
30 # can't use Moo attribute because of circular dependency
31 65 fieldhash my %_generators;
32  
33 9 ▶ sub next {
Subroutine next: called CC=2 SCAR=7.1
Pod documented
34 9 my ($self) = @_;
35 9 ▶ ($_generators{$self} ||= $self->generator)->();
Condition66%2/3
$_generators{$self} ||= $self->generator
l!l&&r!l&&!r
720
MC/DC0%0/2
$_generators{$self} ||= $self->generator
$_generators{$self} $self->generator
Truth table66%2/3
A ||= B
A= $_generators{$self}B= $self->generator
ABresult
1X1
011
000
36 }
37  
38 4 ▶ sub rewind {
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
39 4 my ($self) = @_;
40 4 $_generators{$self} = $self->generator;
41 }
42 }
43  
44 65 ▶sub to_array {
Subroutine to_array: called CC=2 SCAR=6.9
Pod documented
45 90 my ($self) = @_;
46 90 my $next = $self->generator;
47 90 my @a;
48 my $data;
49 90 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 917 90 1007
50 917 push @a, $data;
51 }
52 90 \@a;
53 }
54  
55 15 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
56 15 my ($self) = @_;
57 15 my $next = $self->generator;
58 15 my $n = 0;
59 15 ▶ while ($next->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$next()) 1026 15 1041
60 1026 $n++;
61 }
62 15 $n;
63 }
64  
65 1 ▶sub add_to {
Subroutine add_to: called CC=1 SCAR=0.0
Pod documented
66 1 my ($self, $exporter) = @_;
67 1 $exporter->add_many($self);
68 }
69  
70 6 ▶sub slice {
Subroutine slice: called CC=2 SCAR=6.9
Pod documented
71 6 my ($self, $start, $total) = @_;
72 6 ▶ $start //= 0;
Condition100%2/2
$start //= 0
l!l
51
MC/DC100%1/1
$start //= 0
$start
Truth table100%2/2
A
A= $start
Aresult
11
00
73 Catmandu::Iterator->new(
74 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 13 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
76 13 ▶ if (defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $total) 9 4 13
77 9 ▶ $total || return;
Branch: 1 branch
BranchTrueFalseTotal
$total or return 5 4 9
78 }
79 9 state $next = $self->generator;
80 9 state $data;
81 9 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 12 1 13
82 12 ▶ if ($start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > 0) 4 8 12
83 4 $start--;
84 4 next;
85 }
86 8 ▶ if (defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $total) 5 3 8
87 5 $total--;
88 }
89 8 return $data;
90 }
91 1 return;
92 }
93 6 }
94 6 );
95 }
96  
97 5 ▶sub each {
Subroutine each: called CC=2 SCAR=6.9
Pod documented
98 5 my ($self, $sub) = @_;
99 5 my $next = $self->generator;
100 5 my $n = 0;
101 5 my $data;
102 5 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 9 4 13
103 9 $sub->($data);
104 8 $n++;
105 }
106 4 $n;
107 }
108  
109 1 ▶sub each_until {
Subroutine each_until: called CC=3 SCAR=11.0
Pod undocumented
110 1 my ($self, $sub) = @_;
111 1 my $next = $self->generator;
112 1 my $n = 0;
113 1 my $data;
114 1 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 2 0 2
115 2 ▶ $sub->($data) || last;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) or last 1 1 2
116 1 $n++;
117 }
118 1 $n;
119 }
120  
121 10 ▶sub tap {
Subroutine tap: called CC=1 SCAR=0.0
Pod documented
122 10 my ($self, $sub) = @_;
123 Catmandu::Iterator->new(
124 10 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 30 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
126 30 state $next = $self->generator;
127 30 state $data;
128 30 ▶ if (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 20 10 30
129 20 $sub->($data);
130 20 return $data;
131 }
132 10 return;
133 }
134 10 }
135 10 );
136 }
137  
138 3 ▶sub every {
Subroutine every: called CC=1 SCAR=0.0
Pod documented
139 3 my ($self, $n, $sub) = @_;
140 3 check_positive($n);
141 Catmandu::Iterator->new(
142 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 22 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
144 22 state $next = $self->generator;
145 22 state $data;
146 22 state $i = 0;
147 22 ▶ if (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 20 2 22
148 20 ▶ if (++$i == $n) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$i == $n) 8 12 20
149 8 $sub->($data);
150 8 $i = 0;
151 }
152 20 return $data;
153 }
154 2 return;
155 }
156 2 }
157 2 );
158 }
159  
160 8 ▶sub any {
Subroutine any: called CC=3 SCAR=11.0
Pod documented
161 8 my ($self, $sub) = @_;
162 8 my $next = $self->generator;
163 8 my $data;
164 8 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 21 4 25
165 21 ▶ $sub->($data) && return 1;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return 1 4 17 21
166 }
167 4 return 0;
168 }
169  
170 2 ▶sub many {
Subroutine many: called CC=4 SCAR=13.9
Pod documented
171 2 my ($self, $sub) = @_;
172 2 my $next = $self->generator;
173 2 my $n = 0;
174 2 my $data;
175 2 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 5 1 6
176 5 ▶ $sub->($data) && ++$n > 1 && return 1;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and ++$n > 1 and return 1 1 4 5
Condition100%3/3
&$sub($data) and ++$n > 1
!ll&&!rl&&r
221
MC/DC100%2/2
&$sub($data) and ++$n > 1
&$sub($data) ++$n > 1
Truth table100%3/3
A and B
A= &$sub($data)B= ++$n > 1
ABresult
0X0
100
111
177 }
178 1 return 0;
179 }
180  
181 3 ▶sub all {
Subroutine all: called CC=3 SCAR=11.0
Pod documented
182 3 my ($self, $sub) = @_;
183 3 my $next = $self->generator;
184 3 my $data;
185 3 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 5 2 7
186 5 ▶ $sub->($data) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) or return 0 4 1 5
187 }
188 2 return 1;
189 }
190  
191 28 ▶sub map {
Subroutine map: called CC=1 SCAR=0.0
Pod documented
192 28 my ($self, $sub) = @_;
193 Catmandu::Iterator->new(
194 27 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 807 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
196 807 state $next = $self->generator;
197 807 state @buff;
198 807 ▶ @buff = $sub->($next->() // return) unless @buff;
Branch: 1 branch
BranchTrueFalseTotal
unless @buff 804 3 807
Condition100%2/2
&$next() // (return)
l!l
77925
MC/DC100%1/1
&$next() // (return)
&$next()
Truth table100%2/2
A
A= &$next()
Aresult
11
00
199 781 shift @buff;
200 }
201 27 }
202 28 );
203 }
204  
205 1 ▶sub fix {
Subroutine fix: called CC=1 SCAR=0.0
Pod documented
206 1 my $self = shift;
207 1 my $fixer = Catmandu->fixer(@_);
208 1 ▶ $self->map(sub {$fixer->fix(shift)});
Subroutine __ANON__: called CC=1 SCAR=0.0
209 }
210  
211 26 ▶sub reduce {
Subroutine reduce: called CC=3 SCAR=11.0
Pod documented
212 26 my $self = shift;
213 26 my $memo_set = @_ > 1;
214 26 my $sub = pop;
215 26 my $memo = shift;
216 26 my $next = $self->generator;
217 26 my $data;
218 26 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 95 26 121
219 95 ▶ if ($memo_set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($memo_set) { } 94 1 95
220 94 $memo = $sub->($memo, $data);
221 }
222 else {
223 1 $memo = $data;
224 1 $memo_set = 1;
225 }
226 }
227 26 $memo;
228 }
229  
230 12 ▶sub first {
Subroutine first: called CC=1 SCAR=0.0
Pod documented
231 12 $_[0]->generator->();
232 }
233  
234 2 ▶sub rest {
Subroutine rest: called CC=1 SCAR=0.0
Pod documented
235 2 $_[0]->slice(1);
236 }
237  
238 2 ▶sub take {
Subroutine take: called CC=1 SCAR=0.0
Pod documented
239 2 $_[0]->slice(0, $_[1]);
240 }
241  
242 {
243 27 ▶ my $to_sub = sub {
Subroutine __ANON__: called CC=5 SCAR=22.8
244 27 my ($arg1, $arg2) = @_;
245  
246 27 ▶ if (is_string($arg1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($arg1)) 4 23 27
247 4 ▶ if (is_regex_ref($arg2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_regex_ref($arg2)) 4 0 4
248 10 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
249 10 ▶ is_hash_ref($_[0]) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
is_hash_ref($_[0]) or return 0 10 0 10
250 10 my $val = $_[0]->{$arg1};
251 10 ▶ is_value($val) && $val =~ $arg2;
Branch: 1 branch
BranchTrueFalseTotal
is_value($val) and $val =~ /$arg2/ 10 0 10
252 4 };
253 }
254 0 ▶ if (is_array_ref($arg2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_ref($arg2)) 0 0 0
255 0 ▶ return sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
256 0 ▶ is_hash_ref($_[0]) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
is_hash_ref($_[0]) or return 0 0 0 0
257 0 ▶ is_value(my $val = $_[0]->{$arg1}) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
is_value(my $val = $_[0]{$arg1}) or return 0 0 0 0
258 0 for my $v (@$arg2) {
259 0 ▶ return 1 if $val eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $v 0 0 0
260 }
261 0 0;
262 0 };
263 }
264 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
265 0 ▶ is_hash_ref($_[0]) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
is_hash_ref($_[0]) or return 0 0 0 0
266 0 my $val = $_[0]->{$arg1};
267 0 ▶ is_value($val) && $val eq $arg2;
Branch: 1 branch
BranchTrueFalseTotal
is_value($val) and $val eq $arg2 0 0 0
268 0 };
269 }
270  
271 23 ▶ if (is_regex_ref($arg1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_regex_ref($arg1)) 4 19 23
272 10 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
273 10 my $val = $_[0];
274 10 ▶ is_value($val) && $val =~ $arg1;
Branch: 1 branch
BranchTrueFalseTotal
is_value($val) and $val =~ /$arg1/ 10 0 10
275 4 };
276 }
277  
278 19 $arg1;
279 65 };
280  
281 4 ▶ sub detect {
Subroutine detect: called CC=3 SCAR=11.0
Pod documented
282 4 my $self = shift;
283 4 my $sub = $to_sub->(@_);
284 4 my $next = $self->generator;
285 4 my $data;
286 4 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 8 1 9
287 8 ▶ $sub->($data) && return $data;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return $data 3 5 8
288 }
289 1 return;
290 }
291  
292 10 ▶ sub select {
Subroutine select: called CC=1 SCAR=0.0
Pod documented
293 10 my $self = shift;
294 10 my $sub = $to_sub->(@_);
295 Catmandu::Iterator->new(
296 10 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
297 27 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
298 27 state $next = $self->generator;
299 27 state $data;
300 27 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 31 10 41
301 31 ▶ $sub->($data) && return $data;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return $data 17 14 31
302 }
303 10 return;
304 }
305 10 }
306 10 );
307 }
308  
309 4 ▶ sub grep {goto &select}
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
310  
311 13 ▶ sub reject {
Subroutine reject: called CC=1 SCAR=0.0
Pod documented
312 13 my $self = shift;
313 13 my $sub = $to_sub->(@_);
314 Catmandu::Iterator->new(
315 12 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
316 36 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
317 36 state $next = $self->generator;
318 36 state $data;
319 36 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 35 11 46
320 35 ▶ $sub->($data) || return $data;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) or return $data 11 24 35
321 }
322 11 return;
323 }
324 12 }
325 13 );
326 }
327 65 };
328  
329 3 ▶sub sorted {
Subroutine sorted: called CC=3 SCAR=11.0
Pod documented
330 3 my ($self, $cmp) = @_;
331 3 ▶ if (!defined $cmp) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $cmp) { } 1 2 3
elsif (ref $cmp) { } 1 1 2
332 1 Catmandu::ArrayIterator->new([sort @{$self->to_array}]);
333 }
334 elsif (ref $cmp) {
335 Catmandu::ArrayIterator->new(
336 1 [sort {$cmp->($a, $b)} @{$self->to_array}]);
337 }
338 else {
339 # TODO: use Schwartzian transform for more complex key
340 Catmandu::ArrayIterator->new(
341 1 [sort {$a->{$cmp} cmp $b->{$cmp}} @{$self->to_array}]);
342 }
343 }
344  
345 2 ▶sub pluck {
Subroutine pluck: called CC=1 SCAR=0.0
Pod documented
346 2 my ($self, $key) = @_;
347 $self->map(
348 6 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
349 6 $_[0]->{$key};
350 }
351 2 );
352 }
353  
354 5 ▶sub invoke {
Subroutine invoke: called CC=1 SCAR=0.0
Pod documented
355 5 my ($self, $method, @args) = @_;
356 $self->map(
357 16 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
358 16 $_[0]->$method(@args);
359 }
360 5 );
361 }
362  
363 4 ▶sub contains {goto &includes}
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
364  
365 6 ▶sub includes {
Subroutine includes: called CC=1 SCAR=0.0
Pod documented
366 6 my ($self, $data) = @_;
367 $self->any(
368 17 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
369 17 is_same($data, $_[0]);
370 }
371 6 );
372 }
373  
374 3 ▶sub group {
Subroutine group: called CC=1 SCAR=0.0
Pod documented
375 3 my ($self, $size) = @_;
376 Catmandu::Iterator->new(
377 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
378 17 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
379 17 state $next = $self->generator;
380  
381 17 my $group = [];
382  
383 17 ▶ for (my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 24 13 37
384 24 ▶ push @$group, $next->() // last;
Condition100%2/2
&$next() // (last)
l!l
204
MC/DC100%1/1
&$next() // (last)
&$next()
Truth table100%2/2
A
A= &$next()
Aresult
11
00
385 }
386  
387 17 ▶ unless (@$group) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$group) 3 14 17
388 3 return;
389 }
390  
391 14 Catmandu::ArrayIterator->new($group);
392 }
393 3 }
394 3 );
395 }
396  
397 4 ▶sub interleave {
Subroutine interleave: called CC=1 SCAR=0.0
Pod documented
398 4 my @iterators = @_;
399 Catmandu::Iterator->new(
400 4 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
401 26 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
402 26 state @generators;
403 26 state $n = @iterators;
404 26 state $i = 0;
405 26 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 30 4 34
406 30 ▶ $i = 0 if $i == $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $n 12 18 30
407 30 ▶ my $next = $generators[$i] ||= $iterators[$i]->generator;
Condition66%2/3
$generators[$i] ||= $iterators[$i]->generator
l!l&&r!l&&!r
2280
MC/DC0%0/2
$generators[$i] ||= $iterators[$i]->generator
$generators[$i] $iterators[$i]->generator
Truth table66%2/3
A ||= B
A= $generators[$i]B= $iterators[$i]->generator
ABresult
1X1
011
000
408 30 ▶ if (defined(my $data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = &$next())) { } 22 8 30
409 22 $i++;
410 22 return $data;
411 }
412 else {
413 8 splice @generators, $i, 1;
414 8 $n--;
415 }
416 }
417 4 return;
418 }
419 4 }
420 4 );
421 }
422  
423 5 ▶sub max {
Subroutine max: called CC=1 SCAR=0.0
Pod documented
424 5 my ($self, $sub) = @_;
425 $self->reduce(
426 undef,
427 12 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
428 12 my ($memo, $data) = @_;
429 12 ▶ my $val = defined $sub ? $sub->($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 3 9 12
430 12 ▶ return $val > $memo ? $val : $memo
Branch: 2 branches
BranchTrueFalseTotal
if is_number($memo) and is_number($val) 5 7 12
$val > $memo ? : 0 5 5
Condition100%3/3
is_number($memo) and is_number($val)
!ll&&!rl&&r
525
MC/DC100%2/2
is_number($memo) and is_number($val)
is_number($memo) is_number($val)
Truth table100%3/3
A and B
A= is_number($memo)B= is_number($val)
ABresult
0X0
100
111
431 if is_number($memo) && is_number($val);
432 7 ▶ return $memo if is_number($memo);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($memo) 2 5 7
433 5 ▶ return $val if is_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($val) 2 3 5
434 3 return;
435 }
436 5 );
437 }
438  
439 5 ▶sub min {
Subroutine min: called CC=1 SCAR=0.0
Pod documented
440 5 my ($self, $sub) = @_;
441 $_[0]->reduce(
442 undef,
443 12 ▶ sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
444 12 my ($memo, $data) = @_;
445 12 ▶ my $val = defined $sub ? $sub->($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 3 9 12
446 12 ▶ return $val < $memo ? $val : $memo
Branch: 2 branches
BranchTrueFalseTotal
if is_number($memo) and is_number($val) 5 7 12
$val < $memo ? : 5 0 5
Condition100%3/3
is_number($memo) and is_number($val)
!ll&&!rl&&r
525
MC/DC100%2/2
is_number($memo) and is_number($val)
is_number($memo) is_number($val)
Truth table100%3/3
A and B
A= is_number($memo)B= is_number($val)
ABresult
0X0
100
111
447 if is_number($memo) && is_number($val);
448 7 ▶ return $memo if is_number($memo);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($memo) 2 5 7
449 5 ▶ return $val if is_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($val) 2 3 5
450 3 return;
451 }
452 5 );
453 }
454  
455 6 ▶sub benchmark {
Subroutine benchmark: called CC=1 SCAR=0.2
Pod documented
456 6 my ($self) = @_;
457 $self->tap(
458 14 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
459 14 state $n = 0;
460 14 state $t = [gettimeofday];
461 14 ▶ if (++$n % 100 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$n % 100 == 0) 0 14 14
462 0 printf STDERR "added %9d (%d/sec)\n", $n,
463 $n / tv_interval($t);
464 }
465 }
466 6 );
467 }
468  
469 6 ▶sub format {
Subroutine format: called CC=6 SCAR=18.0
Pod documented
470 6 my ($self, %opts) = @_;
471 6 ▶ $opts{header} //= 1;
Condition50%1/2
$opts{"header"} //= 1
l!l
06
MC/DC0%0/1
$opts{"header"} //= 1
$opts{"header"}
Truth table50%1/2
A
A= $opts{"header"}
Aresult
11
00
472 6 ▶ $opts{col_sep} //= " | ";
Condition50%1/2
$opts{"col_sep"} //= " | "
l!l
06
MC/DC0%0/1
$opts{"col_sep"} //= " | "
$opts{"col_sep"}
Truth table50%1/2
A
A= $opts{"col_sep"}
Aresult
11
00
473 6 ▶ my @cols = $opts{cols} ? @{$opts{cols}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$opts{'cols'} ? : 6 0 6
474 6 my @col_lengths = map length, @cols;
475  
476 my $rows = $self->map(
477 717 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
478 717 my $data = $_[0];
479 717 my $row = [];
480 717 ▶ for (my $i = 0; $i < @cols; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @cols) 2151 717 2868
481 2151 ▶ my $col = $data->{$cols[$i]} // "";
Condition100%2/2
$$data{$cols[$i]} // ""
l!l
210942
MC/DC100%1/1
$$data{$cols[$i]} // ""
$$data{$cols[$i]}
Truth table100%2/2
A
A= $$data{$cols[$i]}
Aresult
11
00
482 2151 my $len = length $col;
483 2151 ▶ $col_lengths[$i] = $len if $len > $col_lengths[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $len > $col_lengths[$i] 42 2109 2151
484 2151 push @$row, $col;
485 }
486 717 $row;
487 }
488 6 )->to_array;
489  
490 6 my @indices = 0 .. @cols - 1;
491 my $pattern
492 6 = join($opts{col_sep}, map {"%-$col_lengths[$_]s"} @indices) . "\n";
493  
494 6 ▶ if ($opts{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"header"}) 6 0 6
495 6 printf $pattern, @cols;
496 6 my $sep = $opts{col_sep};
497 6 $sep =~ s/[^|]/-/g;
498 6 print join($sep, map {'-' x $col_lengths[$_]} @indices) . "\n";
499 }
500 6 for my $row (@$rows) {
501 717 printf $pattern, @$row;
502 }
503 }
504  
505 2 ▶sub stop_if {
Subroutine stop_if: called CC=1 SCAR=0.0
Pod documented
506 2 my ($self, $sub) = @_;
507 Catmandu::Iterator->new(
508 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
509 5 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
510 5 state $next = $self->generator;
511 5 ▶ my $data = $next->() // return;
Condition50%1/2
&$next() // (return)
l!l
50
MC/DC0%0/1
&$next() // (return)
&$next()
Truth table50%1/2
A
A= &$next()
Aresult
11
00
512 5 ▶ $sub->($data) && return;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return 2 3 5
513 3 $data;
514 }
515 2 }
516 2 );
517 }
518  
519 5 ▶sub run {
Subroutine run: called CC=3 SCAR=11.0
Pod documented
520 5 my ($self) = @_;
521 5 my $next = $self->generator;
522 5 my $run = 0;
523 5 ▶ while (defined($next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &$next()) 24 5 29
524 24 ▶ $run ||= 1;
Condition100%2/2
$run ||= 1
l!l
204
MC/DC100%1/1
$run ||= 1
$run
Truth table100%2/2
A
A= $run
Aresult
11
00
525 }
526 5 $run;
527 }
528  
529 65 1;
530  
531 __END__
« Catmandu/Fix/sleep.pm ↑ Summary Catmandu/Exporter/Text.pm »