lib/CSV/LINQ.pm

filter: Statementstmt 62.5% 462 / 739 Branchbran 43.6% 180 / 412 Conditioncond 33.3% 24 / 72 MC/DCmcdc 19.1% 9 / 47 Subroutinesub 68.8% 75 / 109 Podpod 98.1% 54 / 55 totaltotal 56.0% 804 / 1434 CC 238 SCAR 28.0
CC
238
Cov
54%
Contains
72.0
SequenceEqual
72.0
__ANON__
42.0
CRAP 16.4
↑ Summary
1 package CSV::LINQ;
2 ###############################################################################
3 # CSV::LINQ - LINQ-style query interface for CSV files
4 # Compatible: Perl 5.005_03 and later
5 # Platform : Windows / UNIX
6 ###############################################################################
7  
8 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub'; eval 'package warnings; sub import {}' } }
Subroutine BEGIN: called CC=3 SCAR=16.5
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006" and not defined &warnings::import) 0 5 5
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
500
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006" not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
ABresult
0X0
100
111
10 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 local $^W = 1;
12  
13 5 ▶BEGIN { pop @INC if $INC[-1] eq '.' }
Subroutine BEGIN: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 0 5 5
14  
15 5 ▶use vars qw($VERSION $_fh_seq);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 $VERSION = '1.00';
17 5 $VERSION = $VERSION;
18 5 $_fh_seq = 0;
19  
20 ###############################################################################
21 # Internal file-handle helper
22 ###############################################################################
23  
24 # _open_fh - open a file for reading ('<') or writing ('>') and return
25 # the glob name string. Works on Perl 5.005_03 and all later versions.
26 #
27 # Always uses a unique numbered package glob (CSV::LINQ::FH::H<n>) so
28 # that concurrent iterators each get their own IO slot.
29 #
30 # $raw: if true, binmode is called (raw bytes).
31 # Pass 0 for CSV, where OS-level \r\n->\n conversion is desired.
32 35 ▶sub _open_fh {
Subroutine _open_fh: called CC=4 SCAR=14.1
33 35 my($mode, $file, $raw) = @_;
34 35 $_fh_seq++;
35 35 my $seq = $_fh_seq;
36 35 my $fhn = "CSV::LINQ::FH::H${seq}";
37 35 ▶ my $arg = ($mode eq '>') ? ">$file" : "< $file";
Branch: 1 branch
BranchTrueFalseTotal
$mode eq '>' ? : 8 27 35
38 5 ▶ { no strict 'refs'; open($fhn, $arg) or die "Cannot open '$file': $!\n" }
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open $fhn, $arg or die "Cannot open '${file}': $!\n" 35 0 35
39 0 ▶ if ($raw) { no strict 'refs'; binmode(*{$fhn}) }
Subroutine BEGIN: called CC=1 SCAR=0.5
Branch: 1 branch
BranchTrueFalseTotal
if ($raw) 0 35 35
40 35 return $fhn;
41 }
42  
43 ###############################################################################
44 # Constructor
45 ###############################################################################
46  
47 202 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
48 202 my($class, $iter) = @_;
49 202 return bless { _iter => $iter }, $class;
50 }
51  
52 ###############################################################################
53 # CSV parsing (RFC 4180 compliant)
54 ###############################################################################
55  
56 103 ▶sub _parse_csv_line {
Subroutine _parse_csv_line: called CC=13 SCAR=25.7
57 103 my($line, $sep) = @_;
58 103 ▶ $sep = ',' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 0 103 103
59 103 my @fields = ();
60 103 $line =~ s{\r\n\z|\r\z|\n\z}{};
61 103 my $pos = 0;
62 103 my $len = length($line);
63 103 ▶ while ($pos <= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $len) 286 103 389
64 286 ▶ if ($pos < $len && substr($line, $pos, 1) eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($line, $pos, 1) eq '"') { } 4 282 286
Condition100%3/3
$pos < $len and substr($line, $pos, 1) eq "\""
!ll&&!rl&&r
12814
MC/DC100%2/2
$pos < $len and substr($line, $pos, 1) eq "\""
$pos < $len substr($line, $pos, 1) eq "\""
Truth table100%3/3
A and B
A= $pos < $lenB= substr($line, $pos, 1) eq "\""
ABresult
0X0
100
111
65 4 $pos++;
66 4 my $field = '';
67 4 ▶ while ($pos < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len) 46 0 46
68 46 my $c = substr($line, $pos, 1);
69 46 ▶ if ($c eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq '"') { } 6 40 46
70 6 $pos++;
71 6 ▶ if ($pos < $len && substr($line, $pos, 1) eq '"') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($line, $pos, 1) eq '"') { } 2 4 6
Condition100%3/3
$pos < $len and substr($line, $pos, 1) eq "\""
!ll&&!rl&&r
132
MC/DC100%2/2
$pos < $len and substr($line, $pos, 1) eq "\""
$pos < $len substr($line, $pos, 1) eq "\""
Truth table100%3/3
A and B
A= $pos < $lenB= substr($line, $pos, 1) eq "\""
ABresult
0X0
100
111
72 2 $field .= '"';
73 2 $pos++;
74 }
75 else {
76 4 last;
77 }
78 }
79 else {
80 40 $field .= $c;
81 40 $pos++;
82 }
83 }
84 4 push @fields, $field;
85 4 ▶ $pos++ if $pos < $len && substr($line, $pos, 1) eq $sep;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < $len and substr($line, $pos, 1) eq $sep 3 1 4
Condition66%2/3
$pos < $len and substr($line, $pos, 1) eq $sep
!ll&&!rl&&r
103
MC/DC50%1/2
$pos < $len and substr($line, $pos, 1) eq $sep
$pos < $len substr($line, $pos, 1) eq $sep
Truth table66%2/3
A and B
A= $pos < $lenB= substr($line, $pos, 1) eq $sep
ABresult
0X0
100
111
86 }
87 else {
88 282 my $start = $pos;
89 282 ▶ while ($pos < $len && substr($line, $pos, 1) ne $sep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $len and substr($line, $pos, 1) ne $sep) 1003 282 1285
Condition100%3/3
$pos < $len and substr($line, $pos, 1) ne $sep
!ll&&!rl&&r
1031791003
MC/DC100%2/2
$pos < $len and substr($line, $pos, 1) ne $sep
$pos < $len substr($line, $pos, 1) ne $sep
Truth table100%3/3
A and B
A= $pos < $lenB= substr($line, $pos, 1) ne $sep
ABresult
0X0
100
111
90 1003 $pos++;
91 }
92 282 push @fields, substr($line, $start, $pos - $start);
93 282 $pos++;
94 }
95 }
96 103 return @fields;
97 }
98  
99 78 ▶sub _format_csv_field {
Subroutine _format_csv_field: called CC=5 SCAR=19.6
100 78 my($value, $sep) = @_;
101 78 ▶ $sep = ',' unless defined $sep;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sep 0 78 78
102 78 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 78 78
103 78 ▶ if ($value =~ /["\n\r]/ || index($value, $sep) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /["\n\r]/ or index($value, $sep) != -1) 0 78 78
Condition33%1/3
$value =~ /["\n\r]/ or index($value, $sep) != -1
l!l&&r!l&&!r
0078
MC/DC0%0/2
$value =~ /["\n\r]/ or index($value, $sep) != -1
$value =~ /["\n\r]/ index($value, $sep) != -1
Truth table33%1/3
A or B
A= $value =~ /["\n\r]/B= index($value, $sep) != -1
ABresult
1X1
011
000
104 0 $value =~ s/"/""/g;
105 0 return '"' . $value . '"';
106 }
107 78 return $value;
108 }
109  
110 ###############################################################################
111 # Data source methods
112 ###############################################################################
113  
114 99 ▶sub From {
Subroutine From: called CC=2 SCAR=6.9
Pod documented
115 99 my($class, $arrayref) = @_;
116 99 ▶ die "From() requires ARRAY reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arrayref eq "ARRAY" 0 99 99
117 unless ref($arrayref) eq 'ARRAY';
118 99 my $i = 0;
119 505 ▶ my $iter = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
120 505 ▶ return undef if $i >= scalar(@{$arrayref});
Branch: 1 branch
BranchTrueFalseTotal
if $i >= scalar @{$arrayref;} 95 410 505
121 410 return $arrayref->[$i++];
122 99 };
123 99 return $class->new($iter);
124 }
125  
126 27 ▶sub FromCSV {
Subroutine FromCSV: called CC=5 SCAR=16.5
Pod documented
127 27 my($class, $file, %opts) = @_;
128 27 ▶ my $sep = defined $opts{sep} ? $opts{sep} : ',';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'sep'} ? : 3 24 27
129 27 my $headers = $opts{headers};
130 27 my $skip = $opts{skip_header};
131 27 my $fhn = _open_fh('<', $file, 0);
132 27 my @cols = ();
133 {
134 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
135 27 ▶ if (!defined $headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $headers) { } 27 0 27
136 27 my $hdr = readline(*{$fhn});
137 27 ▶ if (defined $hdr) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hdr) 27 0 27
138 27 @cols = _parse_csv_line($hdr, $sep);
139 }
140 }
141 else {
142 0 @cols = @{$headers};
143 0 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 0 0 0
144 0 readline(*{$fhn});
145 }
146 }
147 }
148 98 ▶ my $iter = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
149 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
150 98 my $line = readline(*{$fhn});
151 98 ▶ if (!defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 27 71 98
152 27 close(*{$fhn});
153 27 return undef;
154 }
155 71 $line =~ s{\r\n\z|\r\z|\n\z}{};
156 71 ▶ return undef if $line eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 0 71 71
157 71 my @vals = _parse_csv_line($line, $sep);
158 71 my %rec = ();
159 71 for my $i (0 .. $#cols) {
160 199 $rec{ $cols[$i] } = $vals[$i];
161 }
162 71 return { %rec };
163 27 };
164 27 return $class->new($iter);
165 }
166  
167 10 ▶sub Range {
Subroutine Range: called CC=1 SCAR=0.0
Pod documented
168 10 my($class, $start, $count) = @_;
169 10 my $current = $start;
170 10 my $end = $start + $count - 1;
171 52 ▶ my $iter = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
172 52 ▶ return undef if $current > $end;
Branch: 1 branch
BranchTrueFalseTotal
if $current > $end 7 45 52
173 45 return $current++;
174 10 };
175 10 return $class->new($iter);
176 }
177  
178 0 ▶sub Empty {
Subroutine Empty: not called CC=1 SCAR=6.9
Pod documented
179 0 my($class) = @_;
180 0 ▶ my $iter = sub { return undef };
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 return $class->new($iter);
182 }
183  
184 0 ▶sub Repeat {
Subroutine Repeat: not called CC=1 SCAR=6.9
Pod documented
185 0 my($class, $element, $count) = @_;
186 0 my $i = 0;
187 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 ▶ return undef if $i >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $count 0 0 0
189 0 $i++;
190 0 return $element;
191 0 };
192 0 return $class->new($iter);
193 }
194  
195 ###############################################################################
196 # Internal iterator helpers
197 ###############################################################################
198  
199 787 ▶sub _next {
Subroutine _next: called CC=1 SCAR=0.0
200 787 my($self) = @_;
201 787 return $self->{_iter}->();
202 }
203  
204 ###############################################################################
205 # Filtering methods
206 ###############################################################################
207  
208 22 ▶sub Where {
Subroutine Where: called CC=3 SCAR=11.0
Pod documented
209 22 my($self, @args) = @_;
210 22 my $pred;
211 22 ▶ if (ref($args[0]) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq 'CODE') { } 6 16 22
212 6 $pred = $args[0];
213 }
214 else {
215 16 ▶ die "Where() DSL requires even number of arguments\n"
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 != 0 0 16 16
216 if @args % 2 != 0;
217 16 my %cond = @args;
218 50 ▶ $pred = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
219 50 my $rec = $_[0];
220 50 for my $k (keys %cond) {
221 50 ▶ return 0 unless defined $rec->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rec->{$k} 0 50 50
222 50 ▶ return 0 unless $rec->{$k} eq $cond{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $rec->{$k} eq $cond{$k} 18 32 50
223 }
224 32 return 1;
225 16 };
226 }
227 22 my $source = $self;
228 71 ▶ my $iter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
229 71 while (1) {
230 103 my $elem = $source->_next();
231 103 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 21 82 103
232 82 ▶ return $elem if $pred->($elem);
Branch: 1 branch
BranchTrueFalseTotal
if &$pred($elem) 50 32 82
233 }
234 22 };
235 22 return ref($self)->new($iter);
236 }
237  
238 ###############################################################################
239 # Projection methods
240 ###############################################################################
241  
242 21 ▶sub Select {
Subroutine Select: called CC=1 SCAR=0.0
Pod documented
243 21 my($self, $selector) = @_;
244 21 my $source = $self;
245 131 ▶ my $iter = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
246 131 my $elem = $source->_next();
247 131 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 21 110 131
248 110 return $selector->($elem);
249 21 };
250 21 return ref($self)->new($iter);
251 }
252  
253 3 ▶sub SelectMany {
Subroutine SelectMany: called CC=1 SCAR=0.0
Pod documented
254 3 my($self, $selector) = @_;
255 3 my $source = $self;
256 3 my @buf = ();
257 18 ▶ my $iter = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
258 18 while (1) {
259 27 ▶ if (@buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@buf) 15 12 27
260 15 return shift @buf;
261 }
262 12 my $elem = $source->_next();
263 12 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 3 9 12
264 9 my $arr = $selector->($elem);
265 9 ▶ die "SelectMany: selector must return an ARRAY reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arr eq "ARRAY" 0 9 9
266 unless ref($arr) eq 'ARRAY';
267 9 push @buf, @{$arr};
268 }
269 3 };
270 3 return ref($self)->new($iter);
271 }
272  
273 ###############################################################################
274 # Concatenation methods
275 ###############################################################################
276  
277 2 ▶sub Concat {
Subroutine Concat: called CC=1 SCAR=0.0
Pod documented
278 2 my($self, $second) = @_;
279 2 my $source = $self;
280 2 my $second2 = $second;
281 2 my $first_done = 0;
282 14 ▶ my $iter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
283 14 ▶ unless ($first_done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first_done) 8 6 14
284 8 my $elem = $source->_next();
285 8 ▶ if (defined $elem) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem) 6 2 8
286 6 return $elem;
287 }
288 2 $first_done = 1;
289 }
290 8 return $second2->_next();
291 2 };
292 2 return ref($self)->new($iter);
293 }
294  
295 0 ▶sub Zip {
Subroutine Zip: not called CC=1 SCAR=6.9
Pod documented
296 0 my($self, $second, $selector) = @_;
297 0 my $src1 = $self;
298 0 my $src2 = $second;
299 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
300 0 my $e1 = $src1->_next();
301 0 my $e2 = $src2->_next();
302 0 ▶ return undef unless defined $e1 && defined $e2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e1 and defined $e2 0 0 0
Condition0%0/3
defined $e1 and defined $e2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e1 and defined $e2
defined $e1 defined $e2
Truth table0%0/3
A and B
A= defined $e1B= defined $e2
ABresult
0X0
100
111
303 0 return $selector->($e1, $e2);
304 0 };
305 0 return ref($self)->new($iter);
306 }
307  
308 ###############################################################################
309 # Partitioning methods
310 ###############################################################################
311  
312 7 ▶sub Take {
Subroutine Take: called CC=2 SCAR=6.9
Pod documented
313 7 my($self, $count) = @_;
314 7 ▶ $count = 0 if $count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 0 7 7
315 7 my $source = $self;
316 7 my $taken = 0;
317 24 ▶ my $iter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
318 24 ▶ return undef if $taken >= $count;
Branch: 1 branch
BranchTrueFalseTotal
if $taken >= $count 6 18 24
319 18 my $elem = $source->_next();
320 18 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 1 17 18
321 17 $taken++;
322 17 return $elem;
323 7 };
324 7 return ref($self)->new($iter);
325 }
326  
327 5 ▶sub Skip {
Subroutine Skip: called CC=2 SCAR=7.0
Pod documented
328 5 my($self, $count) = @_;
329 5 ▶ $count = 0 if $count < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count < 0 0 5 5
330 5 my $source = $self;
331 5 my $skipped = 0;
332 14 ▶ my $iter = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
333 14 ▶ while ($skipped < $count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipped < $count) 13 14 27
334 13 my $elem = $source->_next();
335 13 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 13 13
336 13 $skipped++;
337 }
338 14 return $source->_next();
339 5 };
340 5 return ref($self)->new($iter);
341 }
342  
343 0 ▶sub TakeWhile {
Subroutine TakeWhile: not called CC=1 SCAR=6.9
Pod documented
344 0 my($self, $pred) = @_;
345 0 my $source = $self;
346 0 my $done = 0;
347 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
348 0 ▶ return undef if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
349 0 my $elem = $source->_next();
350 0 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 0 0
351 0 ▶ if ($pred->($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$pred($elem)) 0 0 0
352 0 return $elem;
353 }
354 0 $done = 1;
355 0 return undef;
356 0 };
357 0 return ref($self)->new($iter);
358 }
359  
360 0 ▶sub SkipWhile {
Subroutine SkipWhile: not called CC=1 SCAR=6.9
Pod documented
361 0 my($self, $pred) = @_;
362 0 my $source = $self;
363 0 my $skipping = 1;
364 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
365 0 ▶ while ($skipping) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skipping) 0 0 0
366 0 my $elem = $source->_next();
367 0 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 0 0
368 0 ▶ unless ($pred->($elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$pred($elem)) 0 0 0
369 0 $skipping = 0;
370 0 return $elem;
371 }
372 }
373 0 return $source->_next();
374 0 };
375 0 return ref($self)->new($iter);
376 }
377  
378 ###############################################################################
379 # Ordering methods
380 ###############################################################################
381  
382 16 ▶sub OrderBy {
Subroutine OrderBy: called CC=2 SCAR=6.9
Pod documented
383 16 my($self, $key_sel) = @_;
384 16 my @items = ();
385 16 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 89 16 105
386 16 return CSV::LINQ::Ordered->_new_ordered([ @items ],
387 [{ sel => $key_sel, dir => 1, type => 'smart' }]);
388 }
389  
390 1 ▶sub OrderByDescending {
Subroutine OrderByDescending: called CC=2 SCAR=6.9
Pod documented
391 1 my($self, $key_sel) = @_;
392 1 my @items = ();
393 1 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 7 1 8
394 1 return CSV::LINQ::Ordered->_new_ordered([ @items ],
395 [{ sel => $key_sel, dir => -1, type => 'smart' }]);
396 }
397  
398 24 ▶sub OrderByStr {
Subroutine OrderByStr: called CC=2 SCAR=6.9
Pod documented
399 24 my($self, $key_sel) = @_;
400 24 my @items = ();
401 24 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 94 24 118
402 24 return CSV::LINQ::Ordered->_new_ordered([ @items ],
403 [{ sel => $key_sel, dir => 1, type => 'str' }]);
404 }
405  
406 5 ▶sub OrderByStrDescending {
Subroutine OrderByStrDescending: called CC=2 SCAR=6.9
Pod documented
407 5 my($self, $key_sel) = @_;
408 5 my @items = ();
409 5 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 15 5 20
410 5 return CSV::LINQ::Ordered->_new_ordered([ @items ],
411 [{ sel => $key_sel, dir => -1, type => 'str' }]);
412 }
413  
414 16 ▶sub OrderByNum {
Subroutine OrderByNum: called CC=2 SCAR=6.9
Pod documented
415 16 my($self, $key_sel) = @_;
416 16 my @items = ();
417 16 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 72 16 88
418 16 return CSV::LINQ::Ordered->_new_ordered([ @items ],
419 [{ sel => $key_sel, dir => 1, type => 'num' }]);
420 }
421  
422 5 ▶sub OrderByNumDescending {
Subroutine OrderByNumDescending: called CC=2 SCAR=6.9
Pod documented
423 5 my($self, $key_sel) = @_;
424 5 my @items = ();
425 5 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 18 5 23
426 5 return CSV::LINQ::Ordered->_new_ordered([ @items ],
427 [{ sel => $key_sel, dir => -1, type => 'num' }]);
428 }
429  
430 0 ▶sub Reverse {
Subroutine Reverse: not called CC=2 SCAR=17.9
Pod documented
431 0 my($self) = @_;
432 0 my @items = ();
433 0 ▶ while (defined(my $e = $self->_next())) { push @items, $e }
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
434 0 my @rev = reverse @items;
435 0 return ref($self)->new(ref($self)->From([ @rev ])->{_iter});
436 }
437  
438 ###############################################################################
439 # Internal sort helpers
440 ###############################################################################
441  
442 # _extract_key($raw_value, $type) - normalise one sort key
443 #
444 # Returns a scalar for 'num'/'str', or a two-element arrayref [flag, value]
445 # for 'smart':
446 # [0, $numeric_val] - key is numeric
447 # [1, $string_val ] - key is string
448 459 ▶sub _extract_key {
Subroutine _extract_key: called CC=7 SCAR=19.5
449 459 my($val, $type) = @_;
450 459 ▶ $val = '' unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 2 457 459
451 459 ▶ if ($type eq 'num') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'num') { } 128 331 459
elsif ($type eq 'str') { } 172 159 331
452 128 ▶ return defined($val) && length($val) ? $val + 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $val && length $val ? : 126 2 128
Condition66%2/3
defined $val and length $val
!ll&&!rl&&r
02126
MC/DC50%1/2
defined $val and length $val
defined $val length $val
Truth table66%2/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
453 }
454 elsif ($type eq 'str') {
455 172 return "$val";
456 }
457 else {
458 # smart: detect whether value looks like a number
459 159 my $t = $val;
460 159 $t =~ s{^\s+}{};
461 159 $t =~ s{\s+$}{};
462 159 ▶ if ($t =~ /^[+-]?(?:\d+\.?\d*|\d*\.\d+)(?:[eE][+-]?\d+)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /^[+-]?(?:\d+\.?\d*|\d*\.\d+)(?:[eE][+-]?\d+)?$/) { } 8 151 159
463 8 return [0, $t + 0];
464 }
465 else {
466 151 return [1, "$val"];
467 }
468 }
469 }
470  
471 # _compare_keys($ka, $kb, $type) - compare two extracted keys
472 546 ▶sub _compare_keys {
Subroutine _compare_keys: called CC=7 SCAR=19.9
473 546 my($ka, $kb, $type) = @_;
474 546 ▶ if ($type eq 'num') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'num') { } 152 394 546
elsif ($type eq 'str') { } 186 208 394
475 152 return $ka <=> $kb;
476 }
477 elsif ($type eq 'str') {
478 186 return $ka cmp $kb;
479 }
480 else {
481 # smart: both are [flag, value] arrayrefs
482 208 my $fa = $ka->[0]; my $va = $ka->[1];
483 208 my $fb = $kb->[0]; my $vb = $kb->[1];
484 208 ▶ if ($fa == 0 && $fb == 0) { return $va <=> $vb }
Branch: 2 branches
BranchTrueFalseTotal
if ($fa == 0 and $fb == 0) { } 7 201 208
elsif ($fa == 1 and $fb == 1) { } 201 0 201
Condition66%2/3
$fa == 0 and $fb == 0
!ll&&!rl&&r
20107
MC/DC50%1/2
$fa == 0 and $fb == 0
$fa == 0 $fb == 0
Truth table66%2/3
A and B
A= $fa == 0B= $fb == 0
ABresult
0X0
100
111
485 201 ▶ elsif ($fa == 1 && $fb == 1) { return $va cmp $vb }
Condition33%1/3
$fa == 1 and $fb == 1
!ll&&!rl&&r
00201
MC/DC0%0/2
$fa == 1 and $fb == 1
$fa == 1 $fb == 1
Truth table33%1/3
A and B
A= $fa == 1B= $fb == 1
ABresult
0X0
100
111
486 0 else { return $fa <=> $fb }
487 }
488 }
489  
490 # _perform_sort($items_aref, $specs_aref) - stable multi-key sort
491 #
492 # Schwartzian Transform:
493 # 1. Decorate each element: [ orig_index, [key1..keyN], item ]
494 # 2. Sort by keys in sequence; original index as final tie-breaker (stability)
495 # 3. Undecorate
496 67 ▶sub _perform_sort {
Subroutine _perform_sort: called CC=5 SCAR=16.1
497 67 my($items, $specs) = @_;
498  
499 # Step 1: decorate
500 my @decorated = map {
501 295 my $idx = $_;
502 295 my $item = $items->[$idx];
503 295 my @keys = map { _extract_key($_->{sel}->($item), $_->{type}) } @{$specs};
504 295 [$idx, [ @keys ], $item]
505 67 } 0 .. $#{$items};
506  
507 # Step 2: sort
508 my @sorted_dec = sort {
509 67 my $r = 0;
510 437 for my $i (0 .. $#{$specs}) {
511 546 my $cmp = _compare_keys($a->[1][$i], $b->[1][$i], $specs->[$i]{type});
512 546 ▶ if ($specs->[$i]{dir} < 0) { $cmp = -$cmp }
Branch: 1 branch
BranchTrueFalseTotal
if ($specs->[$i]{"dir"} < 0) 65 481 546
513 546 ▶ if ($cmp != 0) { $r = $cmp; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp != 0) 419 127 546
514 }
515 437 ▶ $r != 0 ? $r : ($a->[0] <=> $b->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$r != 0 ? : 419 18 437
516 } @decorated;
517  
518 # Step 3: undecorate
519 67 return map { $_->[2] } @sorted_dec;
520 }
521  
522 ###############################################################################
523 # Ordered sub-class (ThenBy support)
524 ###############################################################################
525  
526 package CSV::LINQ::Ordered;
527 5 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
528 5 @ISA = ('CSV::LINQ');
529  
530 # _new_ordered($items_aref, $specs_aref) - internal constructor
531 #
532 # $specs_aref is an arrayref of sort-spec hashrefs:
533 # { sel => $code_ref, # key selector: ($item) -> $key
534 # dir => 1 or -1, # 1 = ascending, -1 = descending
535 # type => 'smart'|'str'|'num' # comparison family
536 # }
537 #
538 # Uses _factory so each _next() call draws from a fresh sorted iterator,
539 # enabling re-iteration of Ordered objects.
540 97 ▶sub _new_ordered {
Subroutine _new_ordered: called CC=1 SCAR=0.0
541 97 my($class, $items, $specs) = @_;
542 return bless {
543 _items => $items,
544 _specs => $specs,
545 67 ▶ _factory => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
546 67 my @sorted = CSV::LINQ::_perform_sort($items, $specs);
547 67 my $i = 0;
548 67 ▶ return sub { $i < scalar(@sorted) ? $sorted[$i++] : undef };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$i < scalar @sorted ? : 286 63 349
549 },
550 97 }, $class;
551 }
552  
553 # Override _next() to use _factory if available
554 # When the iterator signals end-of-sequence (undef), clear {_iter} so
555 # the next call to _next() rebuilds a fresh iterator from {_factory},
556 # enabling re-iteration of the same Ordered object.
557 510 ▶sub _next {
Subroutine _next: called CC=3 SCAR=11.0
558 510 my($self) = @_;
559 510 ▶ unless (exists $self->{_iter}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"_iter"}) 67 443 510
560 67 $self->{_iter} = $self->{_factory}->();
561 }
562 510 my $val = $self->{_iter}->();
563 510 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 91 419 510
564 91 delete $self->{_iter};
565 }
566 510 return $val;
567 }
568  
569 # _thenby - shared implementation for all ThenBy* variants
570 #
571 # Non-destructive: builds a new spec list and returns a new
572 # CSV::LINQ::Ordered object. The original object is unchanged.
573 30 ▶sub _thenby {
Subroutine _thenby: called CC=1 SCAR=0.0
574 30 my($self, $key_sel, $dir, $type) = @_;
575 30 my @new_specs = (@{$self->{_specs}}, { sel => $key_sel, dir => $dir, type => $type });
576 30 return CSV::LINQ::Ordered->_new_ordered($self->{_items}, [ @new_specs ]);
577 }
578  
579 12 ▶sub ThenBy { my($s, $k)=@_; $s->_thenby($k, 1, 'smart') }
Subroutine ThenBy: called CC=1 SCAR=0.0
580 1 ▶sub ThenByDescending { my($s, $k)=@_; $s->_thenby($k, -1, 'smart') }
Subroutine ThenByDescending: called CC=1 SCAR=0.0
581 9 ▶sub ThenByStr { my($s, $k)=@_; $s->_thenby($k, 1, 'str') }
Subroutine ThenByStr: called CC=1 SCAR=0.0
582 1 ▶sub ThenByStrDescending { my($s, $k)=@_; $s->_thenby($k, -1, 'str') }
Subroutine ThenByStrDescending: called CC=1 SCAR=0.0
583 5 ▶sub ThenByNum { my($s, $k)=@_; $s->_thenby($k, 1, 'num') }
Subroutine ThenByNum: called CC=1 SCAR=0.0
584 2 ▶sub ThenByNumDescending { my($s, $k)=@_; $s->_thenby($k, -1, 'num') }
Subroutine ThenByNumDescending: called CC=1 SCAR=0.0
585  
586 package CSV::LINQ;
587  
588  
589 ###############################################################################
590 # Grouping methods
591 ###############################################################################
592  
593 2 ▶sub GroupBy {
Subroutine GroupBy: called CC=6 SCAR=18.1
Pod documented
594 2 my($self, $key_sel, $elem_sel) = @_;
595 2 my %groups = ();
596 2 my @keys = ();
597 2 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 6 2 8
598 6 my $k = $key_sel->($e);
599 6 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 6 6
600 6 ▶ unless (exists $groups{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $groups{$k}) 4 2 6
601 4 push @keys, $k;
602 4 $groups{$k} = [];
603 }
604 6 ▶ if (defined $elem_sel) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $elem_sel) { } 0 6 6
605 0 push @{ $groups{$k} }, $elem_sel->($e);
606 }
607 else {
608 6 push @{ $groups{$k} }, $e;
609 }
610 }
611 2 my @result = ();
612 2 for my $k (@keys) {
613 4 push @result, { Key => $k, Elements => $groups{$k} };
614 }
615 2 return ref($self)->From([ @result ]);
616 }
617  
618 ###############################################################################
619 # Set operations
620 ###############################################################################
621  
622 1 ▶sub Distinct {
Subroutine Distinct: called CC=1 SCAR=0.0
Pod documented
623 1 my($self, $comparer) = @_;
624 1 my $source = $self;
625 1 my %seen = ();
626 4 ▶ my $iter = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
627 4 while (1) {
628 7 my $elem = $source->_next();
629 7 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 1 6 7
630 6 ▶ my $key = defined $comparer ? $comparer->($elem) : $elem;
Branch: 1 branch
BranchTrueFalseTotal
defined $comparer ? : 0 6 6
631 6 ▶ $key = '' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 6 6
632 6 ▶ next if $seen{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$key} 3 3 6
633 3 $seen{$key} = 1;
634 3 return $elem;
635 }
636 1 };
637 1 return ref($self)->new($iter);
638 }
639  
640 0 ▶sub Union {
Subroutine Union: not called CC=1 SCAR=6.9
Pod documented
641 0 my($self, $second, $comparer) = @_;
642 0 return $self->Concat($second)->Distinct($comparer);
643 }
644  
645 0 ▶sub Intersect {
Subroutine Intersect: not called CC=4 SCAR=30.0
Pod documented
646 0 my($self, $second, $comparer) = @_;
647 0 my %in2 = ();
648 0 ▶ while (defined(my $e = $second->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $second->_next)) 0 0 0
649 0 ▶ my $k = defined $comparer ? $comparer->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $comparer ? : 0 0 0
650 0 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
651 0 $in2{$k} = 1;
652 }
653 0 my $source = $self;
654 0 my %seen = ();
655 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
656 0 while (1) {
657 0 my $elem = $source->_next();
658 0 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 0 0
659 0 ▶ my $k = defined $comparer ? $comparer->($elem) : $elem;
Branch: 1 branch
BranchTrueFalseTotal
defined $comparer ? : 0 0 0
660 0 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
661 0 ▶ next unless $in2{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $in2{$k} 0 0 0
662 0 ▶ next if $seen{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$k} 0 0 0
663 0 $seen{$k} = 1;
664 0 return $elem;
665 }
666 0 };
667 0 return ref($self)->new($iter);
668 }
669  
670 0 ▶sub Except {
Subroutine Except: not called CC=4 SCAR=30.0
Pod documented
671 0 my($self, $second, $comparer) = @_;
672 0 my %in2 = ();
673 0 ▶ while (defined(my $e = $second->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $second->_next)) 0 0 0
674 0 ▶ my $k = defined $comparer ? $comparer->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $comparer ? : 0 0 0
675 0 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
676 0 $in2{$k} = 1;
677 }
678 0 my $source = $self;
679 0 my %seen = ();
680 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
681 0 while (1) {
682 0 my $elem = $source->_next();
683 0 ▶ return undef unless defined $elem;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $elem 0 0 0
684 0 ▶ my $k = defined $comparer ? $comparer->($elem) : $elem;
Branch: 1 branch
BranchTrueFalseTotal
defined $comparer ? : 0 0 0
685 0 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
686 0 ▶ next if $in2{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $in2{$k} 0 0 0
687 0 ▶ next if $seen{$k};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$k} 0 0 0
688 0 $seen{$k} = 1;
689 0 return $elem;
690 }
691 0 };
692 0 return ref($self)->new($iter);
693 }
694  
695 ###############################################################################
696 # Join operations
697 ###############################################################################
698  
699 5 ▶sub Join {
Subroutine Join: called CC=4 SCAR=13.9
Pod documented
700 5 my($self, $inner, $outer_key, $inner_key, $result_sel) = @_;
701 5 my %lookup = ();
702 5 ▶ while (defined(my $e = $inner->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $inner->_next)) 10 5 15
703 10 my $k = $inner_key->($e);
704 10 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 10 10
705 10 ▶ $lookup{$k} = [] unless exists $lookup{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lookup{$k} 10 0 10
706 10 push @{ $lookup{$k} }, $e;
707 }
708 5 my $source = $self;
709 5 my @buf = ();
710 17 ▶ my $iter = sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
711 17 while (1) {
712 29 ▶ if (@buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (@buf) 12 17 29
713 12 return shift @buf;
714 }
715 17 my $outer = $source->_next();
716 17 ▶ return undef unless defined $outer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outer 5 12 17
717 12 my $k = $outer_key->($outer);
718 12 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 12 12
719 12 ▶ next unless exists $lookup{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lookup{$k} 0 12 12
720 12 for my $inner_elem (@{ $lookup{$k} }) {
721 12 push @buf, $result_sel->($outer, $inner_elem);
722 }
723 }
724 5 };
725 5 return ref($self)->new($iter);
726 }
727  
728 0 ▶sub GroupJoin {
Subroutine GroupJoin: not called CC=4 SCAR=30.0
Pod documented
729 0 my($self, $inner, $outer_key, $inner_key, $result_sel) = @_;
730 0 my %lookup = ();
731 0 ▶ while (defined(my $e = $inner->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $inner->_next)) 0 0 0
732 0 my $k = $inner_key->($e);
733 0 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
734 0 ▶ $lookup{$k} = [] unless exists $lookup{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $lookup{$k} 0 0 0
735 0 push @{ $lookup{$k} }, $e;
736 }
737 0 my $source = $self;
738 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
739 0 my $outer = $source->_next();
740 0 ▶ return undef unless defined $outer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $outer 0 0 0
741 0 my $k = $outer_key->($outer);
742 0 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 0 0
743 0 ▶ my $group = exists $lookup{$k} ? $lookup{$k} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $lookup{$k} ? : 0 0 0
744 0 my $inner_query = ref($self)->From([ @{$group} ]);
745 0 return $result_sel->($outer, $inner_query);
746 0 };
747 0 return ref($self)->new($iter);
748 }
749  
750 ###############################################################################
751 # Quantifier methods
752 ###############################################################################
753  
754 2 ▶sub All {
Subroutine All: called CC=3 SCAR=11.0
Pod documented
755 2 my($self, $pred) = @_;
756 2 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 6 1 7
757 6 ▶ return 0 unless $pred->($e);
Branch: 1 branch
BranchTrueFalseTotal
unless &$pred($e) 1 5 6
758 }
759 1 return 1;
760 }
761  
762 1 ▶sub Any {
Subroutine Any: called CC=5 SCAR=19.4
Pod documented
763 1 my($self, $pred) = @_;
764 1 ▶ if (defined $pred) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pred) { } 1 0 1
765 1 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 5 0 5
766 5 ▶ return 1 if $pred->($e);
Branch: 1 branch
BranchTrueFalseTotal
if &$pred($e) 1 4 5
767 }
768 0 return 0;
769 }
770 else {
771 0 ▶ return defined($self->_next()) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_next ? : 0 0 0
772 }
773 }
774  
775 0 ▶sub Contains {
Subroutine Contains: not called CC=8 SCAR=42.8
Pod documented
776 0 my($self, $value, $comparer) = @_;
777 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
778 0 ▶ if (defined $comparer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comparer) { } 0 0 0
779 0 ▶ return 1 if $comparer->($e, $value);
Branch: 1 branch
BranchTrueFalseTotal
if &$comparer($e, $value) 0 0 0
780 }
781 else {
782 0 ▶ if (!defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
elsif (defined $e and $e eq $value) { } 0 0 0
783 0 ▶ return 1 unless defined $e;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 0 0
784 }
785 ▶ elsif (defined $e && $e eq $value) {
Condition0%0/3
defined $e and $e eq $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e and $e eq $value
defined $e $e eq $value
Truth table0%0/3
A and B
A= defined $eB= $e eq $value
ABresult
0X0
100
111
786 0 return 1;
787 }
788 }
789 }
790 0 return 0;
791 }
792  
793 0 ▶sub SequenceEqual {
Subroutine SequenceEqual: not called CC=8 SCAR=42.8
Pod documented
794 0 my($self, $second, $comparer) = @_;
795 0 while (1) {
796 0 my $e1 = $self->_next();
797 0 my $e2 = $second->_next();
798 0 ▶ if (!defined $e1 && !defined $e2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $e1 or defined $e2) 0 0 0
Condition0%0/3
defined $e1 or defined $e2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $e1 or defined $e2
defined $e1 defined $e2
Truth table0%0/3
A or B
A= defined $e1B= defined $e2
ABresult
1X1
011
000
799 0 return 1;
800 }
801 0 ▶ return 0 if !defined $e1 || !defined $e2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e1 and defined $e2 0 0 0
Condition0%0/3
defined $e1 and defined $e2
!ll&&!rl&&r
000
MC/DC0%0/2
defined $e1 and defined $e2
defined $e1 defined $e2
Truth table0%0/3
A and B
A= defined $e1B= defined $e2
ABresult
0X0
100
111
802 0 ▶ if (defined $comparer) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comparer) { } 0 0 0
803 0 ▶ return 0 unless $comparer->($e1, $e2);
Branch: 1 branch
BranchTrueFalseTotal
unless &$comparer($e1, $e2) 0 0 0
804 }
805 else {
806 0 ▶ return 0 unless $e1 eq $e2;
Branch: 1 branch
BranchTrueFalseTotal
unless $e1 eq $e2 0 0 0
807 }
808 }
809 }
810  
811 ###############################################################################
812 # Element access methods
813 ###############################################################################
814  
815 3 ▶sub First {
Subroutine First: called CC=5 SCAR=22.5
Pod documented
816 3 my($self, $pred) = @_;
817 3 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 3 0 3
818 3 ▶ if (!defined $pred || $pred->($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pred or &$pred($e)) 3 0 3
Condition33%1/3
not defined $pred or &$pred($e)
l!l&&r!l&&!r
300
MC/DC0%0/2
not defined $pred or &$pred($e)
not defined $pred &$pred($e)
Truth table33%1/3
not(A) or B
A= not defined $predB= &$pred($e)
ABresult
1X1
011
000
819 3 return $e;
820 }
821 }
822 0 ▶ if (defined $pred) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pred) 0 0 0
823 0 die "No element satisfies the condition\n";
824 }
825 0 die "Sequence contains no elements\n";
826 }
827  
828 0 ▶sub FirstOrDefault {
Subroutine FirstOrDefault: not called CC=5 SCAR=34.0
Pod documented
829 0 my($self, $pred, $default) = @_;
830 0 ▶ if (ref($pred) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pred eq 'CODE') { } 0 0 0
831 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
832 0 ▶ return $e if $pred->($e);
Branch: 1 branch
BranchTrueFalseTotal
if &$pred($e) 0 0 0
833 }
834 0 return $default;
835 }
836 else {
837 0 $default = $pred;
838 0 my $e = $self->_next();
839 0 ▶ return defined $e ? $e : $default;
Branch: 1 branch
BranchTrueFalseTotal
defined $e ? : 0 0 0
840 }
841 }
842  
843 3 ▶sub Last {
Subroutine Last: called CC=6 SCAR=21.0
Pod documented
844 3 my($self, $pred) = @_;
845 3 my $found;
846 3 my $has = 0;
847 3 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 20 3 23
848 20 ▶ if (!defined $pred || $pred->($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pred or &$pred($e)) 20 0 20
Condition33%1/3
not defined $pred or &$pred($e)
l!l&&r!l&&!r
2000
MC/DC0%0/2
not defined $pred or &$pred($e)
not defined $pred &$pred($e)
Truth table33%1/3
not(A) or B
A= not defined $predB= &$pred($e)
ABresult
1X1
011
000
849 20 $found = $e;
850 20 $has = 1;
851 }
852 }
853 3 ▶ if ($has) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has) 3 0 3
854 3 return $found;
855 }
856 0 ▶ if (defined $pred) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pred) 0 0 0
857 0 die "No element satisfies the condition\n";
858 }
859 0 die "Sequence contains no elements\n";
860 }
861  
862 0 ▶sub LastOrDefault {
Subroutine LastOrDefault: not called CC=5 SCAR=34.0
Pod documented
863 0 my($self, $pred) = @_;
864 0 my $found;
865 0 my $has = 0;
866 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
867 0 ▶ if (!defined $pred || $pred->($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pred or &$pred($e)) 0 0 0
Condition0%0/3
not defined $pred or &$pred($e)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $pred or &$pred($e)
not defined $pred &$pred($e)
Truth table0%0/3
not(A) or B
A= not defined $predB= &$pred($e)
ABresult
1X1
011
000
868 0 $found = $e;
869 0 $has = 1;
870 }
871 }
872 0 ▶ return $has ? $found : undef;
Branch: 1 branch
BranchTrueFalseTotal
$has ? : 0 0 0
873 }
874  
875 0 ▶sub Single {
Subroutine Single: not called CC=6 SCAR=37.4
Pod documented
876 0 my($self, $pred) = @_;
877 0 my $found;
878 0 my $count = 0;
879 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
880 0 ▶ if (!defined $pred || $pred->($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pred or &$pred($e)) 0 0 0
Condition0%0/3
not defined $pred or &$pred($e)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $pred or &$pred($e)
not defined $pred &$pred($e)
Truth table0%0/3
not(A) or B
A= not defined $predB= &$pred($e)
ABresult
1X1
011
000
881 0 $found = $e;
882 0 $count++;
883 0 ▶ die "Sequence contains more than one element\n"
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 0 0 0
884 if $count > 1;
885 }
886 }
887 0 ▶ die "Sequence contains no elements\n" if $count == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $count == 0 0 0 0
888 0 return $found;
889 }
890  
891 0 ▶sub SingleOrDefault {
Subroutine SingleOrDefault: not called CC=6 SCAR=37.4
Pod documented
892 0 my($self, $pred) = @_;
893 0 my $found;
894 0 my $count = 0;
895 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
896 0 ▶ if (!defined $pred || $pred->($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pred or &$pred($e)) 0 0 0
Condition0%0/3
not defined $pred or &$pred($e)
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $pred or &$pred($e)
not defined $pred &$pred($e)
Truth table0%0/3
not(A) or B
A= not defined $predB= &$pred($e)
ABresult
1X1
011
000
897 0 $found = $e;
898 0 $count++;
899 0 ▶ return undef if $count > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $count > 1 0 0 0
900 }
901 }
902 0 ▶ return $count == 1 ? $found : undef;
Branch: 1 branch
BranchTrueFalseTotal
$count == 1 ? : 0 0 0
903 }
904  
905 0 ▶sub ElementAt {
Subroutine ElementAt: not called CC=4 SCAR=30.0
Pod documented
906 0 my($self, $index) = @_;
907 0 ▶ die "ElementAt: index out of range\n" if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
908 0 my $i = 0;
909 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
910 0 ▶ return $e if $i == $index;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $index 0 0 0
911 0 $i++;
912 }
913 0 die "ElementAt: index out of range\n";
914 }
915  
916 0 ▶sub ElementAtOrDefault {
Subroutine ElementAtOrDefault: not called CC=4 SCAR=30.0
Pod documented
917 0 my($self, $index) = @_;
918 0 ▶ return undef if $index < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 0 0 0
919 0 my $i = 0;
920 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
921 0 ▶ return $e if $i == $index;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $index 0 0 0
922 0 $i++;
923 }
924 0 return undef;
925 }
926  
927 ###############################################################################
928 # Aggregation methods
929 ###############################################################################
930  
931 3 ▶sub Count {
Subroutine Count: called CC=4 SCAR=14.3
Pod documented
932 3 my($self, $pred) = @_;
933 3 my $n = 0;
934 3 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 19 3 22
935 19 ▶ if (!defined $pred || $pred->($e)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $pred or &$pred($e)) 19 0 19
Condition33%1/3
not defined $pred or &$pred($e)
l!l&&r!l&&!r
1900
MC/DC0%0/2
not defined $pred or &$pred($e)
not defined $pred &$pred($e)
Truth table33%1/3
not(A) or B
A= not defined $predB= &$pred($e)
ABresult
1X1
011
000
936 19 $n++;
937 }
938 }
939 3 return $n;
940 }
941  
942 1 ▶sub Sum {
Subroutine Sum: called CC=4 SCAR=14.0
Pod documented
943 1 my($self, $selector) = @_;
944 1 my $total = 0;
945 1 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 10 1 11
946 10 ▶ my $v = defined $selector ? $selector->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 10 0 10
947 10 ▶ $total += (defined $v ? $v : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 10 0 10
948 }
949 1 return $total;
950 }
951  
952 0 ▶sub Min {
Subroutine Min: not called CC=6 SCAR=37.4
Pod documented
953 0 my($self, $selector) = @_;
954 0 my $min;
955 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
956 0 ▶ my $v = defined $selector ? $selector->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 0 0 0
957 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
958 0 ▶ $min = $v if !defined $min || $v < $min;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $v < $min 0 0 0
Condition0%0/3
not defined $min or $v < $min
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $min or $v < $min
not defined $min $v < $min
Truth table0%0/3
not(A) or B
A= not defined $minB= $v < $min
ABresult
1X1
011
000
959 }
960 0 return $min;
961 }
962  
963 0 ▶sub Max {
Subroutine Max: not called CC=6 SCAR=37.4
Pod documented
964 0 my($self, $selector) = @_;
965 0 my $max;
966 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
967 0 ▶ my $v = defined $selector ? $selector->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 0 0 0
968 0 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
969 0 ▶ $max = $v if !defined $max || $v > $max;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $v > $max 0 0 0
Condition0%0/3
not defined $max or $v > $max
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $max or $v > $max
not defined $max $v > $max
Truth table0%0/3
not(A) or B
A= not defined $maxB= $v > $max
ABresult
1X1
011
000
970 }
971 0 return $max;
972 }
973  
974 1 ▶sub Average {
Subroutine Average: called CC=5 SCAR=16.4
Pod documented
975 1 my($self, $selector) = @_;
976 1 my $sum = 0;
977 1 my $n = 0;
978 1 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 5 1 6
979 5 ▶ my $v = defined $selector ? $selector->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 5 0 5
980 5 ▶ $sum += (defined $v ? $v : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 5 0 5
981 5 $n++;
982 }
983 1 ▶ die "Sequence contains no elements\n" if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 1 1
984 1 return $sum / $n;
985 }
986  
987 0 ▶sub AverageOrDefault {
Subroutine AverageOrDefault: not called CC=5 SCAR=34.0
Pod documented
988 0 my($self, $selector) = @_;
989 0 my $sum = 0;
990 0 my $n = 0;
991 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
992 0 ▶ my $v = defined $selector ? $selector->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $selector ? : 0 0 0
993 0 ▶ $sum += (defined $v ? $v : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
994 0 $n++;
995 }
996 0 ▶ return $n == 0 ? undef : $sum / $n;
Branch: 1 branch
BranchTrueFalseTotal
$n == 0 ? : 0 0 0
997 }
998  
999 0 ▶sub Aggregate {
Subroutine Aggregate: not called CC=6 SCAR=37.4
Pod documented
1000 0 my($self, @args) = @_;
1001 0 my($seed, $func, $result_sel);
1002 0 ▶ if (@args == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1) { } 0 0 0
elsif (@args == 2) { } 0 0 0
1003 0 $func = $args[0];
1004 0 my $first = $self->_next();
1005 0 ▶ return undef unless defined $first;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first 0 0 0
1006 0 $seed = $first;
1007 }
1008 elsif (@args == 2) {
1009 0 ($seed, $func) = @args;
1010 }
1011 else {
1012 0 ($seed, $func, $result_sel) = @args;
1013 }
1014 0 my $acc = $seed;
1015 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
1016 0 $acc = $func->($acc, $e);
1017 }
1018 0 ▶ return defined $result_sel ? $result_sel->($acc) : $acc;
Branch: 1 branch
BranchTrueFalseTotal
defined $result_sel ? : 0 0 0
1019 }
1020  
1021 ###############################################################################
1022 # Conversion methods
1023 ###############################################################################
1024  
1025 101 ▶sub ToArray {
Subroutine ToArray: called CC=2 SCAR=6.9
Pod documented
1026 101 my($self) = @_;
1027 101 my @result = ();
1028 101 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 363 101 464
1029 363 push @result, $e;
1030 }
1031 101 return @result;
1032 }
1033  
1034 0 ▶sub ToList {
Subroutine ToList: not called CC=2 SCAR=17.9
Pod documented
1035 0 my($self) = @_;
1036 0 my @result = ();
1037 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
1038 0 push @result, $e;
1039 }
1040 0 return [ @result ];
1041 }
1042  
1043 0 ▶sub DefaultIfEmpty {
Subroutine DefaultIfEmpty: not called CC=1 SCAR=6.9
Pod documented
1044 0 my($self, $default) = @_;
1045 0 my $source = $self;
1046 0 my $started = 0;
1047 0 my $done = 0;
1048 0 ▶ my $iter = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
1049 0 ▶ if (!$started) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($started) 0 0 0
1050 0 my $elem = $source->_next();
1051 0 $started = 1;
1052 0 ▶ unless (defined $elem) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $elem) 0 0 0
1053 0 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 0 0
1054 0 $done = 1;
1055 0 return $default;
1056 }
1057 0 return undef;
1058 }
1059 0 return $elem;
1060 }
1061 0 ▶ return undef if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
1062 0 return $source->_next();
1063 0 };
1064 0 return ref($self)->new($iter);
1065 }
1066  
1067 2 ▶sub ToDictionary {
Subroutine ToDictionary: called CC=4 SCAR=14.0
Pod documented
1068 2 my($self, $key_sel, $val_sel) = @_;
1069 2 my %dict = ();
1070 2 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 4 2 6
1071 4 my $k = $key_sel->($e);
1072 4 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 4 4
1073 4 ▶ my $v = defined $val_sel ? $val_sel->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $val_sel ? : 4 0 4
1074 4 $dict{$k} = $v;
1075 }
1076 2 return { %dict };
1077 }
1078  
1079 2 ▶sub ToLookup {
Subroutine ToLookup: called CC=5 SCAR=16.1
Pod documented
1080 2 my($self, $key_sel, $val_sel) = @_;
1081 2 my %lookup = ();
1082 2 my @keys = ();
1083 2 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 6 2 8
1084 6 my $k = $key_sel->($e);
1085 6 ▶ $k = '' unless defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 0 6 6
1086 6 ▶ unless (exists $lookup{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $lookup{$k}) 4 2 6
1087 4 push @keys, $k;
1088 4 $lookup{$k} = [];
1089 }
1090 6 ▶ my $v = defined $val_sel ? $val_sel->($e) : $e;
Branch: 1 branch
BranchTrueFalseTotal
defined $val_sel ? : 6 0 6
1091 6 push @{ $lookup{$k} }, $v;
1092 }
1093 2 return { %lookup };
1094 }
1095  
1096 8 ▶sub ToCSV {
Subroutine ToCSV: called CC=17 SCAR=33.5
Pod documented
1097 8 my($self, $file, %opts) = @_;
1098 8 ▶ my $sep = defined $opts{sep} ? $opts{sep} : ',';
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'sep'} ? : 0 8 8
1099 my $headers = defined $opts{headers} ? $opts{headers}
1100 : defined $opts{label_order} ? $opts{label_order}
1101 8 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
defined $opts{'headers'} ? : 4 4 8
defined $opts{'label_order'} ? : 2 2 4
1102 8 my $no_header = $opts{no_header};
1103 8 my @items = ();
1104 8 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 18 8 26
1105 18 push @items, $e;
1106 }
1107 8 my $fhn = _open_fh('>', $file, 0);
1108 8 ▶ unless ($no_header) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($no_header) 8 0 8
1109 8 my @cols = ();
1110 8 ▶ if (defined $headers) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $headers) { } 6 2 8
elsif (@items and ref $items[0] eq 'HASH') { } 2 0 2
1111 6 @cols = @{$headers};
1112 }
1113 ▶ elsif (@items && ref($items[0]) eq 'HASH') {
Condition33%1/3
@items and ref $items[0] eq "HASH"
!ll&&!rl&&r
002
MC/DC0%0/2
@items and ref $items[0] eq "HASH"
@items ref $items[0] eq "HASH"
Truth table33%1/3
A and B
A= @itemsB= ref $items[0] eq "HASH"
ABresult
0X0
100
111
1114 2 @cols = sort keys %{ $items[0] };
1115 }
1116 8 ▶ if (@cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cols) 8 0 8
1117 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1118 8 print {*{$fhn}}
1119 8 join($sep, map { _format_csv_field($_, $sep) } @cols) . "\n";
1120 }
1121 8 ▶ if (!@cols && @items && ref($items[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (not @cols and @items and ref $items[0] ne 'HASH') { } 0 8 8
Condition33%2/6
not @cols and @items and ref $items[0] ne "HASH"
!ll&&!rl&&r
800
not @cols and @items
!ll&&!rl&&r
800
MC/DC0%0/3
not @cols and @items and ref $items[0] ne "HASH"
not @cols @items ref $items[0] ne "HASH"
Truth table25%1/4
not(A) and B and C
A= not @colsB= @itemsC= ref $items[0] ne "HASH"
ABCresult
0XX0
10X0
1100
1111
1122 # scalar sequence - no header
1123 }
1124 else {
1125 8 for my $item (@items) {
1126 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1127 18 ▶ if (ref($item) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 18 0 18
1128 18 print {*{$fhn}}
1129 join($sep, map {
1130 18 _format_csv_field($item->{$_}, $sep)
1131 } @cols) . "\n";
1132 }
1133 else {
1134 0 print {*{$fhn}}
1135 _format_csv_field($item, $sep) . "\n";
1136 }
1137 }
1138 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1139 8 close(*{$fhn});
1140 8 return 1;
1141 }
1142 }
1143 0 for my $item (@items) {
1144 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1145 0 ▶ if (ref($item) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $item eq 'HASH') { } 0 0 0
1146 0 my @cols = sort keys %{$item};
1147 0 print {*{$fhn}}
1148 join($sep, map {
1149 0 _format_csv_field($item->{$_}, $sep)
1150 } @cols) . "\n";
1151 }
1152 else {
1153 0 print {*{$fhn}}
1154 _format_csv_field($item, $sep) . "\n";
1155 }
1156 }
1157 {
1158 0 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
1159 0 close(*{$fhn});
1160 }
1161 0 return 1;
1162 }
1163  
1164 ###############################################################################
1165 # Utility methods
1166 ###############################################################################
1167  
1168 0 ▶sub ForEach {
Subroutine ForEach: not called CC=2 SCAR=17.9
Pod documented
1169 0 my($self, $action) = @_;
1170 0 ▶ while (defined(my $e = $self->_next())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $e = $self->_next)) 0 0 0
1171 0 $action->($e);
1172 }
1173 0 return;
1174 }
1175  
1176 5 1;
1177  
1178 __END__
↑ Summary