blib/lib/Catmandu/Iterable.pm

filter: Statementstmt 95.5% 260 / 272 Branchbran 81.6% 98 / 120 Conditioncond 79.3% 23 / 29 MC/DCmcdc 52.9% 9 / 17 Subroutinesub 96.9% 64 / 66 Podpod 96.8% 31 / 32 totaltotal 90.4% 485 / 536 CC 75 SCAR 15.2
CC
75
Cov
90%
__ANON__
30.0
__ANON__
12.0
__ANON__
9.7
CRAP 4.6
« blib/lib/Catmandu/Exporter/Text.pm ↑ Summary blib/lib/Catmandu/Fix/sleep.pm »
1 package Catmandu::Iterable;
2  
3 47 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 47 our $VERSION = '1.0201_02';
6  
7 47 ▶use Catmandu::Util qw(:is :check);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Hash::Util::FieldHash qw(fieldhash);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 47 ▶use Role::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 47 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # delay loading these because of circular dependency
14 47 require Catmandu::Iterator;
15 47 require Catmandu::ArrayIterator;
16  
17 47 requires 'generator';
18  
19 {
20 # can't use Moo attribute because of circular dependency
21 47 fieldhash my %_generators;
22  
23 sub next {
24 9 ▶ my ($self) = @_;
Subroutine next: called CC=2 SCAR=7.1
Pod documented
25 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
26 }
27  
28 sub rewind {
29 4 ▶ my ($self) = @_;
Subroutine rewind: called CC=1 SCAR=0.0
Pod documented
30 4 $_generators{$self} = $self->generator;
31 }
32 }
33  
34 47 sub to_array {
35 76 ▶ my ($self) = @_;
Subroutine to_array: called CC=2 SCAR=6.9
Pod documented
36 76 my $next = $self->generator;
37 76 my @a;
38 my $data;
39 76 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 720 76 796
40 720 push @a, $data;
41 }
42 76 \@a;
43 }
44  
45 sub count {
46 15 ▶ my ($self) = @_;
Subroutine count: called CC=2 SCAR=6.9
Pod documented
47 15 my $next = $self->generator;
48 15 my $n = 0;
49 15 ▶ while ($next->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$next()) 1026 15 1041
50 1026 $n++;
51 }
52 15 $n;
53 }
54  
55 sub slice {
56 4 ▶ my ($self, $start, $total) = @_;
Subroutine slice: called CC=2 SCAR=6.9
Pod documented
57 4 ▶ $start //= 0;
Condition50%1/2
$start //= 0
l!l
40
MC/DC0%0/1
$start //= 0
$start
Truth table50%1/2
A
A= $start
Aresult
11
00
58 Catmandu::Iterator->new(
59 sub {
60 sub {
61 9 ▶ if (defined $total) {
Subroutine __ANON__: called CC=6 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (defined $total) 5 4 9
62 5 ▶ $total || return;
Branch: 1 branch
BranchTrueFalseTotal
$total or return 2 3 5
63 }
64 7 state $next = $self->generator;
65 7 state $data;
66 7 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 8 1 9
67 8 ▶ if ($start > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > 0) 2 6 8
68 2 $start--;
69 2 next;
70 }
71 6 ▶ if (defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $total) 3 3 6
72 3 $total--;
73 }
74 6 return $data;
75 }
76 1 return;
77 }
78 4 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
79 4 );
80 }
81  
82 sub each {
83 4 ▶ my ($self, $sub) = @_;
Subroutine each: called CC=2 SCAR=6.9
Pod documented
84 4 my $next = $self->generator;
85 4 my $n = 0;
86 4 my $data;
87 4 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 6 4 10
88 6 $sub->($data);
89 6 $n++;
90 }
91 4 $n;
92 }
93  
94 sub each_until {
95 1 ▶ my ($self, $sub) = @_;
Subroutine each_until: called CC=3 SCAR=11.0
Pod undocumented
96 1 my $next = $self->generator;
97 1 my $n = 0;
98 1 my $data;
99 1 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 2 0 2
100 2 ▶ $sub->($data) || last;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) or last 1 1 2
101 1 $n++;
102 }
103 1 $n;
104 }
105  
106 sub tap {
107 2 ▶ my ($self, $sub) = @_;
Subroutine tap: called CC=1 SCAR=0.0
Pod documented
108 Catmandu::Iterator->new(
109 sub {
110 sub {
111 9 ▶ state $next = $self->generator;
Subroutine __ANON__: called CC=2 SCAR=6.9
112 9 state $data;
113 9 ▶ if (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 7 2 9
114 7 $sub->($data);
115 7 return $data;
116 }
117 2 return;
118 }
119 2 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
120 2 );
121 }
122  
123 sub any {
124 8 ▶ my ($self, $sub) = @_;
Subroutine any: called CC=3 SCAR=11.0
Pod documented
125 8 my $next = $self->generator;
126 8 my $data;
127 8 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 21 4 25
128 21 ▶ $sub->($data) && return 1;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return 1 4 17 21
129 }
130 4 return 0;
131 }
132  
133 sub many {
134 2 ▶ my ($self, $sub) = @_;
Subroutine many: called CC=4 SCAR=13.9
Pod documented
135 2 my $next = $self->generator;
136 2 my $n = 0;
137 2 my $data;
138 2 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 5 1 6
139 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
140 }
141 1 return 0;
142 }
143  
144 sub all {
145 2 ▶ my ($self, $sub) = @_;
Subroutine all: called CC=3 SCAR=11.0
Pod documented
146 2 my $next = $self->generator;
147 2 my $data;
148 2 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 4 1 5
149 4 ▶ $sub->($data) || return 0;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) or return 0 1 3 4
150 }
151 1 return 1;
152 }
153  
154 sub map {
155 22 ▶ my ($self, $sub) = @_;
Subroutine map: called CC=1 SCAR=0.0
Pod documented
156 Catmandu::Iterator->new(
157 sub {
158 sub {
159 611 ▶ state $next = $self->generator;
Subroutine __ANON__: called CC=2 SCAR=6.9
160 611 ▶ $sub->($next->() // return);
Condition100%2/2
&$next() // (return)
l!l
59120
MC/DC100%1/1
&$next() // (return)
&$next()
Truth table100%2/2
A
A= &$next()
Aresult
11
00
161 }
162 21 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
163 22 );
164 }
165  
166 sub reduce {
167 21 ▶ my $self = shift;
Subroutine reduce: called CC=3 SCAR=11.0
Pod documented
168 21 my $memo_set = @_ > 1;
169 21 my $sub = pop;
170 21 my $memo = shift;
171 21 my $next = $self->generator;
172 21 my $data;
173 21 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 70 21 91
174 70 ▶ if ($memo_set) {
Branch: 1 branch
BranchTrueFalseTotal
if ($memo_set) { } 69 1 70
175 69 $memo = $sub->($memo, $data);
176 }
177 else {
178 1 $memo = $data;
179 1 $memo_set = 1;
180 }
181 }
182 21 $memo;
183 }
184  
185 sub first {
186 10 ▶ $_[0]->generator->();
Subroutine first: called CC=1 SCAR=0.0
Pod documented
187 }
188  
189 sub rest {
190 2 ▶ $_[0]->slice(1);
Subroutine rest: called CC=1 SCAR=0.0
Pod documented
191 }
192  
193 sub take {
194 2 ▶ $_[0]->slice(0, $_[1]);
Subroutine take: called CC=1 SCAR=0.0
Pod documented
195 }
196  
197 {
198 my $to_sub = sub {
199 24 ▶ my ($arg1, $arg2) = @_;
Subroutine __ANON__: called CC=5 SCAR=22.8
200  
201 24 ▶ if (is_string($arg1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_string($arg1)) 4 20 24
202 4 ▶ if (is_regex_ref($arg2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_regexpref($arg2)) 4 0 4
203 return sub {
204 10 ▶ is_hash_ref($_[0]) || return 0;
Subroutine __ANON__: called CC=3 SCAR=11.7
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_plain_hashref($_[0]) or return 0 0 10 10
205 10 my $val = $_[0]->{$arg1};
206 10 ▶ is_value($val) && $val =~ $arg2;
Branch: 1 branch
BranchTrueFalseTotal
is_value($val) and $val =~ /$arg2/ 10 0 10
207 4 };
208 }
209 0 ▶ if (is_array_ref($arg2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_plain_arrayref($arg2)) 0 0 0
210 return sub {
211 0 ▶ is_hash_ref($_[0]) || return 0;
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_plain_hashref($_[0]) or return 0 0 0 0
212 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
213 0 for my $v (@$arg2) {
214 0 ▶ return 1 if $val eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $v 0 0 0
215 }
216 0 0;
217 0 };
218 }
219 return sub {
220 0 ▶ is_hash_ref($_[0]) || return 0;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
Ref::Util::XS::is_plain_hashref($_[0]) or return 0 0 0 0
221 0 my $val = $_[0]->{$arg1};
222 0 ▶ is_value($val) && $val eq $arg2;
Branch: 1 branch
BranchTrueFalseTotal
is_value($val) and $val eq $arg2 0 0 0
223 0 };
224 }
225  
226 20 ▶ if (is_regex_ref($arg1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Ref::Util::XS::is_regexpref($arg1)) 4 16 20
227 return sub {
228 10 ▶ my $val = $_[0];
Subroutine __ANON__: called CC=2 SCAR=7.2
229 10 ▶ is_value($val) && $val =~ $arg1;
Branch: 1 branch
BranchTrueFalseTotal
is_value($val) and $val =~ /$arg1/ 10 0 10
230 4 };
231 }
232  
233 16 $arg1;
234 47 };
235  
236 sub detect {
237 4 ▶ my $self = shift;
Subroutine detect: called CC=3 SCAR=11.0
Pod documented
238 4 my $sub = $to_sub->(@_);
239 4 my $next = $self->generator;
240 4 my $data;
241 4 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 8 1 9
242 8 ▶ $sub->($data) && return $data;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return $data 3 5 8
243 }
244 1 return;
245 }
246  
247 sub select {
248 9 ▶ my $self = shift;
Subroutine select: called CC=1 SCAR=0.0
Pod documented
249 9 my $sub = $to_sub->(@_);
250 Catmandu::Iterator->new(
251 sub {
252 sub {
253 23 ▶ state $next = $self->generator;
Subroutine __ANON__: called CC=3 SCAR=11.0
254 23 state $data;
255 23 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 27 9 36
256 27 ▶ $sub->($data) && return $data;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return $data 14 13 27
257 }
258 9 return;
259 }
260 9 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
261 9 );
262 }
263  
264 4 ▶ sub grep {goto &select}
Subroutine grep: called CC=1 SCAR=0.0
Pod documented
265  
266 sub reject {
267 11 ▶ my $self = shift;
Subroutine reject: called CC=1 SCAR=0.0
Pod documented
268 11 my $sub = $to_sub->(@_);
269 Catmandu::Iterator->new(
270 sub {
271 sub {
272 30 ▶ state $next = $self->generator;
Subroutine __ANON__: called CC=3 SCAR=11.0
273 30 state $data;
274 30 ▶ while (defined($data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = &$next())) 31 9 40
275 31 ▶ $sub->($data) || return $data;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) or return $data 20 11 31
276 }
277 9 return;
278 }
279 10 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
280 11 );
281 }
282 47 };
283  
284 sub sorted {
285 3 ▶ my ($self, $cmp) = @_;
Subroutine sorted: called CC=3 SCAR=11.0
Pod documented
286 3 ▶ if (!defined $cmp) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $cmp) { } 1 2 3
elsif (ref $cmp) { } 1 1 2
287 1 Catmandu::ArrayIterator->new([sort @{$self->to_array}]);
288 }
289 elsif (ref $cmp) {
290 Catmandu::ArrayIterator->new(
291 1 [sort {$cmp->($a, $b)} @{$self->to_array}]);
292 }
293 else {
294 # TODO: use Schwartzian transform for more complex key
295 Catmandu::ArrayIterator->new(
296 1 [sort {$a->{$cmp} cmp $b->{$cmp}} @{$self->to_array}]);
297 }
298 }
299  
300 sub pluck {
301 2 ▶ my ($self, $key) = @_;
Subroutine pluck: called CC=1 SCAR=0.0
Pod documented
302 $self->map(
303 sub {
304 6 ▶ $_[0]->{$key};
Subroutine __ANON__: called CC=1 SCAR=0.0
305 }
306 2 );
307 }
308  
309 sub invoke {
310 5 ▶ my ($self, $method, @args) = @_;
Subroutine invoke: called CC=1 SCAR=0.0
Pod documented
311 $self->map(
312 sub {
313 16 ▶ $_[0]->$method(@args);
Subroutine __ANON__: called CC=1 SCAR=0.0
314 }
315 5 );
316 }
317  
318 4 ▶sub contains {goto &includes}
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
319  
320 sub includes {
321 6 ▶ my ($self, $data) = @_;
Subroutine includes: called CC=1 SCAR=0.0
Pod documented
322 $self->any(
323 sub {
324 17 ▶ is_same($data, $_[0]);
Subroutine __ANON__: called CC=1 SCAR=0.0
325 }
326 6 );
327 }
328  
329 sub group {
330 3 ▶ my ($self, $size) = @_;
Subroutine group: called CC=1 SCAR=0.0
Pod documented
331 Catmandu::Iterator->new(
332 sub {
333 sub {
334 17 ▶ state $next = $self->generator;
Subroutine __ANON__: called CC=4 SCAR=13.9
335  
336 17 my $group = [];
337  
338 17 ▶ for (my $i = 0; $i < $size; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $size) 24 13 37
339 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
340 }
341  
342 17 ▶ unless (@$group) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$group) 3 14 17
343 3 return;
344 }
345  
346 14 Catmandu::ArrayIterator->new($group);
347 }
348 3 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
349 3 );
350 }
351  
352 sub interleave {
353 4 ▶ my @iterators = @_;
Subroutine interleave: called CC=1 SCAR=0.0
Pod documented
354 Catmandu::Iterator->new(
355 sub {
356 sub {
357 26 ▶ state @generators;
Subroutine __ANON__: called CC=5 SCAR=16.1
358 26 state $n = @iterators;
359 26 state $i = 0;
360 26 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 30 4 34
361 30 ▶ $i = 0 if $i == $n;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $n 12 18 30
362 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
363 30 ▶ if (defined(my $data = $next->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $data = &$next())) { } 22 8 30
364 22 $i++;
365 22 return $data;
366 }
367 else {
368 8 splice @generators, $i, 1;
369 8 $n--;
370 }
371 }
372 4 return;
373 }
374 4 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
375 4 );
376 }
377  
378 sub max {
379 5 ▶ my ($self, $sub) = @_;
Subroutine max: called CC=1 SCAR=0.0
Pod documented
380 $self->reduce(
381 undef,
382 sub {
383 12 ▶ my ($memo, $data) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.5
384 12 ▶ my $val = defined $sub ? $sub->($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 3 9 12
385 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
386 if is_number($memo) && is_number($val);
387 7 ▶ return $memo if is_number($memo);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($memo) 2 5 7
388 5 ▶ return $val if is_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($val) 2 3 5
389 3 return;
390 }
391 5 );
392 }
393  
394 sub min {
395 5 ▶ my ($self, $sub) = @_;
Subroutine min: called CC=1 SCAR=0.0
Pod documented
396 $_[0]->reduce(
397 undef,
398 sub {
399 12 ▶ my ($memo, $data) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.5
400 12 ▶ my $val = defined $sub ? $sub->($data) : $data;
Branch: 1 branch
BranchTrueFalseTotal
defined $sub ? : 3 9 12
401 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
402 if is_number($memo) && is_number($val);
403 7 ▶ return $memo if is_number($memo);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($memo) 2 5 7
404 5 ▶ return $val if is_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if is_number($val) 2 3 5
405 3 return;
406 }
407 5 );
408 }
409  
410 sub benchmark {
411 1 ▶ my ($self) = @_;
Subroutine benchmark: called CC=1 SCAR=0.2
Pod documented
412 $self->tap(
413 sub {
414 4 ▶ state $n = 0;
Subroutine __ANON__: called CC=2 SCAR=7.6
415 4 state $t = [gettimeofday];
416 4 ▶ if (++$n % 100 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$n % 100 == 0) 0 4 4
417 0 printf STDERR "added %9d (%d/sec)\n", $n,
418 $n / tv_interval($t);
419 }
420 }
421 1 );
422 }
423  
424 sub format {
425 6 ▶ my ($self, %opts) = @_;
Subroutine format: called CC=6 SCAR=18.0
Pod documented
426 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
427 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
428 6 ▶ my @cols = $opts{cols} ? @{$opts{cols}} : ();
Branch: 1 branch
BranchTrueFalseTotal
$opts{'cols'} ? : 6 0 6
429 6 my @col_lengths = map length, @cols;
430  
431 my $rows = $self->map(
432 sub {
433 543 ▶ my $data = $_[0];
Subroutine __ANON__: called CC=4 SCAR=13.9
434 543 my $row = [];
435 543 ▶ for (my $i = 0; $i < @cols; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @cols) 1629 543 2172
436 1629 ▶ my $col = $data->{$cols[$i]} // "";
Condition100%2/2
$$data{$cols[$i]} // ""
l!l
16245
MC/DC100%1/1
$$data{$cols[$i]} // ""
$$data{$cols[$i]}
Truth table100%2/2
A
A= $$data{$cols[$i]}
Aresult
11
00
437 1629 my $len = length $col;
438 1629 ▶ $col_lengths[$i] = $len if $len > $col_lengths[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $len > $col_lengths[$i] 38 1591 1629
439 1629 push @$row, $col;
440 }
441 543 $row;
442 }
443 6 )->to_array;
444  
445 6 my @indices = 0 .. @cols - 1;
446 my $pattern
447 6 = join($opts{col_sep}, map {"%-$col_lengths[$_]s"} @indices) . "\n";
448  
449 6 ▶ if ($opts{header}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"header"}) 6 0 6
450 6 printf $pattern, @cols;
451 6 my $sep = $opts{col_sep};
452 6 $sep =~ s/[^|]/-/g;
453 6 print join($sep, map {'-' x $col_lengths[$_]} @indices) . "\n";
454 }
455 6 for my $row (@$rows) {
456 543 printf $pattern, @$row;
457 }
458 }
459  
460 sub stop_if {
461 2 ▶ my ($self, $sub) = @_;
Subroutine stop_if: called CC=1 SCAR=0.0
Pod documented
462 Catmandu::Iterator->new(
463 sub {
464 sub {
465 5 ▶ state $next = $self->generator;
Subroutine __ANON__: called CC=3 SCAR=11.0
466 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
467 5 ▶ $sub->($data) && return;
Branch: 1 branch
BranchTrueFalseTotal
&$sub($data) and return 2 3 5
468 3 $data;
469 }
470 2 ▶ }
Subroutine __ANON__: called CC=1 SCAR=0.0
471 2 );
472 }
473  
474 47 1;
475  
476 __END__
477  
478 =pod
479  
480 =head1 NAME
481  
482 Catmandu::Iterable - Base class for all iterable Catmandu classes
483  
484 =head1 SYNOPSIS
485  
486 # Create an example Iterable using the Catmandu::Importer::Mock class
487 my $it = Catmandu::Importer::Mock->new(size => 10);
488  
489 my $array_ref = $it->to_array;
490 my $num = $it->count;
491  
492 # Loop functions
493 $it->each(sub { print shift->{n} });
494  
495 my $item = $it->first;
496  
497 $it->rest
498 ->each(sub { print shift->{n} });
499  
500 $it->slice(3,2)
501 ->each(sub { print shift->{n} });
502  
503 $it->take(5)
504 ->each(sub { print shift->{n} });
505  
506 $it->group(5)
507 ->each(sub { printf "group of %d items\n" , shift->count});
508  
509 $it->tap(\&logme)->tap(\&printme)->tap(\&mailme)
510 ->each(sub { print shift->{n} });
511  
512 my $titles = $it->pluck('title')->to_array;
513  
514 # Select and loop
515 my $item = $it->detect(sub { shift->{n} > 5 });
516  
517 $it->select(sub { shift->{n} > 5})
518 ->each(sub { print shift->{n} });
519  
520 $it->reject(sub { shift->{n} > 5})
521 ->each(sub { print shift->{n} });
522  
523 # Boolean
524 if ($it->any(sub { shift->{n} > 5}) {
525 .. at least one n > 5 ..
526 }
527  
528 if ($it->many(sub { shift->{n} > 5}) {
529 .. at least two n > 5 ..
530 }
531  
532 if ($it->all(sub { shift->{n} > 5}) {
533 .. all n > 5 ..
534 }
535  
536 # Modify and summary
537 my $it2 = $it->map(sub { shift->{n} * 2 });
538  
539 my $sum = $it2->reduce(0,sub {
540 my ($prev,$this) = @_;
541 $prev + $this;
542 });
543  
544 my $it3 = $it->group(2)->invoke('to_array');
545  
546 # Calculate maximum of 'n' field
547 my $max = $it->max(sub {
548 shift->{n};
549 });
550  
551 # Calculate minimum of 'n' field
552 my $in = $it->min(sub {
553 shift->{n};
554 });
555  
556 =head1 DESCRIPTION
557  
558 The Catmandu::Iterable class provides many list methods to Iterators such as Importers and
559 Exporters. Most of the methods are lazy if the underlying datastream supports it. Beware of
560 idempotence: many iterators contain state information and calls will give different results on
561 a second invocation.
562  
563 =head1 METHODS
564  
565 =head2 to_array
566  
567 Return all the items in the Iterator as an ARRAY ref.
568  
569 =head2 count
570  
571 Return the count of all the items in the Iterator.
572  
573 =head3 LOOPING
574  
575 =head2 each(\&callback)
576  
577 For each item in the Iterator execute the callback function with the item as first argument. Returns
578 the number of items in the Iterator.
579  
580 =head2 first
581  
582 Return the first item from the Iterator.
583  
584 =head2 rest
585  
586 Returns an Iterator containing everything except the first item.
587  
588 =head2 slice(INDEX,LENGTH)
589  
590 Returns an Iterator starting at the item at INDEX returning at most LENGTH results.
591  
592 =head2 take(NUM)
593  
594 Returns an Iterator with the first NUM results.
595  
596 =head2 group(NUM)
597  
598 Splitting the Iterator into NUM parts and returning an Iterator for each part.
599  
600 =head2 interleave(@iterators)
601  
602 Returns an Iterator which returns the first item of each iterator then the
603 second of each and so on.
604  
605 =head2 contains($data)
606  
607 Alias for C<includes>.
608  
609 =head2 includes($data)
610  
611 return true if any item in the collection is deeply equal to C<$data>.
612  
613 =head2 tap(\&callback)
614  
615 Returns a copy of the Iterator and executing callback on each item. This method works
616 like the Unix L<tee> command. Use this command to peek into an iterable while it is
617 processing results. E.g. you are writing code to process an iterable and wrote
618 something like:
619  
620 $it->each(sub {
621 # Very complicated routine
622 ...
623 });
624  
625 Now you would like to benchmark this piece of code (how fast are we processing).
626 This can be done by tapping into the iterator and calling a 'benchmark' subroutine
627 in your program that for instance counts the number of items divided by the
628 execution time.
629  
630 $it->tap(\&benchmark)->each(sub {
631 # Very complicated routine
632 ...
633 });
634  
635 sub benchmark {
636 my $item = shift;
637 $start ||= time;
638 $count++;
639  
640 printf "%d recs/sec\n" , $count/(time - $start + 1) if $count % 100 == 0;
641 }
642  
643 Note that the C<benchmark> method already implements this common case.
644  
645 =head2 detect(\&callback)
646  
647 Returns the first item for which callback returns a true value.
648  
649 =head2 detect(qr/..../)
650  
651 If the iterator contains STRING values, then return the first item which matches the
652 regex.
653  
654 =head2 detect($key => $val)
655  
656 If the iterator contains HASH values, then return the first item where the value of
657 C<$key> is equal to val.
658  
659 =head2 detect($key => qr/..../)
660  
661 If the iterator contains HASH values, then return the first item where the value of
662 C<$key> matches the regex.
663  
664 =head2 detect($key => [$val, ...])
665  
666 If the iterator contains HASH values, then return the first item where the value of
667 C<$key> is equal to any of the vals given.
668  
669 =head2 pluck($key)
670  
671 Return an Iterator that only contains the values of the given C<$key>.
672  
673 =head2 select(\&callback)
674  
675 Returns an Iterator for each item for which callback returns a true value.
676  
677 =head2 select(qr/..../)
678  
679 If the iterator contains STRING values, then return each item which matches the regex.
680  
681 =head2 select($key => $val)
682  
683 If the iterator contains HASH values, then return each item where the value of
684 C<$key> is equal to val.
685  
686 =head2 select($key => qr/..../)
687  
688 If the iterator contains HASH values, then return each item where the value of C<$key>
689 matches the regex.
690  
691 =head2 select($key => [$val, ...])
692  
693 If the iterator contains HASH values, then return each item where the value of
694 C<$key> is equal to any of the vals given.
695  
696 =head2 grep( ... )
697  
698 Alias for C<select( ... )>.
699  
700 =head2 reject(\&callback)
701  
702 Returns an Iterator for each item for which callback returns a false value.
703  
704 =head2 reject(qr/..../)
705  
706 If the iterator contains STRING values, then reject every item except those
707 matching the regex.
708  
709 =head2 reject($key => qr/..../)
710  
711 If the iterator contains HASH values, then reject every item for where the value of $key
712 DOESN'T match the regex.
713  
714 =head2 reject($key => $val)
715  
716 If the iterator contains HASH values, then return each item where the value of
717 $key is NOT equal to val.
718  
719 =head2 reject($key => [$val, ...])
720  
721 If the iterator contains HASH values, then return each item where the value of
722 $key is NOT equal to any of the vals given.
723  
724 =head2 sorted
725  
726 Returns an Iterator with items sorted lexically. Note that sorting requires
727 memory because all items are buffered in a L<Catmandu::ArrayIterator>.
728  
729 =head2 sorted(\&callback)
730  
731 Returns an Iterator with items sorted by a callback. The callback is expected to
732 returns an integer less than, equal to, or greater than C<0>. The following code
733 snippets result in the equal arrays:
734  
735 $iterator->sorted(\&callback)->to_array
736 [ sort \&callback @{ $iterator->to_array } ]
737  
738 =head2 sorted($key)
739  
740 Returns an Iterator with items lexically sorted by a key. This is equivalent to
741 sorting with the following callback:
742  
743 $iterator->sorted(sub { $_[0]->{$key} cmp $_[1]->{$key} })
744  
745 =head3 EXTERNAL ITERATOR
746  
747 L<Catmandu::Iterable> behaves like an internal iterator. C<next> and C<rewind>
748 allow you to use it like an external iterator.
749  
750 =head2 next
751  
752 Each call to C<next> will return the next item until the iterator is exhausted,
753 then it will keep returning C<undef>.
754  
755 while (my $data = $it->next) {
756 # do stuff
757 }
758  
759 $it->next; # returns undef
760  
761 =head2 rewind
762  
763 Rewind the external iterator to the first item.
764  
765 $it->next; # => {n => 1}
766 $it->next; # => {n => 2}
767 $it->next; # => {n => 3}
768 $it->rewind
769 $it->next; # => {n => 1}
770  
771 Note the the iterator must support this behavior. Many importers are not
772 rewindable.
773  
774 =head3 BOOLEAN FUNCTIONS
775  
776 =head2 any(\&callback)
777  
778 Returns true if at least one item generates a true value when executing callback.
779  
780 =head2 many(\&callback)
781  
782 Alias for C<many>.
783  
784 =head2 many(\&callback)
785  
786 Returns true if at least two items generate a true value when executing callback.
787  
788 =head2 all(\&callback)
789  
790 Returns true if all the items generate a true value when executing callback.
791  
792 =head3 MAP & REDUCE
793  
794 =head2 map(\&callback)
795  
796 Returns a new Iterator containing for each item the result of the callback.
797  
798 =head2 reduce([START],\&callback)
799  
800 Alias for C<reduce>.
801  
802 =head2 reduce([START],\&callback)
803  
804 For each item in the Iterator execute &callback($prev,$item) where $prev is the
805 option START value or the result of the previous call to callback. Returns the
806 final result of the callback function.
807  
808 =head2 invoke(NAME)
809  
810 Returns an interator were the method NAME is called on every object in the iterable.
811 This is a shortcut for $it->map(sub { $_[0]->NAME }).
812  
813 =head2 max()
814  
815 Returns the maximum of an iterator containing only numbers.
816  
817 =head2 max(\&callback)
818  
819 Returns the maximum of the numbers returned by executing callback.
820  
821 =head2 min()
822  
823 Returns the minimum of an iterator containing only numbers.
824  
825 =head2 min(\&callback)
826  
827 Returns the minimum of the numbers returned by executing callback.
828  
829 =head2 benchmark()
830  
831 Prints the number of records processed per second to STDERR.
832  
833 =head2 format(cols => ['key', ...], col_sep => ' | ', header => 1|0)
834  
835 Print the iterator data formatted as a spreadsheet like table. Note that this
836 method will load the whole dataset in memory to calculate column widths. See
837 also L<Catmandu::Exporter::Table> for a more elaborated method of printing
838 iterators in tabular form.
839  
840 =head2 stop_if(\&callback)
841  
842 Returns a new iterator thats stops processing if the callback returns false.
843  
844 # stop after encountering 3 frobnitzes
845 my $frobnitzes = 0;
846 $iterator->stop_if(sub {
847 my $rec = shift;
848 $frobnitzes++ if $rec->{title} =~ /frobnitz/;
849 $frobnitzes > 3;
850 })->each(sub {
851 my $rec = shift;
852 ...
853 });
854  
855 =head1 SEE ALSO
856  
857 L<Catmandu::Iterator>.
858  
859 =cut
« blib/lib/Catmandu/Exporter/Text.pm ↑ Summary blib/lib/Catmandu/Fix/sleep.pm »