File Coverage

blib/lib/POE/Filter/CSV.pm
Criterion Covered Total %
statement 39 51 76.4
branch 3 10 30.0
condition n/a
subroutine 9 11 81.8
pod 6 6 100.0
total 57 78 73.0


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;
9              
10 2     2   52110 use strict;
  2         5  
  2         83  
11 2     2   10 use warnings;
  2         3  
  2         58  
12 2     2   3198 use Text::CSV;
  2         53729  
  2         13  
13 2     2   73 use vars qw($VERSION);
  2         4  
  2         102  
14 2     2   10 use base qw(POE::Filter);
  2         4  
  2         2382  
15              
16             $VERSION = '1.16';
17              
18             sub new {
19 1     1 1 13 my $class = shift;
20 1         3 my $self = {};
21 1         4 $self->{BUFFER} = [];
22 1         9 $self->{csv_filter} = Text::CSV->new(@_);
23 1         110 bless $self, $class;
24             }
25              
26             sub get {
27 2     2 1 2518 my ($self, $raw) = @_;
28 2         4 my $events = [];
29              
30 2         5 foreach my $event ( @$raw ) {
31 2         15 my $status = $self->{csv_filter}->parse($event);
32 2 50       626 push @$events, [ $self->{csv_filter}->fields() ] if $status;
33             }
34 2         42 return $events;
35             }
36              
37             sub get_one_start {
38 0     0 1 0 my ($self, $raw) = @_;
39 0         0 push @{ $self->{BUFFER} }, $_ for @$raw;
  0         0  
40             }
41              
42             sub get_one {
43 0     0 1 0 my $self = shift;
44 0         0 my $events = [];
45              
46 0         0 my $event = shift @{ $self->{BUFFER} };
  0         0  
47 0 0       0 if ( defined $event ) {
48 0         0 my $status = $self->{csv_filter}->parse($event);
49 0 0       0 push @$events, [ $self->{csv_filter}->fields() ] if $status;
50             }
51 0         0 return $events;
52             }
53              
54             sub put {
55 2     2 1 770 my ($self,$events) = @_;
56 2         3 my $raw_lines = [];
57              
58 2         5 foreach my $event ( @$events ) {
59 2 50       7 if ( ref $event eq 'ARRAY' ) {
60 2         9 my $status = $self->{csv_filter}->combine(@$event);
61 2 50       204 push @$raw_lines, $self->{csv_filter}->string() if $status;
62              
63             }
64             else {
65 0         0 warn "non arrayref passed to put()\n";
66             }
67             }
68 2         17 return $raw_lines;
69             }
70              
71             sub clone {
72 1     1 1 387 my $self = shift;
73 1         3 my $nself = { };
74 1         2 $nself->{$_} = $self->{$_} for keys %{ $self };
  1         10  
75 1         3 $nself->{BUFFER} = [ ];
76 1         4 return bless $nself, ref $self;
77             }
78              
79             1;
80             __END__