| 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.11; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | my $proto = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=52 SCAR=49.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $file and length $file
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | croak('Missing $file argument'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 8 | ▶ | if (ref($file)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 3 | ▶ | unless ((ref($file) eq 'GLOB') || UNIVERSAL::isa($file, 'IO::Handle')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $file eq "GLOB" or UNIVERSAL::isa($file, "IO::Handle")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 5 | ▶ | $options{'debug'} && warn(__PACKAGE__ . "::new file open mode is $mode\n"); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | if (%options) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 8 | foreach my $key (keys %options) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 25 | my $value = $options{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 25 | ▶ | if (($key eq 'debug') || ($key eq 'skip_empty_lines')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 7 branches
Condition33%1/3
$key eq "debug" or $key eq "skip_empty_lines"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | $self->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | ▶ | elsif (($key eq 'enclosure') || ($key eq 'escape')) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$key eq "enclosure" or $key eq "escape"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 7 | ▶ | if (!defined($value) || ref($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $value or ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | croak("The '$key' option must be a string"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 7 | $self->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | elsif ($key eq 'delimiter') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 7 | ▶ | if (!defined($value) || ref($value) || !length($value)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not defined $value or ref $value or not length $value
not defined $value or ref $value
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | croak("The '$key' option must be a non-empty string"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | $self->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | elsif ($key eq 'include_fields') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 1 | ▶ | if (ref($value) ne 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | croak("The '$key' option must be an arrayref"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | %opt_include_fields = map { $_ => undef } @$value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | elsif ($key eq 'field_aliases') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 8 | ▶ | if (ref($value) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | croak("The '$key' option must be a hashref"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 8 | %opt_field_aliases = map { lc($_) => $value->{$_} } keys %$value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | elsif ($key eq 'field_normalizer') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | if (ref($value) ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | croak("The '$key' option must be a code ref"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 1 | $opt_field_normalizer = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | elsif ($key eq 'mutators') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 1 | ▶ | if (ref($value) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | croak("The '$key' option must be a hashref of field name => code ref pairs"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | foreach my $name (keys %$value) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | my $mutator = $options{$key}->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | if (defined($mutator)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | ▶ | unless (ref($mutator) eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | croak('The mutator for "' . $name . '" must be a CODE ref'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 1 | $self->{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | croak("Unknown option '$key'"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | my $text_csv = $self->{'text_csv'} = Text::CSV->new({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 'auto_diag' => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 'binary' => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 'blank_is_undef' => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 'empty_is_undef' => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 'sep_char' => $self->{'delimiter'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 'escape_char' => $self->{'escape'}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 8 | 'quote_char' => $self->{'enclosure'}, | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | # Emulate the original Text::CSV error message format but without the LF and with the caller script/module. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 8 | if (0) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | $text_csv->callbacks( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 'error' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | my ($err, $msg, $pos, $recno, $fldno) = @_; # This is dumb because the object itself is not given. | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if ($err eq '2012') { # EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | #CSV_XS ERROR: 2021 - EIQ - NL char inside quotes, binary off @ rec 10 pos 51 field 6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | #die 'error args: ' . Data::Dumper::Dumper(\@_); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | local $Carp::CarpInternal{'Text::CSV'} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | local $Carp::CarpInternal{'Text::CSV_PP'} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | local $Carp::CarpInternal{'Text::CSV_XS'} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | carp(ref($text_csv) . " ERROR: $err - $msg \@ rec $recno pos $pos field $fldno"); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Read header row. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 8 | ▶ | if (my $row = $self->{'text_csv'}->getline($self->{'h'})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | # Get the fieldname => column indices | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 8 | ▶ | for (my $x = 0; $x < @$row; $x++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 64 | my $name = $row->[$x]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 64 | ▶ | unless(defined($name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 64 | $name =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 64 | ▶ | unless(length($name)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 64 | ▶ | if ($opt_field_normalizer) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 8 | &$opt_field_normalizer(\$name); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 64 | ▶ | if (%opt_field_aliases) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 64 | my $key = lc($name); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 64 | ▶ | if (defined($opt_field_aliases{$key})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 8 | $name = $opt_field_aliases{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 64 | ▶ | if (%opt_include_fields && !exists($opt_include_fields{$name})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
%opt_include_fields and not exists $opt_include_fields{$name}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 58 | ▶ | if (exists($self->{'field_cols'}->{$name})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | croak('Duplicate field "' . $name . '" detected'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 58 | $self->{'field_cols'}->{$name} = $x; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 8 | ▶ | unless(%{$self->{'field_cols'}}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | croak(%opt_include_fields ? 'No fields found in header row to include' : 'No fields found in header row'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | # If include_fields option was given, reorder keys of field_cols to match it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 8 | ▶ | if (%opt_include_fields) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | my %field_cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | #{$self->{'field_cols'}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 1 | tie(%field_cols, 'Tie::IxHash'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 1 | foreach my $key (@{$options{'include_fields'}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 2 | ▶ | if (exists($self->{'field_cols'}->{$key})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 2 | $field_cols{$key} = $self->{'field_cols'}->{$key}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | $self->{'field_cols'} = \%field_cols; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | croak('No header line found in CSV'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # Check that all the required header fields are present. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 8 | ▶ | if (%opt_include_fields) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 1 | my @missing; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 1 | foreach my $name (keys %opt_include_fields) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 2 | ▶ | if (!exists($self->{'field_cols'}->{$name})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 0 | push(@missing, $name); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | ▶ | if (@missing) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | croak('The following column headers are missing: ' . join(', ', @missing)); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 8 | ▶ | bless($self, ref($proto) || $proto); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $proto || $proto
MC/DC0%0/2
ref $proto || $proto
ref $proto
$proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 8 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | =head2 DESTROY | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | Closes the private file handle, if any. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | sub DESTROY { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 8 | ▶ | if ($self->{'own_h'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 5 | close($self->{'h'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | =head1 PROTECTED OBJECT METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | =head2 _read() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | Reads the next CSV data row and sets internal variables. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | sub _read { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 36 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read: called CC=10 SCAR=23.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 36 | ▶ | if (my $csv_row = $self->{'text_csv'}->getline($self->{'h'})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 27 | ▶ | if ($self->{'debug'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | require Data::Dumper; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | local $Data::Dumper::Terse = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | warn(__PACKAGE__ . '::_read ' . Data::Dumper::Dumper($csv_row)); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 27 | tie(my %row, 'Tie::IxHash'); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 27 | my $field_cols = $self->{'field_cols'}; # name to index map | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 27 | foreach my $k ($self->fieldNames()) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 198 | my $i = $field_cols->{$k}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 198 | my $v = $csv_row->[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 198 | ▶ | if (defined($v)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 124 | $v =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 124 | ▶ | unless(length($v)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | $v = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 198 | $row{$k} = $v; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # Call mutators if defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 27 | ▶ | if (my $mutators = $self->{'mutators'}) { # name to coderef map | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 3 | foreach my $k (keys %$mutators) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 6 | ▶ | if (exists($row{$k})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 6 | ▶ | if (my $mutator = $mutators->{$k}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 6 | &$mutator(\$row{$k}, \%row); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 27 | $self->{'row'} = \%row; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 27 | $self->{'linenum'}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 9 | $self->{'row'} = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 9 | $self->{'linenum'} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | =head1 PUBLIC OBJECT METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | =head2 fieldNames() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | Returns the field names as an array. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | sub fieldNames { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 30 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fieldNames: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 30 | return keys(%{$self->{'field_cols'}}); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | =head2 current() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | Returns the current row. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | sub current { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
current: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | return $self->{'row'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | =head2 linenum() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | Returns the current row index. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | sub linenum { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
linenum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 9 | return $self->{'linenum'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | =head2 nextRow() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | Reads the next row. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | sub nextRow { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 36 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
nextRow: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 36 | $self->_read(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | 36 | return $self->{'row'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | =head2 eof() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | Returns boolean | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | sub eof { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 20 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
eof: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 20 | return $self->{'text_csv'}->eof(); | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | =head2 rewind() | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | Rewinds the file handle. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | sub rewind { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 1 | ▶ | seek($self->{'h'},0,0) || croak('Failed to rewind file handle'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | 1 | $self->{'text_csv'}->getline($self->{'h'}); # skip the header row | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||