Catmandu/Importer/CSV.pm

filter: Statementstmt 93.4% 43 / 46 Branchbran 62.5% 5 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 1 totaltotal 90.0% 63 / 70 CC 5 SCAR 6.6
CC
5
Cov
89%
__ANON__
4.9
_build_csv
3.0
BEGIN
1.0
CRAP 1.9
« Catmandu/Fix/Bind/maybe.pm ↑ Summary Catmandu/Counter.pm »
1 package Catmandu::Importer::CSV;
2  
3 7 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $VERSION = '1.2025';
6  
7 7 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 with 'Catmandu::Importer';
13  
14 7 has csv => (is => 'ro', lazy => 1, builder => '_build_csv');
15 has sep_char => (
16 is => 'ro',
17 13 ▶ default => sub {','},
Subroutine __ANON__: called CC=1 SCAR=0.0
18 25 ▶ coerce => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 25 my $sep_char = $_[0];
20 25 $sep_char =~ s/(\\[abefnrt])/"qq{$1}"/gee;
21 25 return $sep_char;
22 }
23 7 );
24 22 ▶has quote_char => (is => 'ro', default => sub {'"'});
Subroutine __ANON__: called CC=1 SCAR=0.0
25 22 ▶has escape_char => (is => 'ro', default => sub {'"'});
Subroutine __ANON__: called CC=1 SCAR=0.0
26 25 ▶has allow_loose_quotes => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
27 25 ▶has allow_loose_escapes => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
28 9 ▶has header => (is => 'ro', default => sub {1});
Subroutine __ANON__: called CC=1 SCAR=0.0
29 has fields => (
30 is => 'rwp',
31 25 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=16.0
32 25 my $fields = $_[0];
33 25 ▶ if (ref $fields eq 'ARRAY') {return $fields}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fields eq "ARRAY") 25 0 25
34 0 ▶ if (ref $fields eq 'HASH') {return [sort keys %$fields]}
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fields eq "HASH") 0 0 0
35 0 return [split ',', $fields];
36 },
37 7 );
38  
39 25 ▶sub _build_csv {
Subroutine _build_csv: called CC=3 SCAR=11.0
40 25 my ($self) = @_;
41 25 ▶ Text::CSV->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->quote_char ? : 22 3 25
$self->escape_char ? : 22 3 25
42 {
43 binary => 1,
44 sep_char => $self->sep_char,
45 quote_char => $self->quote_char ? $self->quote_char : undef,
46 escape_char => $self->escape_char ? $self->escape_char : undef,
47 allow_loose_quotes => $self->allow_loose_quotes,
48 allow_loose_escapes => $self->allow_loose_escapes,
49 }
50 );
51 }
52  
53 25 ▶sub generator {
Subroutine generator: called CC=1 SCAR=0.0
Pod undocumented
54 25 my ($self) = @_;
55 sub {
56 state $line = 0;
57 state $fh = $self->fh;
58 state $csv = do {
59 if ($self->header) {
60 if ($self->fields) {
61 $self->csv->header($fh, { 'sep_set' => [$self->sep_char] });
62 $line++;
63 }
64 else {
65 my @hdr = $self->csv->header($fh, { 'munge_column_names' => 'none', 'sep_set' => [$self->sep_char] });
66 $self->_set_fields(\@hdr);
67 $line++;
68 }
69 }
70 if ($self->fields) {
71 $self->csv->column_names($self->fields);
72 }
73 $self->csv;
74 };
75  
76 # generate field names if needed
77 unless ($self->fields) {
78 my $row = $csv->getline($fh) // return;
79 $line++;
80 my $fields = [0 .. (@$row - 1)];
81 $self->_set_fields($fields);
82 $csv->column_names($fields);
83 return reduce {
84 $a->{$b} = $row->[$b] if length $row->[$b];
85 $a;
86 } +{}, @$fields;
87 }
88  
89 my $rec = $csv->getline_hr($fh);
90 $line++;
91  
92 if (defined $rec || $csv->eof()) {
93 return $rec;
94 }
95 else {
96 my ($cde, $str, $pos) = $csv->error_diag();
97 die
98 "at line $line (byte $pos) found a Text::CSV parse error($cde) $str";
99 }
100 25 };
101 }
102  
103 7 1;
104  
105 __END__
« Catmandu/Fix/Bind/maybe.pm ↑ Summary Catmandu/Counter.pm »