blib/lib/Cikl/Smrt/Parsers/ParseCsv.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 78 CC 6 SCAR 29.5
CC
6
Cov
0%
parse
42.0
name
2.0
BEGIN
2.0
CRAP 19.1
1 package Cikl::Smrt::Parsers::ParseCsv;
2  
3 use strict;
4 use warnings;
5  
6 use Mouse;
7 use Cikl::Smrt::Parser;
8 extends 'Cikl::Smrt::Parser';
9 use namespace::autoclean;
10 use Text::CSV_XS;
11  
12 use constant NAME => 'csv';
13 sub name { return NAME; }
14  
15 has 'feed_limit' => (
16 is => 'ro',
17 );
18  
19 has 'values' => (
20 is => 'ro',
21 isa => 'Str',
22 required => 1
23 );
24  
25 has 'skipfirst' => (
26 is => 'ro',
27 isa => 'Num',
28 default => 0
29 );
30  
31 sub parse {
32 my $self = shift;
33 my $fh = shift;
34 my $broker = shift;
35  
36 my $csv = Text::CSV_XS->new();
37  
38 my @cols = split(/\s*,\s*/, $self->values);
39
40 my $start = 0;
41 my $end = undef;
42 if(my $l = $self->feed_limit){
43 if(ref($l) eq 'ARRAY'){
44 ($start,$end) = @{$l};
45 } else {
46 ($start,$end) = (0,$l-1);
47 }
48 }
49 $start += 1 if($self->skipfirst);
50  
51 # Find our way to the first line we need to read.
52 my $lineno = -1;
53 while (!$fh->eof()) {
54 $lineno++;
55 last if (defined($end) && $lineno > $end);
56  
57 my $row = $csv->getline($fh);
58  
59 next if $lineno < $start;
60  
61 my $h = {};
62 foreach (0 ... $#cols){
63 $h->{$cols[$_]} = $row->[$_];
64 }
65 $broker->emit($h);
66 }
67  
68 return(undef);
69 }
70  
71 __PACKAGE__->meta->make_immutable;
72  
73 1;