TableReader/Decoder/CSV.pm

filter: Statementstmt 77.1% 88 / 114 Branchbran 53.1% 34 / 64 Conditioncond 54.0% 20 / 37 MC/DCmcdc 30.0% 6 / 20 Subroutinesub 83.3% 15 / 18 Podpod 50.0% 2 / 4 totaltotal 64.2% 165 / 257 CC 47 SCAR 27.5
CC
47
Cov
66%
progress
56.0
encoding
24.0
_autodetect_bom
14.4
CRAP 15.6
« TableReader/Decoder/XLSX.pm ↑ Summary TableReader/Decoder/Spreadsheet.pm »
1 package Data::TableReader::Decoder::CSV;
2 4 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 extends 'Data::TableReader::Decoder';
6  
7 # ABSTRACT: Access rows of a comma-delimited text file
8 4 our $VERSION = '0.023'; # VERSION
9  
10 4 our @csv_probe_modules= ( ['Text::CSV_XS' => 1.06], ['Text::CSV' => 1.91] );
11 4 our $default_csv_module;
12 17 ▶sub default_csv_module {
Subroutine default_csv_module: called CC=2 SCAR=7.2
Pod undocumented
13 17 ▶ $default_csv_module ||=
Condition66%2/3
$Data::TableReader::Decoder::CSV::default_csv_module ||= Data::TableReader::Decoder::_first_sufficient_module("CSV parser", \@Data::TableReader::Decoder::CSV::csv_probe_modules)
l!l&&r!l&&!r
1430
MC/DC0%0/2
$Data::TableReader::Decoder::CSV::default_csv_module ||= Data::TableReader::Decoder::_first_sufficient_module("CSV parser", \@Data::TableReader::Decoder::CSV::csv_probe_modules)
$Data::TableReader::Decoder::CSV::default_csv_module Data::TableReader::Decoder::_first_sufficient_module("CSV parser", \@Data::TableReader::Decoder::CSV::csv_probe_modules)
Truth table66%2/3
A ||= B
A= $Data::TableReader::Decoder::CSV::default_csv_moduleB= Data::TableReader::Decoder::_first_sufficient_module("CSV parser", \@Data::TableReader::Decoder::CSV::csv_probe_modules)
ABresult
1X1
011
000
14 Data::TableReader::Decoder::_first_sufficient_module('CSV parser', \@csv_probe_modules);
15 }
16  
17  
18 4 has _parser_args => ( is => 'ro', init_arg => 'parser' );
19  
20 4 has parser => ( is => 'lazy', init_arg => undef );
21 12 ▶sub _build_parser {
Subroutine _build_parser: called CC=3 SCAR=11.4
22 12 my $self= shift;
23 12 ▶ my $args= $self->_parser_args || {};
Condition50%1/2
$self->_parser_args || {}
l!l
012
MC/DC0%0/1
$self->_parser_args || {}
$self->_parser_args
Truth table50%1/2
A
A= $self->_parser_args
Aresult
11
00
24 12 ▶ return $args if ref($args)->can('getline');
Branch: 1 branch
BranchTrueFalseTotal
if ref($args)->can("getline") 0 12 12
25 12 return $self->default_csv_module->new({
26 binary => 1,
27 allow_loose_quotes => 1,
28 auto_diag => 2,
29 %$args
30 });
31 }
32  
33  
34 16 ▶has autodetect_encoding => ( is => 'rw', default => sub { 1 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 16 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.2
Pod undocumented
37 16 my ($self, $args)= @_;
38 16 ▶ $self->encoding($args->{encoding}) if defined $args->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"encoding"} 0 16 16
39 }
40  
41 14 ▶sub encoding {
Subroutine encoding: called CC=11 SCAR=31.8
Pod documented
42 14 my ($self, $enc)= @_;
43 14 my $fh= $self->file_handle;
44 14 ▶ if (defined $enc) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $enc) 0 14 14
45 # protect against binmode string injection
46 0 ▶ croak "invalid/unknown encoding '$enc'" unless Encode::find_encoding($enc);
Branch: 1 branch
BranchTrueFalseTotal
unless &Encode::find_encoding($enc) 0 0 0
47 0 binmode($fh, ":encoding($enc)");
48 0 return $enc;
49 }
50
51 14 my @layers= PerlIO::get_layers($fh);
52 14 ▶ if (($enc)= grep { /^encoding|^utf/i } @layers) {
Branch: 1 branch
BranchTrueFalseTotal
if (($enc) = grep({/^encoding|^utf/i;} @layers)) 0 14 14
53 # extract encoding name
54 0 ▶ return 'UTF-8' if $enc eq 'utf8';
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "utf8" 0 0 0
55 0 ▶ return uc($1) if $enc =~ /encoding\(([^)]+)\)/;
Branch: 1 branch
BranchTrueFalseTotal
if $enc =~ /encoding\(([^)]+)\)/ 0 0 0
56 0 return uc($enc); # could throw a parse error, but this is probably more useful behavior
57 }
58
59 # fh_start_pos will be set if we have already checked for BOM
60 14 ▶ if ($self->autodetect_encoding && !defined $self->_fh_start_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->autodetect_encoding and not defined $self->_fh_start_pos) 14 0 14
Condition33%1/3
$self->autodetect_encoding and not defined $self->_fh_start_pos
!ll&&!rl&&r
0014
MC/DC0%0/2
$self->autodetect_encoding and not defined $self->_fh_start_pos
$self->autodetect_encoding not defined $self->_fh_start_pos
Truth table33%1/3
A and not(B)
A= $self->autodetect_encodingB= not defined $self->_fh_start_pos
ABresult
0X0
100
111
61 14 ▶ $self->_fh_start_pos(tell $fh or 0);
Condition50%1/2
tell $fh || 0
l!l
014
MC/DC0%0/1
tell $fh || 0
tell $fh
Truth table50%1/2
A
A= tell $fh
Aresult
11
00
62 14 ▶ if (($enc= $self->_autodetect_bom($fh))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc = $self->_autodetect_bom($fh)) 6 7 13
63 6 binmode($fh, ":encoding($enc)");
64 # re-mark the start after the BOM
65 6 ▶ $self->_fh_start_pos(tell $fh or 0);
Condition50%1/2
tell $fh || 0
l!l
60
MC/DC0%0/1
tell $fh || 0
tell $fh
Truth table50%1/2
A
A= tell $fh
Aresult
11
00
66 6 return $enc;
67 }
68 }
69 7 return '';
70 }
71  
72  
73 4 has _fh_start_pos => ( is => 'rw' );
74 4 has _iterator => ( is => 'rw', weak_ref => 1 );
75 4 has _row_ref => ( is => 'rw' );
76 16 ▶sub iterator {
Subroutine iterator: called CC=7 SCAR=19.7
Pod documented
77 16 my $self= shift;
78 16 ▶ croak "Multiple iterators on CSV stream not supported yet" if $self->_iterator;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_iterator 1 15 16
79 15 my $parser= $self->parser;
80 15 my $fh= $self->file_handle;
81 15 my $row_ref= $self->_row_ref;
82 # Keeping this object is just an indication of whether an iterator has been used yet
83 15 ▶ if (!$row_ref) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $row_ref) { } 14 1 15
elsif ($$row_ref) { } 0 1 1
84 14 $self->_row_ref($row_ref= \(my $row= 0));
85 # trigger BOM detection if needed
86 14 my $enc= $self->encoding;
87 13 ▶ $self->_log->('debug', "encoding is ".($enc||'maybe utf8'));
Condition100%2/2
$enc || 'maybe utf8'
l!l
67
MC/DC100%1/1
$enc || 'maybe utf8'
$enc
Truth table100%2/2
A
A= $enc
Aresult
11
00
88 # ensure _fh_start_pos is set
89 13 ▶ $self->_fh_start_pos(tell $fh or 0);
Condition100%2/2
tell $fh || 0
l!l
67
MC/DC100%1/1
tell $fh || 0
tell $fh
Truth table100%2/2
A
A= tell $fh
Aresult
11
00
90 }
91 elsif ($$row_ref) {
92 0 $self->_log->('debug', 'Seeking back to start of input');
93 0 ▶ seek($fh, $self->_fh_start_pos, 0)
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, $self->_fh_start_pos, 0 or die "Can't seek back to start of stream" 0 0 0
94 or die "Can't seek back to start of stream";
95 0 $$row_ref= 0;
96 }
97 my $i= Data::TableReader::Decoder::CSV::_Iter->new(
98 60 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
99 60 ▶ my $r= $parser->getline($fh) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $parser->getline($fh) or return undef 48 12 60
100 48 ++$$row_ref;
101 48 ▶ @$r= @{$r}[ @{$_[0]} ] if $_[0]; # optional slice argument
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 7 41 48
102 48 return $r;
103 },
104 {
105 14 row => $row_ref,
106 fh => $fh,
107 origin => $self->_fh_start_pos,
108 }
109 );
110 14 $self->_iterator($i);
111 14 return $i;
112 }
113  
114 # This design is simplified from File::BOM in that it ignores UTF-32
115 # and in any "normal" case it can read from a pipe with only one
116 # character to push back, avoiding the need to tie the file handle.
117 # It also checks for whether layers have already been enabled.
118 # It also avoids seeking to the start of the file handle, in case
119 # the user deliberately seeked to a position.
120 14 ▶sub _autodetect_bom {
Subroutine _autodetect_bom: called CC=14 SCAR=26.7
121 14 my ($self, $fh)= @_;
122 14 my $fpos= tell($fh);
123
124 14 local $!;
125 14 ▶ read($fh, my $buf, 1) || return;
Branch: 1 branch
BranchTrueFalseTotal
read $fh, my $buf, 1 or return 14 0 14
126 14 ▶ if ($buf eq "\xFF" || $buf eq "\xFE" || $buf eq "\xEF") {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf eq "\377" or $buf eq "\376" or $buf eq "\357") 8 6 14
Condition100%6/6
$buf eq "\377" or $buf eq "\376" or $buf eq "\357"
l!l&&r!l&&!r
446
$buf eq "\377" or $buf eq "\376"
l!l&&r!l&&!r
2210
MC/DC100%3/3
$buf eq "\377" or $buf eq "\376" or $buf eq "\357"
$buf eq "\377" $buf eq "\376" $buf eq "\357"
Truth table100%4/4
A or B or C
A= $buf eq "\377"B= $buf eq "\376"C= $buf eq "\357"
ABCresult
1XX1
01X1
0011
0000
127 8 ▶ if (read($fh, $buf, 1, 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, $buf, 1, 1) 8 0 8
128 8 ▶ if ($buf eq "\xFF\xFE") {
Branch: 3 branches
BranchTrueFalseTotal
if ($buf eq "\377\376") { } 2 6 8
elsif ($buf eq "\376\377") { } 2 4 6
elsif ($buf eq "\357\273" and read $fh, $buf, 1, 2 and $buf eq "\357\273\277") { } 2 4 6
129 2 return 'UTF-16LE';
130 } elsif ($buf eq "\xFE\xFF") {
131 2 return 'UTF-16BE';
132 ▶ } elsif ($buf eq "\xEF\xBB" and read($fh, $buf, 1, 2) and $buf eq "\xEF\xBB\xBF") {
Condition66%4/6
$buf eq "\357\273" and read $fh, $buf, 1, 2 and $buf eq "\357\273\277"
!ll&&!rl&&r
202
$buf eq "\357\273" and read $fh, $buf, 1, 2
!ll&&!rl&&r
202
MC/DC33%1/3
$buf eq "\357\273" and read $fh, $buf, 1, 2 and $buf eq "\357\273\277"
$buf eq "\357\273" read $fh, $buf, 1, 2 $buf eq "\357\273\277"
Truth table50%2/4
A and B and C
A= $buf eq "\357\273"B= read $fh, $buf, 1, 2C= $buf eq "\357\273\277"
ABCresult
0XX0
10X0
1100
1111
133 2 return 'UTF-8';
134 }
135 }
136 }
137
138 # It wasn't a BOM. Try to undo our read.
139 8 $self->_log->('debug', 'No BOM in stream, seeking back to start');
140 8 ▶ if (length $buf == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $buf == 1) { } 6 2 8
elsif (not seek($fh, $fpos, 0)) { } 1 1 2
141 6 $fh->ungetc(ord $buf);
142 } elsif (!seek($fh, $fpos, 0)) {
143 # Can't seek
144 1 ▶ if ($fh->can('ungets')) { # support for FileHandle::Unget
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->can('ungets')) { } 0 1 1
145 0 $fh->ungets($buf);
146 } else {
147 1 croak "Can't seek input handle after BOM detection; You should set an encoding manually, buffer the entire input, or use FileHandle::Unget";
148 }
149 }
150 7 return;
151 }
152  
153 # If you need to subclass this iterator, don't. Just implement your own.
154 # i.e. I'm not declaring this implementation stable, yet.
155 4 ▶use Data::TableReader::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 4 ▶BEGIN { @Data::TableReader::Decoder::CSV::_Iter::ISA= ('Data::TableReader::Iterator'); }
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 0 ▶sub Data::TableReader::Decoder::CSV::_Iter::position {
Subroutine position: not called CC=1 SCAR=6.9
159 0 my $f= shift->_fields;
160 0 'row '.${ $f->{row} };
161 }
162  
163 8 ▶sub Data::TableReader::Decoder::CSV::_Iter::row {
Subroutine row: called CC=1 SCAR=0.0
164 8 ${ shift->_fields->{row} }
165 }
166  
167 0 ▶sub Data::TableReader::Decoder::CSV::_Iter::progress {
Subroutine progress: not called CC=7 SCAR=40.3
168 0 my $f= shift->_fields;
169 # lazy-build the file size, using seek
170 0 ▶ unless (exists $f->{file_size}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $f->{"file_size"}) 0 0 0
171 0 my $pos= tell $f->{fh};
172 0 ▶ if (defined $pos and $pos >= 0 and seek($f->{fh}, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos and $pos >= 0 and seek $f->{'fh'}, 0, 2) { } 0 0 0
Condition0%0/6
defined $pos and $pos >= 0 and seek $f->{"fh"}, 0, 2
!ll&&!rl&&r
000
defined $pos and $pos >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $pos and $pos >= 0 and seek $f->{"fh"}, 0, 2
defined $pos $pos >= 0 seek $f->{"fh"}, 0, 2
Truth table0%0/4
A and B and C
A= defined $posB= $pos >= 0C= seek $f->{"fh"}, 0, 2
ABCresult
0XX0
10X0
1100
1111
173 0 $f->{file_size}= tell($f->{fh});
174 0 ▶ seek($f->{fh}, $pos, 0) or die "seek: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $f->{"fh"}, $pos, 0 or die "seek: $!" 0 0 0
175 } else {
176 0 $f->{file_size}= undef;
177 }
178 }
179 0 ▶ return $f->{file_size}? (tell $f->{fh})/$f->{file_size} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$f->{'file_size'} ? : 0 0 0
180 }
181  
182 0 ▶sub Data::TableReader::Decoder::CSV::_Iter::tell {
Subroutine tell: not called CC=3 SCAR=24.8
183 0 my $f= shift->_fields;
184 0 my $pos= tell($f->{fh});
185 0 ▶ return undef unless defined $pos && $pos >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pos and $pos >= 0 0 0 0
Condition0%0/3
defined $pos and $pos >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pos and $pos >= 0
defined $pos $pos >= 0
Truth table0%0/3
A and B
A= defined $posB= $pos >= 0
ABresult
0X0
100
111
186 0 return [ $pos, ${$f->{row}} ];
187 }
188  
189 7 ▶sub Data::TableReader::Decoder::CSV::_Iter::seek {
Subroutine seek: called CC=4 SCAR=14.5
190 7 my ($self, $to)= @_;
191 7 my $f= $self->_fields;
192 7 ▶ seek($f->{fh}, ($to? $to->[0] : $f->{origin}), 0) or croak("seek failed: $!");
Branch: 2 branches
BranchTrueFalseTotal
seek $f->{"fh"}, $to ? $to->[0] : $f->{"origin"}, 0 or croak("seek failed: $!") 7 0 7
$to ? : 0 7 7
193 7 ▶ ${ $f->{row} }= $to? $to->[1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$to ? : 0 7 7
194 7 1;
195 }
196  
197 4 1;
198  
199 __END__
« TableReader/Decoder/XLSX.pm ↑ Summary TableReader/Decoder/Spreadsheet.pm »