File Coverage

blib/lib/POE/Filter/CSV_XS.pm
Criterion Covered Total %
statement 36 48 75.0
branch 3 10 30.0
condition n/a
subroutine 8 10 80.0
pod 5 5 100.0
total 52 73 71.2


line stmt bran cond sub pod time code
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              
10 3     3   79377 use strict;
  3         7  
  3         118  
11 3     3   25 use warnings;
  3         4  
  3         80  
12 3     3   4401 use Text::CSV_XS;
  3         52310  
  3         273  
13 3     3   33 use vars qw($VERSION);
  3         5  
  3         145  
14 3     3   15 use base qw(POE::Filter);
  3         6  
  3         13913  
15              
16             $VERSION = '1.16';
17              
18             sub new {
19 2     2 1 27 my $class = shift;
20 2         10 my %opts = @_;
21 2         15 $opts{lc $_} = delete $opts{$_} for keys %opts;
22 2         7 delete $opts{eol};
23 2         5 my $self = {};
24 2         9 $self->{BUFFER} = [];
25 2         21 $self->{csv_filter} = Text::CSV_XS->new( \%opts );
26 2         271 bless $self, $class;
27             }
28              
29             sub get {
30 2     2 1 1397 my ($self, $raw) = @_;
31 2         6 my $events = [];
32              
33 2         7 foreach my $event ( @$raw ) {
34 2         21 my $status = $self->{csv_filter}->parse($event);
35 2 50       140 push @$events, [ $self->{csv_filter}->fields() ] if $status;
36             }
37 2         24 return $events;
38             }
39              
40             sub get_one_start {
41 0     0 1 0 my ($self, $raw) = @_;
42 0         0 push @{ $self->{BUFFER} }, $_ for @$raw;
  0         0  
43             }
44              
45             sub get_one {
46 0     0 1 0 my $self = shift;
47 0         0 my $events = [];
48              
49 0         0 my $event = shift @{ $self->{BUFFER} };
  0         0  
50 0 0       0 if ( defined $event ) {
51 0         0 my $status = $self->{csv_filter}->parse($event);
52 0 0       0 push @$events, [ $self->{csv_filter}->fields() ] if $status;
53             }
54 0         0 return $events;
55             }
56              
57             sub put {
58 2     2 1 844 my ($self,$events) = @_;
59 2         7 my $raw_lines = [];
60              
61 2         6 foreach my $event ( @$events ) {
62 2 50       1099 if ( ref $event eq 'ARRAY' ) {
63 2         14 my $status = $self->{csv_filter}->combine(@$event);
64 2 50       55 push @$raw_lines, $self->{csv_filter}->string() if $status;
65              
66             }
67             else {
68 0         0 warn "non arrayref passed to put()\n";
69             }
70             }
71 2         21 return $raw_lines;
72             }
73              
74             1;
75             __END__