lib/CSV/Reader.pm

filter: Statementstmt 78.4% 138 / 176 Branchbran 63.0% 63 / 100 Conditioncond 48.2% 14 / 29 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 87.5% 14 / 16 Podpod 100% 7 / 7 totaltotal 69.0% 239 / 346 CC 67 SCAR 47.0
CC
67
Cov
70%
new
163.5
_read
10.9
__ANON__
6.0
CRAP 109.4
↑ Summary
1 package CSV::Reader;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Text::CSV ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Tie::IxHash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 our $VERSION = 1.12;
7  
8 =head1 NAME
9  
10 CSV::Reader - CSV reader class
11  
12 =head1 DESCRIPTION
13  
14 Simple CSV reader class that uses Text::CSV internally.
15 The CSV files are expected to have a header row of column names.
16 This was designed with the idea of using an iterator interface, but Perl does not support interators (nor interfaces) yet :(
17  
18 =head1 SYNOPSIS
19  
20 use CSV::Reader ();
21 use open OUT => ':locale'; # optional; make perl aware of your terminal's encoding
22  
23 # Create reader from file name:
24 my $reader = new CSV::Reader('/path/to/file.csv');
25  
26 # Create reader from a file handle (GLOB):
27 open(my $h, '<', $filename) || die("Failed to open $filename: $!");
28 # or preferred method that can handle files having a UTF-8 BOM:
29 open(my $h, '<:via(File::BOM)', $filename) || die("Failed to open $filename: $!");
30 my $reader = new CSV::Reader($h);
31  
32 # Create reader from an IO::Handle based object:
33 my $io = IO::File->new(); # subclass of IO::Handle
34 $io->open($filename, '<:via(File::BOM)') || die("Failed to open $filename: $!");
35 my $reader = new CSV::Reader($io);
36  
37 # Create reader with advanced options:
38 my $reader = new CSV::Reader('/path/to/file.csv',
39 'delimiter' => ';',
40 'enclosure' => '',
41 'field_normalizer' => sub {
42 my $nameref = shift;
43 $$nameref = lc($$nameref); # lowercase
44 $$nameref =~ s/\s/_/g; # whitespace to underscore
45 },
46 'field_aliases' => {
47 'postal_code' => 'postcode', # applied after normalization
48 },
49 'mutators' => {
50 'postcode' => sub { # if postalcode is Dutch, then make sure it has no spaces and is in uppercase.
51 my $val_ref = shift;
52 my $row_ref = shift;
53 if (defined($$val_ref) && defined($row_ref->{'country'}) && ($row_ref->{'country'} eq 'NL')) {
54 $$val_ref =~ s/\s+//;
55 $$val_ref = uc($$val_ref);
56 }
57 },
58 'has_fiber_internet' => sub { # set a default for an empty (undef) value
59 my $val_ref = shift;
60 $$val_ref //= 0;
61 },
62 },
63 );
64  
65 # Show the field names found in the header row:
66 print 'Field names: ' . join("\n", $reader->fieldNames()) . "\n";
67  
68 # Iterate over the data rows:
69 while (my $row = $reader->nextRow()) {
70 # It's recommended to validate the $row hashref first with something such as Params::Validate.
71 # Now do whatever you want with the (validated) row hashref...
72 require Data::Dumper; local $Data::Dumper::Terse = 1;
73 print Data::Dumper::Dumper($row);
74 }
75  
76 =head1 PUBLIC STATIC METHODS
77  
78 =head2 new($file, %options)
79  
80 Constructor.
81  
82 $file can be a string file name, an open file handle (GLOB), or an IO::Handle based object (e.g. IO::File or IO::Scalar).
83 If a string file name is given, then the file is opened via File::BOM.
84  
85 The following %options are supported:
86  
87 - debug: boolean, if true, then debug messages are emitted using warn().
88 - field_aliases: hashref of case insensitive alias (in file) => real name (as expected in code) pairs.
89 - field_normalizer: callback that receives a field name by reference to normalize (e.g. make lowercase).
90 - include_fields: arrayref of field names to include. If given, then all other field names are excluded.
91 - delimiter: string, default ','
92 - enclosure: string, default '"'
93 - escape: string, default backslash
94 - mutators: hashref of field name => callback($value_ref, $row_ref) pairs.
95  
96 Note: the option field_aliases is processed after the option field_normalizer if given.
97  
98 Note: the callbacks given with the mutators option are called in their key order (which is an unpredictable order unless they're tied with Tie::IxHash).
99  
100 =cut
101  
102 8 ▶sub new {
Subroutine new: called CC=54 SCAR=51.0
Pod documented
103 8 my $proto = shift;
104 8 my $file = shift;
105 8 my %options = @_;
106 8 my $self = {
107 'h' => undef, # File handle.
108 'own_h' => undef, # Does this class own the file handle.
109 'field_cols' => {}, # Hashref of fieldname => column index pairs.
110 'row' => undef, # Current ReaderRow object.
111 'linenum' => 0, # Data row index.
112 'text_csv' => undef, # The Text::CSV object
113  
114 # Options:
115 'debug' => 0,
116 'delimiter' => ',',
117 'enclosure' => '"',
118 'escape' => '\\',
119 'mutators' => undef,
120 'skip_empty_lines' => 0, # TODO: implement this
121 };
122 8 tie(%{$self->{'field_cols'}}, 'Tie::IxHash');
123  
124 8 ▶ unless (defined($file) && length($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file and length $file) 0 8 8
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
008
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
125 0 croak('Missing $file argument');
126 }
127 8 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 3 5 8
128 3 ▶ unless ((ref($file) eq 'GLOB') || UNIVERSAL::isa($file, 'IO::Handle')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")) 0 3 3
Condition66%2/3
ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")
l!l&&r!l&&!r
210
MC/DC0%0/2
ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")
ref $file eq "GLOB" UNIVERSAL::isa($file, "IO::Handle")
Truth table66%2/3
A or B
A= ref $file eq "GLOB"B= UNIVERSAL::isa($file, "IO::Handle")
ABresult
1X1
011
000
129 0 croak(ref($file) . ' is not a legal file argument type');
130 }
131 3 $self->{'h'} = $file;
132 3 $self->{'own_h'} = 0;
133 }
134 else {
135 5 my $h;
136 5 eval {
137 5 require File::BOM;
138 };
139 5 ▶ my $mode = $@ ? '<' : '<:via(File::BOM)';
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 5 5
140 5 ▶ $options{'debug'} && warn(__PACKAGE__ . "::new file open mode is $mode\n");
Branch: 1 branch
BranchTrueFalseTotal
$options{"debug"} and warn 'CSV::Reader' . "::new file open mode is $mode\n" 0 5 5
141 5 ▶ open($h, $mode, $file) || croak('Failed to open "' . $file . '" for reading using mode "' . $mode . '": ' . $!);
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
open $h, $mode, $file or croak('Failed to open "' . $file . '" for reading using mode "' . $mode . '": ' . $!) 5 0 5
142 5 $self->{'h'} = $h;
143 5 $self->{'own_h'} = 1;
144 }
145  
146 # Get the options.
147 8 my %opt_field_aliases;
148 my $opt_field_normalizer;
149 8 my %opt_include_fields;
150 8 my %text_csv_options; # undocumented experimental feature; text_csv_*: avoid if possible; options with this prefix are passed as is (but without prefix) to the internal Text::CSV object.
151 8 ▶ if (%options) {
Branch: 1 branch
BranchTrueFalseTotal
if (%options) 8 0 8
152 8 foreach my $key (keys %options) {
153 25 my $value = $options{$key};
154 25 ▶ if (($key eq 'debug') || ($key eq 'skip_empty_lines')) {
Branch: 8 branches
BranchTrueFalseTotal
if ($key eq 'debug' or $key eq 'skip_empty_lines') { } 0 25 25
elsif ($key eq 'enclosure' or $key eq 'escape') { } 7 18 25
elsif ($key eq 'delimiter') { } 7 11 18
elsif ($key eq 'include_fields') { } 1 10 11
elsif ($key eq 'field_aliases') { } 8 2 10
elsif ($key eq 'field_normalizer') { } 1 1 2
elsif ($key eq 'mutators') { } 1 0 1
elsif ($key =~ /^(?:Text(?::|_)CSV|text_csv)[\._:](.+)$/) { } 0 0 0
Condition33%1/3
$key eq "debug" or $key eq "skip_empty_lines"
l!l&&r!l&&!r
0025
MC/DC0%0/2
$key eq "debug" or $key eq "skip_empty_lines"
$key eq "debug" $key eq "skip_empty_lines"
Truth table33%1/3
A or B
A= $key eq "debug"B= $key eq "skip_empty_lines"
ABresult
1X1
011
000
155 0 $self->{$key} = $value;
156 }
157 ▶ elsif (($key eq 'enclosure') || ($key eq 'escape')) {
Condition66%2/3
$key eq "enclosure" or $key eq "escape"
l!l&&r!l&&!r
7018
MC/DC50%1/2
$key eq "enclosure" or $key eq "escape"
$key eq "enclosure" $key eq "escape"
Truth table66%2/3
A or B
A= $key eq "enclosure"B= $key eq "escape"
ABresult
1X1
011
000
158 7 ▶ if (!defined($value) || ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 0 7 7
Condition33%1/3
not defined $value or ref $value
l!l&&r!l&&!r
007
MC/DC0%0/2
not defined $value or ref $value
not defined $value ref $value
Truth table33%1/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
159 0 croak("The '$key' option must be a string");
160 }
161 7 $self->{$key} = $value;
162 }
163 elsif ($key eq 'delimiter') {
164 7 ▶ if (!defined($value) || ref($value) || !length($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value or not length $value) 0 7 7
Condition33%2/6
not defined $value or ref $value or not length $value
l!l&&r!l&&!r
007
not defined $value or ref $value
l!l&&r!l&&!r
007
MC/DC0%0/3
not defined $value or ref $value or not length $value
not defined $value ref $value not length $value
Truth table25%1/4
not(A) or B or not(C)
A= not defined $valueB= ref $valueC= not length $value
ABCresult
1XX1
01X1
0011
0000
165 0 croak("The '$key' option must be a non-empty string");
166 }
167 7 $self->{$key} = $value;
168 }
169  
170 elsif ($key eq 'include_fields') {
171 1 ▶ if (ref($value) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY") 0 1 1
172 0 croak("The '$key' option must be an arrayref");
173 }
174 1 %opt_include_fields = map { $_ => undef } @$value;
175 }
176 elsif ($key eq 'field_aliases') {
177 8 ▶ if (ref($value) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "HASH") 0 8 8
178 0 croak("The '$key' option must be a hashref");
179 }
180 8 %opt_field_aliases = map { lc($_) => $value->{$_} } keys %$value;
181 }
182 elsif ($key eq 'field_normalizer') {
183 1 ▶ if (ref($value) ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "CODE") 0 1 1
184 0 croak("The '$key' option must be a code ref");
185 }
186 1 $opt_field_normalizer = $value;
187 }
188 elsif ($key eq 'mutators') {
189 1 ▶ if (ref($value) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "HASH") 0 1 1
190 0 croak("The '$key' option must be a hashref of field name => code ref pairs");
191 }
192 1 foreach my $name (keys %$value) {
193 2 my $mutator = $options{$key}->{$name};
194 2 ▶ if (defined($mutator)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mutator) 2 0 2
195 2 ▶ unless (ref($mutator) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $mutator eq "CODE") 0 2 2
196 0 croak('The mutator for "' . $name . '" must be a CODE ref');
197 }
198 }
199 }
200 1 $self->{$key} = $value;
201 }
202 elsif ($key =~ /^(?:Text(?::|_)CSV|text_csv)[\._:](.+)$/) {
203 0 $text_csv_options{$1} = $value;
204 }
205 else {
206 0 croak("Unknown option '$key'");
207 }
208 }
209 }
210  
211 my $text_csv = $self->{'text_csv'} = $proto->_new_text_csv_object({
212 'auto_diag' => 1,
213 'binary' => 1,
214 'blank_is_undef' => 1,
215 'empty_is_undef' => 1,
216 'sep_char' => $self->{'delimiter'},
217 'escape_char' => $self->{'escape'},
218 8 ▶ 'quote_char' => $self->{'enclosure'},
Condition50%1/2
$proto->_new_text_csv_object({'auto_diag', 1, 'binary', 1, 'blank_is_undef', 1, 'empty_is_undef', 1, 'sep_char', $self->{'delimiter'}, 'escape_char', $self->{'escape'}, 'quote_char', $self->{'enclosure'}, %text_csv_options}) || die('Method _new_text_csv_object() did not return a Text::CSV object as expected')
l!l
80
MC/DC0%0/1
$proto->_new_text_csv_object({'auto_diag', 1, 'binary', 1, 'blank_is_undef', 1, 'empty_is_undef', 1, 'sep_char', $self->{'delimiter'}, 'escape_char', $self->{'escape'}, 'quote_char', $self->{'enclosure'}, %text_csv_options}) || die('Method _new_text_csv_object() did not return a Text::CSV object as expected')
$proto->_new_text_csv_object({'auto_diag', 1, 'binary', 1, 'blank_is_undef', 1, 'empty_is_undef', 1, 'sep_char', $self->{'delimiter'}, 'escape_char', $self->{'escape'}, 'quote_char', $self->{'enclosure'}, %text_csv_options})
Truth table50%1/2
A
A= $proto->_new_text_csv_object({'auto_diag', 1, 'binary', 1, 'blank_is_undef', 1, 'empty_is_undef', 1, 'sep_char', $self->{'delimiter'}, 'escape_char', $self->{'escape'}, 'quote_char', $self->{'enclosure'}, %text_csv_options})
Aresult
11
00
219 %text_csv_options, # undocumented experimental feature; consider overriding _new_text_csv_object() instead.
220 }) || die('Method _new_text_csv_object() did not return a Text::CSV object as expected');
221  
222 # Emulate the original Text::CSV error message format but without the LF and with the caller script/module.
223 8 if (0 && $text_csv->can('callbacks')) { # exists since Text::CSV_XS version 1.06
224 $text_csv->callbacks(
225 0 ▶ 'error' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
226 0 my ($err, $msg, $pos, $recno, $fldno) = @_; # This is dumb because the object itself is not given.
227 0 ▶ if ($err eq '2012') { # EOF
Branch: 1 branch
BranchTrueFalseTotal
if ($err eq 2012) 0 0 0
228 0 return;
229 }
230 #CSV_XS ERROR: 2021 - EIQ - NL char inside quotes, binary off @ rec 10 pos 51 field 6
231 #die 'error args: ' . Data::Dumper::Dumper(\@_);
232 0 local $Carp::CarpInternal{'Text::CSV'} = 1;
233 0 local $Carp::CarpInternal{'Text::CSV_PP'} = 1;
234 0 local $Carp::CarpInternal{'Text::CSV_XS'} = 1;
235 0 carp(ref($text_csv) . " ERROR: $err - $msg \@ rec $recno pos $pos field $fldno");
236 0 return;
237 },
238 );
239 }
240  
241 # Read header row.
242 8 ▶ if (my $row = $self->{'text_csv'}->getline($self->{'h'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->{'text_csv'}->getline($self->{'h'})) { } 8 0 8
243 # Get the fieldname => column indices
244 8 ▶ for (my $x = 0; $x < @$row; $x++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < @$row) 64 8 72
245 64 my $name = $row->[$x];
246 64 ▶ unless(defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 64 64
247 0 next;
248 }
249 64 $name =~ s/^\s+|\s+$//g;
250 64 ▶ unless(length($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $name) 0 64 64
251 0 next;
252 }
253 64 ▶ if ($opt_field_normalizer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_field_normalizer) 8 56 64
254 8 &$opt_field_normalizer(\$name);
255 }
256 64 ▶ if (%opt_field_aliases) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt_field_aliases) 64 0 64
257 64 my $key = lc($name);
258 64 ▶ if (defined($opt_field_aliases{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt_field_aliases{$key}) 8 56 64
259 8 $name = $opt_field_aliases{$key};
260 }
261 }
262 64 ▶ if (%opt_include_fields && !exists($opt_include_fields{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt_include_fields and not exists $opt_include_fields{$name}) 6 58 64
Condition100%3/3
%opt_include_fields and not exists $opt_include_fields{$name}
!ll&&!rl&&r
5626
MC/DC100%2/2
%opt_include_fields and not exists $opt_include_fields{$name}
%opt_include_fields not exists $opt_include_fields{$name}
Truth table100%3/3
A and not(B)
A= %opt_include_fieldsB= not exists $opt_include_fields{$name}
ABresult
0X0
100
111
263 6 next;
264 }
265 58 ▶ if (exists($self->{'field_cols'}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"field_cols"}{$name}) 0 58 58
266 0 croak('Duplicate field "' . $name . '" detected');
267 }
268 58 $self->{'field_cols'}->{$name} = $x;
269 }
270 8 ▶ unless(%{$self->{'field_cols'}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%{$self->{"field_cols"};}) 0 8 8
271 0 ▶ croak(%opt_include_fields ? 'No fields found in header row to include' : 'No fields found in header row');
Branch: 1 branch
BranchTrueFalseTotal
%opt_include_fields ? : 0 0 0
272 }
273 # If include_fields option was given, reorder keys of field_cols to match it.
274 8 ▶ if (%opt_include_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt_include_fields) 1 7 8
275 1 my %field_cols;
276 #{$self->{'field_cols'}}
277 1 tie(%field_cols, 'Tie::IxHash');
278 1 foreach my $key (@{$options{'include_fields'}}) {
279 2 ▶ if (exists($self->{'field_cols'}->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"field_cols"}{$key}) 2 0 2
280 2 $field_cols{$key} = $self->{'field_cols'}->{$key};
281 }
282 }
283 1 $self->{'field_cols'} = \%field_cols;
284 }
285 }
286 else {
287 0 croak('No header line found in CSV');
288 }
289  
290 # Check that all the required header fields are present.
291 8 ▶ if (%opt_include_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (%opt_include_fields) 1 7 8
292 1 my @missing;
293 1 foreach my $name (keys %opt_include_fields) {
294 2 ▶ if (!exists($self->{'field_cols'}->{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"field_cols"}{$name}) 0 2 2
295 0 push(@missing, $name);
296 }
297 }
298 1 ▶ if (@missing) {
Branch: 1 branch
BranchTrueFalseTotal
if (@missing) 0 1 1
299 0 croak('The following column headers are missing: ' . join(', ', @missing));
300 }
301 }
302  
303 8 ▶ bless($self, ref($proto) || $proto);
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
080
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
304 8 return $self;
305 }
306  
307  
308  
309  
310 =head2 DESTROY
311  
312 Closes the private file handle, if any.
313  
314 =cut
315  
316 8 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=6.9
317 8 my $self = shift;
318 8 ▶ if ($self->{'own_h'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"own_h"}) 5 3 8
319 5 close($self->{'h'});
320 }
321 }
322  
323  
324  
325  
326  
327 =head1 PROTECTED STATIC METHODS
328  
329 =head2 _new_text_csv_object($args_hashref)
330  
331 Creates and returns a new Text::CSV object using the given arguments.
332 Child classes may override this method in order to tweak the arguments
333 and/or customize the creation of the internal Text::CSV object.
334  
335 =cut
336  
337 8 ▶sub _new_text_csv_object {
Subroutine _new_text_csv_object: called CC=2 SCAR=9.2
338 8 my $proto = shift;
339 8 my $args = shift; # hashref
340 8 my $result = Text::CSV->new($args);
341 8 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 8 8
342 # handle the bad practice of constructors that return undef instead of dieing.
343 0 my $epitaph = 'Failed to create a Text::CSV object';
344 0 require Data::Dumper; local $Data::Dumper::Terse = 1; $epitaph .= ' using args ' . Data::Dumper::Dumper($args);
345 0 die($epitaph);
346 }
347 8 return $result;
348 }
349  
350  
351  
352  
353  
354 =head1 PROTECTED OBJECT METHODS
355  
356 =head2 _read()
357  
358 Reads the next CSV data row and sets internal variables.
359  
360 =cut
361  
362 36 ▶sub _read {
Subroutine _read: called CC=10 SCAR=23.9
363 36 my $self = shift;
364 36 ▶ if (my $csv_row = $self->{'text_csv'}->getline($self->{'h'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $csv_row = $self->{'text_csv'}->getline($self->{'h'})) { } 27 9 36
365 27 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 27 27
366 0 require Data::Dumper;
367 0 local $Data::Dumper::Terse = 1;
368 0 warn(__PACKAGE__ . '::_read ' . Data::Dumper::Dumper($csv_row));
369 }
370 27 tie(my %row, 'Tie::IxHash');
371 27 my $field_cols = $self->{'field_cols'}; # name to index map
372 27 foreach my $k ($self->fieldNames()) {
373 198 my $i = $field_cols->{$k};
374 198 my $v = $csv_row->[$i];
375 198 ▶ if (defined($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 124 74 198
376 124 $v =~ s/^\s+|\s+$//g;
377 124 ▶ unless(length($v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $v) 0 124 124
378 0 $v = undef;
379 }
380 }
381 198 $row{$k} = $v;
382 }
383  
384 # Call mutators if defined
385 27 ▶ if (my $mutators = $self->{'mutators'}) { # name to coderef map
Branch: 1 branch
BranchTrueFalseTotal
if (my $mutators = $self->{"mutators"}) 3 24 27
386 3 foreach my $k (keys %$mutators) {
387 6 ▶ if (exists($row{$k})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row{$k}) 6 0 6
388 6 ▶ if (my $mutator = $mutators->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mutator = $mutators->{$k}) 6 0 6
389 6 &$mutator(\$row{$k}, \%row);
390 }
391 }
392 }
393 }
394  
395 27 $self->{'row'} = \%row;
396 27 $self->{'linenum'}++;
397 }
398 else {
399 9 $self->{'row'} = undef;
400 9 $self->{'linenum'} = 0;
401 }
402 }
403  
404  
405  
406  
407  
408  
409 =head1 PUBLIC OBJECT METHODS
410  
411 =head2 fieldNames()
412  
413 Returns the field names as an array.
414  
415 =cut
416  
417 30 ▶sub fieldNames {
Subroutine fieldNames: called CC=1 SCAR=0.0
Pod documented
418 30 my $self = shift;
419 30 return keys(%{$self->{'field_cols'}});
420 }
421  
422  
423  
424  
425  
426 =head2 current()
427  
428 Returns the current row.
429  
430 =cut
431  
432 0 ▶sub current {
Subroutine current: not called CC=1 SCAR=6.9
Pod documented
433 0 my $self = shift;
434 0 return $self->{'row'};
435 }
436  
437  
438  
439  
440  
441 =head2 linenum()
442  
443 Returns the current row index.
444  
445 =cut
446  
447 9 ▶sub linenum {
Subroutine linenum: called CC=1 SCAR=0.0
Pod documented
448 9 my $self = shift;
449 9 return $self->{'linenum'};
450 }
451  
452  
453  
454  
455  
456 =head2 nextRow()
457  
458 Reads the next row.
459  
460 =cut
461  
462 36 ▶sub nextRow {
Subroutine nextRow: called CC=1 SCAR=0.0
Pod documented
463 36 my $self = shift;
464 36 $self->_read();
465 36 return $self->{'row'};
466 }
467  
468  
469  
470  
471  
472 =head2 eof()
473  
474 Returns boolean
475  
476 =cut
477  
478 20 ▶sub eof {
Subroutine eof: called CC=1 SCAR=0.0
Pod documented
479 20 my $self = shift;
480 20 return $self->{'text_csv'}->eof();
481 }
482  
483  
484  
485  
486 =head2 rewind()
487  
488 Rewinds the file handle.
489  
490 =cut
491  
492 1 ▶sub rewind {
Subroutine rewind: called CC=2 SCAR=7.1
Pod documented
493 1 my $self = shift;
494 1 ▶ seek($self->{'h'},0,0) || croak('Failed to rewind file handle');
Branch: 1 branch
BranchTrueFalseTotal
seek $self->{"h"}, 0, 0 or croak("Failed to rewind file handle") 1 0 1
495 1 $self->{'text_csv'}->getline($self->{'h'}); # skip the header row
496 }
497  
498  
499  
500 4 1;
501  
502  
503 __END__
↑ Summary