blib/lib/App/RecordStream/Operation/fromcsv.pm

filter: Statementstmt 100% 69 / 69 Branchbran 83.3% 15 / 18 Conditioncond 66.6% 6 / 9 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 6 totaltotal 88.0% 103 / 117 CC 14 SCAR 15.0
CC
14
Cov
94%
get_records_from_handle
8.0
stream_done
4.0
init
4.0
CRAP 4.5
« blib/lib/App/RecordStream/InputStream.pm ↑ Summary blib/lib/App/RecordStream/Operation.pm »
1 package App::RecordStream::Operation::fromcsv;
2  
3 3 our $VERSION = "4.0.23";
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub init {
12 12 ▶ my $this = shift;
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
13 12 my $args = shift;
14  
15 12 my @fields;
16 12 my $header_line = undef;
17 12 my $strict = 0;
18 12 my $delim = ',';
19 12 my $escape = '"';
20 12 my $quote = '"';
21  
22 my $spec = {
23 3 ▶ "keys|k|field|f=s" => sub { push @fields, split(/,/, $_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
24 12 "header" => \$header_line,
25 "strict" => \$strict,
26 "delim|d=s" => \$delim,
27 "escape=s" => \$escape,
28 "quote=s" => \$quote,
29 };
30  
31 12 $this->parse_options($args, $spec);
32  
33 11 ▶ die "Delimiter must be a single character\n\n"
Branch: 1 branch
BranchTrueFalseTotal
unless length $delim == 1 0 11 11
34 unless length $delim == 1;
35  
36 11 ▶ my $csv_args = {
Branch: 1 branch
BranchTrueFalseTotal
$quote eq '' ? : 0 11 11
37 binary => 1,
38 eol => $/,
39 sep_char => $delim,
40 escape_char => $escape,
41  
42 # Text::CSV wants undef, but it's easier to pass the empty string at the shell.
43 quote_char => ($quote eq '' ? undef : $quote),
44 };
45  
46 11 ▶ if ( !$strict ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($strict) 7 4 11
47 7 $csv_args->{'allow_whitespace'} = 1;
48 7 $csv_args->{'allow_loose_quotes'} = 1;
49 7 $csv_args->{'allow_loose_escapes'} = 1;
50 }
51  
52 11 $this->{'FIELDS'} = \@fields;
53 11 $this->{'HEADER_LINE'} = $header_line;
54 11 $this->{'PARSER'} = new Text::CSV($csv_args);
55 11 $this->{'EXTRA_ARGS'} = $args;
56 }
57  
58 sub wants_input {
59 22 ▶ return 0;
Subroutine wants_input: called CC=1 SCAR=0.0
Pod undocumented
60 }
61  
62 sub stream_done {
63 11 ▶ my $this = shift;
Subroutine stream_done: called CC=4 SCAR=13.9
Pod undocumented
64  
65 11 my $files = $this->{'EXTRA_ARGS'};
66  
67 11 ▶ if ( scalar @$files > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$files > 0) { } 2 9 11
68 2 foreach my $file ( @$files ) {
69 4 $this->update_current_filename($file);
70  
71 4 ▶ open(my $fh, '<', $file) or die "Could not open file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Could not open file: $!\n" 0 4 4
72 4 $this->get_records_from_handle($fh);
73 4 close $fh;
74 }
75 }
76 else {
77 9 $this->get_records_from_handle(\*STDIN);
78 }
79 }
80  
81 sub get_records_from_handle {
82 13 ▶ my ($this, $handle) = @_;
Subroutine get_records_from_handle: called CC=8 SCAR=20.8
Pod undocumented
83  
84 13 my $parser = $this->{'PARSER'};
85 13 my $do_headers = $this->{'HEADER_LINE'};
86 13 my @fields = @{ $this->{'FIELDS'} };
87  
88 13 ▶ while(my $row = $parser->getline($handle)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $parser->getline($handle)) 25 13 38
89 25 ▶ if ( $do_headers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_headers) 3 22 25
90 3 push @fields, @$row;
91 3 $do_headers = 0;
92 3 next;
93 }
94  
95 22 my @values = @$row;
96  
97 22 my $record = App::RecordStream::Record->new();
98 22 ▶ for(my $i = 0; $i < @values; ++$i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @values) 62 22 84
99 62 ▶ my $key = $fields[$i] || $i;
Condition100%3/3
$fields[$i] || $i
l!l&&r!l&&!r
242414
MC/DC100%2/2
$fields[$i] || $i
$fields[$i] $i
Truth table100%3/3
A || B
A= $fields[$i]B= $i
ABresult
1X1
011
000
100 62 ${$record->guess_key_from_spec($key)} = $values[$i];
101 }
102 22 $this->push_record($record);
103 }
104  
105 # Parsing was a success only if we reached EOF and we got no error. Code
106 # 2012 is used by Text::CSV_XS for normal EOF condition.
107 13 my ($code, $msg, $pos) = $parser->error_diag;
108 13 ▶ unless ($parser->eof and ($code == 0 or $code == 2012)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser->eof and $code == 0 || $code == 2012) 2 11 13
Condition50%3/6
$parser->eof and $code == 0 || $code == 2012
!ll&&!rl&&r
2011
$code == 0 or $code == 2012
l!l&&r!l&&!r
0110
MC/DC33%1/3
$parser->eof and $code == 0 || $code == 2012
$parser->eof $code == 0 $code == 2012
Truth table50%2/4
A and B or C
A= $parser->eofB= $code == 0C= $code == 2012
ABCresult
0XX0
1000
11X1
1011
109 2 my ($line, $file) = ($., $this->get_current_filename);
110 2 die "fromcsv: parse error: $msg ($code)",
111 ", roughly at position $pos, line $line, file $file\n";
112 }
113 }
114  
115 sub add_help_types {
116 12 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
117 12 $this->use_help_type('keyspecs');
118 }
119  
120 sub usage
121 {
122 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
123  
124 1 my $options = [
125 [ 'key|k <keys>', 'Comma separated list of field names. May be specified multiple times, may be key specs' ],
126 [ 'header', 'Take field names from the first line of input' ],
127 [ 'strict', 'Do not trim whitespaces, allow loose quoting (quotes inside quotes), or allow the use of escape characters when not strictly needed. (not recommended, for most cases, though may help with parsing quoted fields containing newlines)' ],
128 [ 'delim|-d <character>', "Field delimiter to use when reading input lines (default ',')."],
129 [ 'escape <character>', "Escape character used in quoted fields (default '\x22')."],
130 [ 'quote <character>', "Quote character used in quoted fields (default '\x22'). Use the empty string to indicate no quoted fields."],
131 ];
132  
133 1 my $args_string = $this->options_string($options);
134  
135 1 return <<USAGE;
136 Usage: recs-fromcsv <args> [<files>]
137 __FORMAT_TEXT__
138 Each line of input (or lines of <files>) is split on commas to
139 produce an output record. Fields are named numerically (0, 1, etc.), or as
140 given by --field, or as read by --header. Lines may be split on delimiters
141 other than commas by providing --delim.
142 __FORMAT_TEXT__
143  
144 Arguments:
145 $args_string
146  
147 Examples:
148 Parse csv separated fields x and y.
149 recs-fromcsv --field x,y
150 Parse data with a header line specifying fields
151 recs-fromcsv --header
152 Parse tsv data (using bash syntax for a literal tab)
153 recs-fromcsv --delim \$'\\t'
154 USAGE
155 }
156  
157 3 1;
« blib/lib/App/RecordStream/InputStream.pm ↑ Summary blib/lib/App/RecordStream/Operation.pm »