blib/lib/App/TSVUtils.pm

filter: Statementstmt 92.8% 39 / 42 Branchbran 66.6% 8 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 50.0% 1 / 2 totaltotal 87.1% 54 / 62 CC 7 SCAR 13.4
CC
7
Cov
87%
tsvutil
6.3
__ANON__
2.0
BEGIN
1.0
CRAP 3.8
↑ Summary
1 package App::TSVUtils;
2  
3 2 our $DATE = '2019-12-19'; # DATE
4 2 our $VERSION = '0.004'; # VERSION
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our %SPEC;
11  
12 2 my %args_common = (
13 );
14  
15 2 my %arg_filename_0 = (
16 filename => {
17 summary => 'Input TSV file',
18 schema => 'filename*',
19 req => 1,
20 pos => 0,
21 cmdline_aliases => {f=>{}},
22 },
23 );
24  
25 2 my %arg_filename_1 = (
26 filename => {
27 summary => 'Input TSV file',
28 description => <<'_',
29  
30 Use `-` to read from stdin.
31  
32 _
33 schema => 'filename*',
34 req => 1,
35 pos => 1,
36 cmdline_aliases => {f=>{}},
37 },
38 );
39  
40 $SPEC{tsvutil} = {
41 2 v => 1.1,
42 summary => 'Perform action on a TSV file',
43 'x.no_index' => 1,
44 args => {
45 %args_common,
46 action => {
47 schema => ['str*', in=>[
48 'dump',
49 ]],
50 req => 1,
51 pos => 0,
52 cmdline_aliases => {a=>{}},
53 },
54 %arg_filename_1,
55 },
56 args_rels => {
57 },
58 };
59 1 ▶sub tsvutil {
Subroutine tsvutil: called CC=6 SCAR=18.4
Pod undocumented
60 1 my %args = @_;
61 1 my $action = $args{action};
62  
63 1 my $res = "";
64 1 my $i = 0;
65  
66 1 my $fh;
67 1 ▶ if ($args{filename} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'filename'} eq '-') { } 0 1 1
68 0 $fh = *STDIN;
69 } else {
70 open $fh, "<", $args{filename} or
71 1 ▶ return [500, "Can't open input filename '$args{filename}': $!"];
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $args{"filename"} or return [500, qq[Can't open input filename '$args{"filename"}': $!]] 1 0 1
72 }
73 1 binmode $fh, ":encoding(utf8)";
74 ;
75 5 ▶ my $code_getline = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
76 5 my $row0 = <$fh>;
77 5 ▶ return undef unless defined $row0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row0 1 4 5
78 4 chomp($row0);
79 4 [split /\t/, $row0];
80 1 };
81  
82 1 my $rows = [];
83  
84 1 ▶ while (my $row = $code_getline->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = &$code_getline()) 4 1 5
85 4 $i++;
86 4 ▶ if ($action eq 'dump') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq 'dump') { } 4 0 4
87 4 push @$rows, $row;
88 } else {
89 0 return [400, "Unknown action '$action'"];
90 }
91 } # while getline()
92  
93 1 ▶ if ($action eq 'dump') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "dump") 1 0 1
94 1 return [200, "OK", $rows];
95 }
96  
97 0 [200, "OK", $res, {"cmdline.skip_format"=>1}];
98 } # tsvutil
99  
100 $SPEC{tsv_dump} = {
101 2 v => 1.1,
102 summary => 'Dump TSV as data structure (array of arrays)',
103 args => {
104 %args_common,
105 %arg_filename_0,
106 },
107 };
108 1 ▶sub tsv_dump {
Subroutine tsv_dump: called CC=1 SCAR=0.0
Pod documented
109 1 my %args = @_;
110 1 tsvutil(%args, action=>'dump');
111 }
112  
113 2 1;
114 # ABSTRACT: CLI utilities related to TSV
115  
116 __END__
↑ Summary