| 1 | # Author Chris "BinGOs" Williams | ||||||||||
| 2 | # | ||||||||||
| 3 | # This module may be used, modified, and distributed under the same | ||||||||||
| 4 | # terms as Perl itself. Please see the license that came with your Perl | ||||||||||
| 5 | # distribution for details. | ||||||||||
| 6 | # | ||||||||||
| 7 | |||||||||||
| 8 | package POE::Filter::CSV_XS; | ||||||||||
| 9 | 3 | $POE::Filter::CSV_XS::VERSION = '1.18'; | |||||||||
| 10 | #ABSTRACT: A POE-based parser for CSV based files. | ||||||||||
| 11 | |||||||||||
| 12 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 3 | ▶ | use Text::CSV_XS; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 3 | ▶ | use base qw(POE::Filter); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 2 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 18 | 2 | my $class = shift; | |||||||||
| 19 | 2 | my %opts = @_; | |||||||||
| 20 | 2 | $opts{lc $_} = delete $opts{$_} for keys %opts; | |||||||||
| 21 | 2 | delete $opts{eol}; | |||||||||
| 22 | 2 | my $self = {}; | |||||||||
| 23 | 2 | $self->{BUFFER} = []; | |||||||||
| 24 | 2 | $self->{csv_filter} = Text::CSV_XS->new( \%opts ); | |||||||||
| 25 | 2 | bless $self, $class; | |||||||||
| 26 | } | ||||||||||
| 27 | |||||||||||
| 28 | 2 | ▶ | sub get { | ||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 29 | 2 | my ($self, $raw) = @_; | |||||||||
| 30 | 2 | my $events = []; | |||||||||
| 31 | |||||||||||
| 32 | 2 | foreach my $event ( @$raw ) { | |||||||||
| 33 | 2 | my $status = $self->{csv_filter}->parse($event); | |||||||||
| 34 | 2 | ▶ | push @$events, [ $self->{csv_filter}->fields() ] if $status; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | } | ||||||||||
| 36 | 2 | return $events; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | sub get_one_start { | ||||||||
|
Subroutine
get_one_start: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 40 | 0 | my ($self, $raw) = @_; | |||||||||
| 41 | 0 | push @{ $self->{BUFFER} }, $_ for @$raw; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 0 | ▶ | sub get_one { | ||||||||
|
Subroutine
get_one: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 45 | 0 | my $self = shift; | |||||||||
| 46 | 0 | my $events = []; | |||||||||
| 47 | |||||||||||
| 48 | 0 | my $event = shift @{ $self->{BUFFER} }; | |||||||||
| 49 | 0 | ▶ | if ( defined $event ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 0 | my $status = $self->{csv_filter}->parse($event); | |||||||||
| 51 | 0 | ▶ | push @$events, [ $self->{csv_filter}->fields() ] if $status; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | 0 | return $events; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 2 | ▶ | sub put { | ||||||||
|
Subroutine
put: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||
| 57 | 2 | my ($self,$events) = @_; | |||||||||
| 58 | 2 | my $raw_lines = []; | |||||||||
| 59 | |||||||||||
| 60 | 2 | foreach my $event ( @$events ) { | |||||||||
| 61 | 2 | ▶ | if ( ref $event eq 'ARRAY' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 2 | my $status = $self->{csv_filter}->combine(@$event); | |||||||||
| 63 | 2 | ▶ | push @$raw_lines, $self->{csv_filter}->string() if $status; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | |||||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | 0 | warn "non arrayref passed to put()\n"; | |||||||||
| 68 | } | ||||||||||
| 69 | } | ||||||||||
| 70 | 2 | return $raw_lines; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 3 | qq[let,us,csv,xs]; | |||||||||
| 74 | |||||||||||
| 75 | __END__ | ||||||||||