blib/lib/App/CSV.pm

filter: Statementstmt 84.3% 124 / 147 Branchbran 57.3% 39 / 68 Conditioncond 30.0% 6 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 3 totaltotal 70.4% 188 / 267 CC 43 SCAR 28.9
CC
43
Cov
72%
_setup_fh
45.9
format_error
30.0
init
28.8
CRAP 17.9
↑ Summary
1 package App::CSV;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.08';
8  
9 BEGIN {
10 # One day, MooseX::Getopt will allow us to pass pass_through to Getopt::Long.
11 # Until then, do the following ugly thing unconditionally.
12 # (We don't need a BEGIN block here yet. But we will once we start fussing
13 # around with version numbers.)
14 2 ▶ use Getopt::Long qw(:config pass_through);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 }
16  
17 2 with 'MooseX::Getopt';
18  
19 # Create "hasrw" and "hasro" sugar for less cumbersome attribute declarations.
20 # Why isn't this in Moose?
21 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ my $mk_has = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 4 my($access) = @_;
24 66 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 66 my($attr, @args) = @_;
26 66 has $attr => (
27 is => $access,
28 metaclass => 'Getopt', # For cmd_aliases
29 @args,
30 );
31 4 };
32 2 };
33 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 *hasrw = $mk_has->('rw');
35 2 *hasro = $mk_has->('ro');
36 }
37  
38 # Input and output filenames. Significant when we want to DWIM with TSV files.
39 2 hasrw input => (isa => 'Str', cmd_aliases => 'i');
40 2 hasrw output => (isa => 'Str', cmd_aliases => 'o');
41  
42 # isa => 'FileHandle' (or IO::String...)
43 2 hasrw _input_fh => ();
44 2 hasrw _output_fh => ();
45  
46 # TODO: command line aliases?
47 2 hasro from_tsv =>
48 (isa => 'Bool', cmd_aliases => 'from-tsv', predicate => 'has_from_tsv');
49 2 hasro to_tsv =>
50 (isa => 'Bool', cmd_aliases => 'to-tsv', predicate => 'has_to_tsv');
51  
52 2 hasrw _init => (isa => 'Bool');
53  
54 # Normalized column indexes.
55 2 hasrw columns => (isa => 'ArrayRef[Int]', cmd_aliases => 'c');
56  
57 # Named fields.
58 2 hasrw fields => (isa => 'ArrayRef[Str]', cmd_aliases => 'f');
59  
60 # The input and output CSV processors.
61 2 hasrw _input_csv => ();
62 2 hasrw _output_csv => ();
63  
64 # Text::CSV options, straight from the manpage.
65 # We override Text::CSV's default here... because it's WRONG.
66 2 our %TextCSVOptions = (
67 # name => [type, default, alias, @extra_opts]
68 quote_char => ['Str', '"', 'q'],
69 escape_char => ['Str', '"', 'e'],
70 sep_char => ['Str', ',', 's', is => 'rw'],
71 eol => ['Any', ''],
72 always_quote => ['Int', 0],
73 binary => ['Int', 1, 'b'],
74 keep_meta_info => ['Int', 0, 'k'],
75 allow_loose_quotes => ['Int', 0],
76 allow_loose_escapes => ['Int', 0],
77 allow_whitespace => ['Int', 0, 'w'],
78 verbatim => ['Int', 0],
79 );
80  
81 # output CSV processor options default to whatever the input option is.
82 # But you can override it just for output by saying --output_foo instead
83 # of --foo. (Thanks, gphat and t0m.)
84 2 ▶while (my($attr, $opts) = each %TextCSVOptions) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %App::CSV::TextCSVOptions) 22 2 24
85 22 my($type, $default, $short, @extra_opts) = @$opts;
86 22 hasro $attr => (
87 isa => $type,
88 default => $default,
89 __aliases($attr, $short),
90 @extra_opts
91 );
92 hasro "output_$attr" => (
93 isa => $type,
94 lazy => 1,
95 66 ▶ default => sub { $_[0]->$attr },
Subroutine __ANON__: called CC=1 SCAR=0.0
96 22 __output_aliases($attr),
97 @extra_opts,
98 );
99 }
100  
101 44 ▶sub __aliases {
Subroutine __aliases: called CC=4 SCAR=13.9
102 44 my($attr, $short) = @_;
103 44 my @aliases;
104 44 (my $dashes = $attr) =~ s/_/-/g;
105 44 ▶ push @aliases, $dashes if $attr ne $dashes;
Branch: 1 branch
BranchTrueFalseTotal
if $attr ne $dashes 38 6 44
106 44 ▶ push @aliases, $short if $short;
Branch: 1 branch
BranchTrueFalseTotal
if $short 12 32 44
107 44 ▶ return @aliases ? (cmd_aliases => \@aliases) : ();
Branch: 1 branch
BranchTrueFalseTotal
@aliases ? : 40 4 44
108 }
109  
110 22 ▶sub __output_aliases {
Subroutine __output_aliases: called CC=1 SCAR=0.0
111 22 return __aliases("output_" . shift);
112 }
113  
114 10 ▶sub __normalize_column {
Subroutine __normalize_column: called CC=2 SCAR=6.9
115 10 my($in) = @_;
116 10 ▶ return ($in <= 0) ? $in : $in - 1;
Branch: 1 branch
BranchTrueFalseTotal
$in <= 0 ? : 1 9 10
117 }
118  
119 3 ▶sub _get_header_map {
Subroutine _get_header_map: called CC=2 SCAR=6.9
120 3 my ($self) = @_;
121  
122 3 my $header_line = $self->_peek_line;
123 3 my %header_map;
124 3 my $field_number = 0;
125 3 for my $field (@$header_line) {
126 9 $header_map{$field} = ++$field_number;
127 }
128  
129 3 return \%header_map;
130 }
131  
132 4 ▶sub _fields_to_columns {
Subroutine _fields_to_columns: called CC=5 SCAR=16.1
133 4 my ($self, $fields) = @_;
134  
135 4 ▶ my @all_fields = map { /^(\d+)-(\d+)$/ ? $1 .. $2 : $_ } map { split "," } @$fields;
Branch: 1 branch
BranchTrueFalseTotal
/^(\d+)-(\d+)$/ ? : 1 8 9
136 4 my @named_fields = grep { /\D/ } @all_fields;
137  
138 4 my @normalized_fields;
139 # If there is at least one named field, we need to read the header line from input and translate
140 # into column number.
141 4 ▶ if (@named_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@named_fields) { } 3 1 4
142 3 my $header_map = $self->_get_header_map;
143 3 ▶ if (my @missing_fields = grep { not defined $header_map->{$_} } @named_fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing_fields) = grep({not defined $header_map->{$_};} @named_fields)) 1 2 3
144 1 die "The following named fields aren't in the input header: ",
145 join(", ", @missing_fields), "\n";
146 }
147 2 ▶ @normalized_fields = map { __normalize_column(/^\d+/ ? $_ : $header_map->{$_} ) } @all_fields;
Branch: 1 branch
BranchTrueFalseTotal
/^\d+/ ? : 2 2 4
148 }
149 else {
150 1 @normalized_fields = map { __normalize_column($_) } @all_fields;
151 }
152  
153 3 return \@normalized_fields;
154 }
155  
156 # TODO: You know, I end up with something like this on a lot of projects.
157 # Why isn't this easier? Having to remember to "use IO::Handle" is sad, too.
158 12 ▶sub _setup_fh {
Subroutine _setup_fh: called CC=8 SCAR=38.3
159 12 my($self, $name) = @_;
160 12 my $fh_name = "_${name}_fh";
161 12 ▶ return if $self->$fh_name; # someone had already injected a fh.
Branch: 1 branch
BranchTrueFalseTotal
if $self->$fh_name 12 0 12
162  
163 # ARGH. You can't open $fh, ">-", but you can't open $fh, "", "-" !?
164 0 ▶ my $dir2 = $name eq 'input' ? '' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'input' ? : 0 0 0
165 0 ▶ my $dir3 = $name eq 'input' ? '<' : '>';
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'input' ? : 0 0 0
166  
167 0 my $fh;
168 0 ▶ $self->$name('-') if not defined $self->$name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->$name 0 0 0
169 0 ▶ if ($self->$name eq '-') { # use stdio
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$name eq '-') { } 0 0 0
170 0 ▶ open $fh, "$dir2-" or die "open: stdio: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "$dir2-" or die "open: stdio: $!" 0 0 0
171 } else {
172 0 ▶ open $fh, $dir3, $self->$name or die "open: $self->$name: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $dir3, $self->$name or die "open: $self->${name}: $!" 0 0 0
173 }
174 0 $self->$fh_name($fh);
175 }
176  
177 11 ▶sub init {
Subroutine init: called CC=17 SCAR=33.6
Pod undocumented
178 11 my($self) = @_;
179 11 ▶ return if $self->_init;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_init 5 6 11
180 6 $self->_init(1);
181  
182 # TODO: zero-based field numbers as an option? nah?
183 0 ▶ my @columns = (($self->columns ? @{$self->columns} : ()), @{$self->extra_argv});
Branch: 1 branch
BranchTrueFalseTotal
$self->columns ? : 0 6 6
184 6 ▶ $self->columns([map { __normalize_column($_) } @columns]) if @columns;
Branch: 1 branch
BranchTrueFalseTotal
if @columns 2 4 6
185  
186 6 $self->_setup_fh($_) for qw(input output);
187  
188 # DWIMmy TSV
189 6 ▶ if ($self->from_tsv ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->from_tsv or not $self->has_from_tsv and $self->input and $self->input =~ /\.tsv$/) 0 6 6
Condition33%3/9
$self->from_tsv or not $self->has_from_tsv and $self->input and $self->input =~ /\.tsv$/
l!l&&r!l&&!r
006
not $self->has_from_tsv and $self->input and $self->input =~ /\.tsv$/
!ll&&!rl&&r
600
not $self->has_from_tsv and $self->input
!ll&&!rl&&r
060
MC/DC0%0/4
$self->from_tsv or not $self->has_from_tsv and $self->input and $self->input =~ /\.tsv$/
$self->from_tsv not $self->has_from_tsv $self->input $self->input =~ /\.tsv$/
Truth table20%1/5
A or not(B) and C and D
A= $self->from_tsvB= not $self->has_from_tsvC= $self->inputD= $self->input =~ /\.tsv$/
ABCDresult
1XXX1
01111
00XX0
010X0
01100
190 (!$self->has_from_tsv && $self->input && $self->input =~ /\.tsv$/)) {
191 0 $self->sep_char("\t");
192 }
193 6 ▶ if ($self->to_tsv ||
Branch: 1 branch
BranchTrueFalseTotal
if ($self->to_tsv or not $self->has_to_tsv and $self->output and $self->output =~ /\.tsv$/) 0 6 6
Condition33%3/9
$self->to_tsv or not $self->has_to_tsv and $self->output and $self->output =~ /\.tsv$/
l!l&&r!l&&!r
006
not $self->has_to_tsv and $self->output and $self->output =~ /\.tsv$/
!ll&&!rl&&r
600
not $self->has_to_tsv and $self->output
!ll&&!rl&&r
060
MC/DC0%0/4
$self->to_tsv or not $self->has_to_tsv and $self->output and $self->output =~ /\.tsv$/
$self->to_tsv not $self->has_to_tsv $self->output $self->output =~ /\.tsv$/
Truth table20%1/5
A or not(B) and C and D
A= $self->to_tsvB= not $self->has_to_tsvC= $self->outputD= $self->output =~ /\.tsv$/
ABCDresult
1XXX1
01111
00XX0
010X0
01100
194 (!$self->has_to_tsv && $self->output && $self->output =~ /\.tsv$/)) {
195 0 $self->output_sep_char("\t");
196 }
197  
198 $self->_input_csv(Text::CSV->new({
199 6 map { $_ => $self->$_ } keys %TextCSVOptions }));
200 $self->_output_csv(Text::CSV->new({
201 6 map { my $o = "output_$_"; $_ => $self->$o } keys %TextCSVOptions }));
202  
203 # If columns aren't specified, look for the --fields option. It allows
204 # use of named fields, list of comma-separated fields list, field
205 # ranges and so on.
206  
207 6 ▶ if (@columns) {
Branch: 1 branch
BranchTrueFalseTotal
if (@columns) { } 2 4 6
208 2 ▶ warn "--fields (-f) option is ignored since columns are also specified." if $self->fields;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fields 0 2 2
209 } else {
210 4 ▶ my @fields = $self->fields ? @{$self->fields} : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->fields ? : 4 0 4
211 4 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) 4 0 4
212 4 my $columns = $self->_fields_to_columns(\@fields);
213 3 $self->columns($columns);
214 }
215 }
216 }
217  
218 {
219 2 my $line_read;
220  
221 # Read a line from input, but push it back for later reading.
222 3 ▶ sub _peek_line {
Subroutine _peek_line: called CC=1 SCAR=0.0
223 3 my ($self) = @_;
224  
225 3 $line_read = $self->_input_csv->getline($self->_input_fh);
226 }
227  
228 # If there is a line already read, return it; otherwise, read from input.
229 23 ▶ sub _get_line {
Subroutine _get_line: called CC=2 SCAR=6.9
230 23 my ($self) = @_;
231  
232 23 my $line;
233 23 ▶ if ($line_read) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line_read) { } 2 21 23
234 2 $line = $line_read;
235 2 undef $line_read;
236 2 return $line;
237 } else {
238 21 return $self->_input_csv->getline($self->_input_fh);
239 }
240 }
241 }
242  
243 2 ▶sub run {
Subroutine run: called CC=6 SCAR=18.9
Pod undocumented
244 5 my($self) = @_;
245 5 $self->init;
246  
247 # L<perlsyn/"modifiers don't take loop labels">
248 5 INPUT: { do {
249 5 my $data;
250 5 ▶ while (defined($data = $self->_get_line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($data = $self->_get_line)) 18 5 23
251 18 ▶ if ($self->columns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->columns) 18 0 18
252 18 @$data = @$data[@{ $self->columns }];
253 }
254
255 18 ▶ if (!$self->_output_csv->print($self->_output_fh, $data)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_output_csv->print($self->_output_fh, $data)) 0 18 18
256 0 warn $self->format_error("Warning - Output error", diag => [$self->_input_csv->error_diag]), "\n";
257 0 next INPUT;
258 }
259 18 $self->_output_fh->print("\n");
260 }
261  
262 # Keeps us going on input errors.
263 # TODO: strict errors, according to command line, blah
264 5 ▶ if (not defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data) 5 0 5
265 5 ▶ last INPUT if $self->_input_csv->eof;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_input_csv->eof 5 0 5
266 0 warn $self->format_error("Warning - Input error", line => $., diag => [$self->_input_csv->error_diag]), "\n";
267 }
268 } }
269 }
270  
271 0 ▶sub format_error {
Subroutine format_error: not called CC=5 SCAR=34.0
Pod undocumented
272 0 my $self = shift;
273 0 my $msg = shift;
274 0 my %args = @_;
275  
276 $msg .= ", line $args{line}"
277 0 ▶ if defined $args{line};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"line"} 0 0 0
278  
279 0 ▶ if ($args{diag}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"diag"}) 0 0 0
280 0 ▶ my ($code, $err, $pos, $record) = @{ $args{diag} || [] };
Condition0%0/2
$args{'diag'} || []
l!l
00
MC/DC0%0/1
$args{'diag'} || []
$args{'diag'}
Truth table0%0/2
A
A= $args{'diag'}
Aresult
11
00
281 0 ▶ $msg .= ": " . join " - ",
Branch: 1 branch
BranchTrueFalseTotal
defined $record ? : 0 0 0
282 $code, $err, "position $pos",
283 (defined $record ? "record $record" : ());
284 }
285 0 return $msg;
286 }
287  
288 2 1;
289  
290 __END__
↑ Summary