| 1 | package Text::CSV::Simple::__::Base; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use Class::Trigger; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 0 | ▶ | __PACKAGE__->add_trigger(on_failure => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 6 | 0 | my ($self, $csv) = @_; | |||||||||||
| 7 | 0 | warn "Failed on " . $csv->error_input . "\n"; | |||||||||||
| 8 | 2 | }); | |||||||||||
| 9 | |||||||||||||
| 10 | package Text::CSV::Simple; | ||||||||||||
| 11 | |||||||||||||
| 12 | 2 | ▶ | use base 'Text::CSV::Simple::__::Base'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 2 | $VERSION = '1.00'; | |||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 2 | ▶ | use Text::CSV_XS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 2 | ▶ | use File::Slurp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | |||||||||||||
| 21 | =head1 NAME | ||||||||||||
| 22 | |||||||||||||
| 23 | Text::CSV::Simple - Simpler parsing of CSV files | ||||||||||||
| 24 | |||||||||||||
| 25 | =head1 SYNOPSIS | ||||||||||||
| 26 | |||||||||||||
| 27 | my $parser = Text::CSV::Simple->new; | ||||||||||||
| 28 | my @data = $parser->read_file($datafile); | ||||||||||||
| 29 | print @$_ foreach @data; | ||||||||||||
| 30 | |||||||||||||
| 31 | # Only want certain fields? | ||||||||||||
| 32 | my $parser = Text::CSV::Simple->new; | ||||||||||||
| 33 | $parser->want_fields(1, 2, 4, 8); | ||||||||||||
| 34 | my @data = $parser->read_file($datafile); | ||||||||||||
| 35 | |||||||||||||
| 36 | # Map the fields to a hash? | ||||||||||||
| 37 | my $parser = Text::CSV::Simple->new; | ||||||||||||
| 38 | $parser->field_map(qw/id name null town/); | ||||||||||||
| 39 | my @data = $parser->read_file($datafile); | ||||||||||||
| 40 | |||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||
| 42 | |||||||||||||
| 43 | Parsing CSV files is nasty. It seems so simple, but it usually isn't. | ||||||||||||
| 44 | Thankfully Text::CSV_XS takes care of most of that nastiness for us. | ||||||||||||
| 45 | |||||||||||||
| 46 | Like many modules which have to deal with all manner of nastiness and | ||||||||||||
| 47 | edge cases, however, it can be clumsy to work with in the simple case. | ||||||||||||
| 48 | |||||||||||||
| 49 | Thus this module. | ||||||||||||
| 50 | |||||||||||||
| 51 | We simply provide a little wrapper around Text::CSV_XS to streamline the | ||||||||||||
| 52 | common case scenario. (Or at least B<my> common case scenario; feel free | ||||||||||||
| 53 | to write your own wrapper if this one doesn't do what you want). | ||||||||||||
| 54 | |||||||||||||
| 55 | =head1 METHODS | ||||||||||||
| 56 | |||||||||||||
| 57 | =head2 new | ||||||||||||
| 58 | |||||||||||||
| 59 | my $parser = Text::CSV::Simple->new(\%options); | ||||||||||||
| 60 | |||||||||||||
| 61 | Construct a new parser. This takes all the same options as Text::CSV_XS. | ||||||||||||
| 62 | |||||||||||||
| 63 | =head2 read_file | ||||||||||||
| 64 | |||||||||||||
| 65 | my @data = $parser->read_file($filename); | ||||||||||||
| 66 | |||||||||||||
| 67 | Read the data in the given file, parse it, and return it as a list of | ||||||||||||
| 68 | data. | ||||||||||||
| 69 | |||||||||||||
| 70 | Each entry in the returned list will be a listref of parsed CSV data. | ||||||||||||
| 71 | |||||||||||||
| 72 | =head2 want_fields | ||||||||||||
| 73 | |||||||||||||
| 74 | $parser->want_fields(1, 2, 4, 8); | ||||||||||||
| 75 | |||||||||||||
| 76 | If you only want to extract certain fields from the CSV, you can set up | ||||||||||||
| 77 | the list of fields you want, and, hey presto, those are the only ones | ||||||||||||
| 78 | that will be returned in each listref. The fields, as with Perl arrays, | ||||||||||||
| 79 | are zero based (i.e. the above example returns the second, third, fifth | ||||||||||||
| 80 | and ninth entries for each line) | ||||||||||||
| 81 | |||||||||||||
| 82 | =head2 field_map | ||||||||||||
| 83 | |||||||||||||
| 84 | $parser->field_map(qw/id name null town null postcode/); | ||||||||||||
| 85 | |||||||||||||
| 86 | Rather than getting back a listref for each entry in your CSV file, you | ||||||||||||
| 87 | often want a hash of data with meaningful names. If you set up a field_map | ||||||||||||
| 88 | giving the name you'd like for each field, then we do the right thing | ||||||||||||
| 89 | for you! Fields named 'null' vanish into the ether. | ||||||||||||
| 90 | |||||||||||||
| 91 | =head1 TRIGGER POINTS | ||||||||||||
| 92 | |||||||||||||
| 93 | To enable you to make this module do things that I haven't dreamed off | ||||||||||||
| 94 | (without you having to bother me with requests to extend the | ||||||||||||
| 95 | functionality), we use Class::Trigger to provide a variety of points at | ||||||||||||
| 96 | which you can hook in and do what you need. In general these should be | ||||||||||||
| 97 | attached to the $parser object you've already created, although you | ||||||||||||
| 98 | could also subclass this module and set these up as class data. | ||||||||||||
| 99 | |||||||||||||
| 100 | Each time we call a trigger we wrap it in an eval block. If the eval | ||||||||||||
| 101 | block catches an error we simply call 'next' on the loop. These can | ||||||||||||
| 102 | therefore be used for short-circuiting.on certain conditions. | ||||||||||||
| 103 | |||||||||||||
| 104 | =head2 before_parse | ||||||||||||
| 105 | |||||||||||||
| 106 | $parser->add_trigger(before_parse => sub { | ||||||||||||
| 107 | my ($self, $line) = @_; | ||||||||||||
| 108 | die unless $line =~ /wanted/i; | ||||||||||||
| 109 | }); | ||||||||||||
| 110 | |||||||||||||
| 111 | Before we call Text::CSV_XS 'parse' on each line of input text, we call | ||||||||||||
| 112 | the before_parse trigger with that line of text. | ||||||||||||
| 113 | |||||||||||||
| 114 | =head2 after_parse | ||||||||||||
| 115 | |||||||||||||
| 116 | $parser->add_trigger(after_parse => sub { | ||||||||||||
| 117 | my ($self, $data) = @_; | ||||||||||||
| 118 | die unless $wanted{$data->[0]}; | ||||||||||||
| 119 | }); | ||||||||||||
| 120 | |||||||||||||
| 121 | After we sucessfully call Text::CSV_XS 'parse' on each line of input text, | ||||||||||||
| 122 | we call the after_parse trigger with a list ref of the values | ||||||||||||
| 123 | |||||||||||||
| 124 | =head2 error | ||||||||||||
| 125 | |||||||||||||
| 126 | Currenly, for each line that we can't parse, we call the 'failure' | ||||||||||||
| 127 | trigger (with the Text::CSV_XS parser object), which emits a warning | ||||||||||||
| 128 | and moves on. This happens in an invisible superclass, so you can supply | ||||||||||||
| 129 | your own behaviour here: | ||||||||||||
| 130 | |||||||||||||
| 131 | $parser->add_trigger(on_failure => sub { | ||||||||||||
| 132 | my ($self, $csv) = @_; | ||||||||||||
| 133 | warn "Failed on " . $csv->error_input . "\n"; | ||||||||||||
| 134 | }); | ||||||||||||
| 135 | |||||||||||||
| 136 | =cut | ||||||||||||
| 137 | |||||||||||||
| 138 | 6 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 139 | 6 | my $class = shift; | |||||||||||
| 140 | 6 | return bless { _parser => Text::CSV_XS->new(@_), } => $class; | |||||||||||
| 141 | } | ||||||||||||
| 142 | |||||||||||||
| 143 | 6 | ▶ | sub _parser { shift->{_parser} } | ||||||||||
|
Subroutine
_parser: called CC=1 SCAR=0.0
| |||||||||||||
| 144 | |||||||||||||
| 145 | 12 | ▶ | sub _file { | ||||||||||
|
Subroutine
_file: called CC=2 SCAR=6.9
| |||||||||||||
| 146 | 12 | my $self = shift; | |||||||||||
| 147 | 12 | ▶ | $self->{_file} = shift if @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 148 | 12 | return $self->{_file}; | |||||||||||
| 149 | } | ||||||||||||
| 150 | |||||||||||||
| 151 | 6 | ▶ | sub _contents { | ||||||||||
|
Subroutine
_contents: called CC=2 SCAR=7.1
| |||||||||||||
| 152 | 6 | my $self = shift; | |||||||||||
| 153 | 6 | ▶ | my @lines = File::Slurp::read_file($self->_file) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | or die "Can't read " . $self->_file; | ||||||||||||
| 155 | 6 | return @lines; | |||||||||||
| 156 | } | ||||||||||||
| 157 | |||||||||||||
| 158 | 19 | ▶ | sub want_fields { | ||||||||||
|
Subroutine
want_fields: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 159 | 19 | my $self = shift; | |||||||||||
| 160 | 19 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 161 | 1 | $self->{_wanted} = [@_]; | |||||||||||
| 162 | } | ||||||||||||
| 163 | 19 | ▶ | return @{ $self->{_wanted} || [] }; | ||||||||||
|
Condition100%2/2
$self->{'_wanted'} || []
MC/DC100%1/1
$self->{'_wanted'} || []
$self->{'_wanted'}
Truth table100%2/2
A
A= $self->{'_wanted'}
| |||||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | 17 | ▶ | sub field_map { | ||||||||||
|
Subroutine
field_map: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 167 | 17 | my $self = shift; | |||||||||||
| 168 | 17 | ▶ | if (@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 169 | 2 | $self->{_map} = [@_]; | |||||||||||
| 170 | } | ||||||||||||
| 171 | 17 | ▶ | return @{ $self->{_map} || [] }; | ||||||||||
|
Condition100%2/2
$self->{'_map'} || []
MC/DC100%1/1
$self->{'_map'} || []
$self->{'_map'}
Truth table100%2/2
A
A= $self->{'_map'}
| |||||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | 6 | ▶ | sub read_file { | ||||||||||
|
Subroutine
read_file: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||
| 175 | 6 | my ($self, $file) = @_; | |||||||||||
| 176 | 6 | $self->_file($file); | |||||||||||
| 177 | 6 | my @lines = $self->_contents; | |||||||||||
| 178 | 6 | my @return; | |||||||||||
| 179 | 6 | my $csv = $self->_parser; | |||||||||||
| 180 | 6 | foreach my $line (@lines) { | |||||||||||
| 181 | 18 | eval { $self->call_trigger(before_parse => $line) }; | |||||||||||
| 182 | 18 | ▶ | next if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 183 | 17 | ▶ | next unless $line; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | 17 | ▶ | unless ($csv->parse($line)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | 0 | $self->call_trigger(on_failure => $csv); | |||||||||||
| 186 | 0 | next; | |||||||||||
| 187 | } | ||||||||||||
| 188 | 17 | my @fields = $csv->fields; | |||||||||||
| 189 | 17 | eval { $self->call_trigger(after_parse => \@fields) }; | |||||||||||
| 190 | 17 | ▶ | next if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 191 | 15 | ▶ | if (my @wanted = $self->want_fields) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 192 | 3 | @fields = @fields[ $self->want_fields ]; | |||||||||||
| 193 | } | ||||||||||||
| 194 | 15 | my $addition = [ @fields ]; | |||||||||||
| 195 | 15 | ▶ | if (my @map = $self->field_map) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 196 | 6 | my $hash = { map { $_ => shift @fields } @map }; | |||||||||||
| 197 | 6 | delete $hash->{null}; | |||||||||||
| 198 | 6 | $addition = $hash; | |||||||||||
| 199 | } | ||||||||||||
| 200 | 15 | eval { $self->call_trigger(after_processing => $addition) }; | |||||||||||
| 201 | 15 | ▶ | next if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 202 | 15 | push @return, $addition; | |||||||||||
| 203 | } | ||||||||||||
| 204 | 6 | return @return; | |||||||||||
| 205 | } | ||||||||||||
| 206 | |||||||||||||
| 207 | =head1 AUTHOR | ||||||||||||
| 208 | |||||||||||||
| 209 | Tony Bowden | ||||||||||||
| 210 | |||||||||||||
| 211 | =head1 BUGS | ||||||||||||
| 212 | |||||||||||||
| 213 | This doesn't cope with multi-line fields. Technically the CSV format | ||||||||||||
| 214 | allows this, but this is meant to be a ::Simple module, and coping with | ||||||||||||
| 215 | that is currently outside the cope of this module. | ||||||||||||
| 216 | |||||||||||||
| 217 | =head1 SEE ALSO | ||||||||||||
| 218 | |||||||||||||
| 219 | Text::CSV_XS | ||||||||||||
| 220 | |||||||||||||
| 221 | =head1 BUGS and QUERIES | ||||||||||||
| 222 | |||||||||||||
| 223 | Please direct all correspondence regarding this module to: | ||||||||||||
| 224 | bug-Text-CSV-Simple@rt.cpan.org | ||||||||||||
| 225 | |||||||||||||
| 226 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 227 | |||||||||||||
| 228 | Copyright (C) 2004-2005 Tony Bowden. | ||||||||||||
| 229 | |||||||||||||
| 230 | This program is free software; you can redistribute it and/or modify | ||||||||||||
| 231 | it under the terms of the GNU General Public License; either version | ||||||||||||
| 232 | 2 of the License, or (at your option) any later version. | ||||||||||||
| 233 | |||||||||||||
| 234 | This program is distributed in the hope that it will be useful, | ||||||||||||
| 235 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||
| 236 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||||||||||||
| 237 | |||||||||||||
| 238 | =cut | ||||||||||||
| 239 | |||||||||||||