File Coverage

blib/lib/App/TSVUtils.pm
Criterion Covered Total %
statement 30 33 90.9
branch 6 10 60.0
condition n/a
subroutine 6 6 100.0
pod 1 2 50.0
total 43 51 84.3


line stmt bran cond sub pod time code
1             package App::TSVUtils;
2              
3             our $DATE = '2019-12-19'; # DATE
4             our $VERSION = '0.004'; # VERSION
5              
6 1     1   87690 use 5.010001;
  1         11  
7 1     1   4 use strict;
  1         1  
  1         17  
8 1     1   3 use warnings;
  1         2  
  1         403  
9              
10             our %SPEC;
11              
12             my %args_common = (
13             );
14              
15             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             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             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             sub tsvutil {
60 1     1 0 3 my %args = @_;
61 1         3 my $action = $args{action};
62              
63 1         2 my $res = "";
64 1         2 my $i = 0;
65              
66 1         1 my $fh;
67 1 50       4 if ($args{filename} eq '-') {
68 0         0 $fh = *STDIN;
69             } else {
70             open $fh, "<", $args{filename} or
71 1 50       36 return [500, "Can't open input filename '$args{filename}': $!"];
72             }
73 1         16 binmode $fh, ":encoding(utf8)";
74             ;
75             my $code_getline = sub {
76 5     5   50 my $row0 = <$fh>;
77 5 100       21 return undef unless defined $row0;
78 4         8 chomp($row0);
79 4         20 [split /\t/, $row0];
80 1         57 };
81              
82 1         2 my $rows = [];
83              
84 1         3 while (my $row = $code_getline->()) {
85 4         4 $i++;
86 4 50       15 if ($action eq 'dump') {
87 4         12 push @$rows, $row;
88             } else {
89 0         0 return [400, "Unknown action '$action'"];
90             }
91             } # while getline()
92              
93 1 50       4 if ($action eq 'dump') {
94 1         20 return [200, "OK", $rows];
95             }
96              
97 0         0 [200, "OK", $res, {"cmdline.skip_format"=>1}];
98             } # tsvutil
99              
100             $SPEC{tsv_dump} = {
101             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             sub tsv_dump {
109 1     1 1 2872 my %args = @_;
110 1         4 tsvutil(%args, action=>'dump');
111             }
112              
113             1;
114             # ABSTRACT: CLI utilities related to TSV
115              
116             __END__
117              
118             =pod
119              
120             =encoding UTF-8
121              
122             =head1 NAME
123              
124             App::TSVUtils - CLI utilities related to TSV
125              
126             =head1 VERSION
127              
128             This document describes version 0.004 of App::TSVUtils (from Perl distribution App-TSVUtils), released on 2019-12-19.
129              
130             =head1 DESCRIPTION
131              
132             This distribution contains the following CLI utilities:
133              
134             =over
135              
136             =item * L<dump-tsv>
137              
138             =item * L<tsv-dump>
139              
140             =back
141              
142             =head1 FUNCTIONS
143              
144              
145             =head2 tsv_dump
146              
147             Usage:
148              
149             tsv_dump(%args) -> [status, msg, payload, meta]
150              
151             Dump TSV as data structure (array of arrays).
152              
153             This function is not exported.
154              
155             Arguments ('*' denotes required arguments):
156              
157             =over 4
158              
159             =item * B<filename>* => I<filename>
160              
161             Input TSV file.
162              
163             =back
164              
165             Returns an enveloped result (an array).
166              
167             First element (status) is an integer containing HTTP status code
168             (200 means OK, 4xx caller error, 5xx function error). Second element
169             (msg) is a string containing error message, or 'OK' if status is
170             200. Third element (payload) is optional, the actual result. Fourth
171             element (meta) is called result metadata and is optional, a hash
172             that contains extra information.
173              
174             Return value: (any)
175              
176             =for Pod::Coverage ^(tsvutil)$
177              
178             =head1 HOMEPAGE
179              
180             Please visit the project's homepage at L<https://metacpan.org/release/App-TSVUtils>.
181              
182             =head1 SOURCE
183              
184             Source repository is at L<https://github.com/perlancar/perl-App-TSVUtils>.
185              
186             =head1 BUGS
187              
188             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-TSVUtils>
189              
190             When submitting a bug or request, please include a test-file or a
191             patch to an existing test-file that illustrates the bug or desired
192             feature.
193              
194             =head1 SEE ALSO
195              
196             L<App::SerializeUtils>
197              
198             L<App::LTSVUtils>, which includes utilities like L<ltsv2tsv>, L<tsv2ltsv>, among
199             others.
200              
201             L<App::CSVUtils>, which includes L<csv2tsv>, L<tsv2csv> among others. Scripts
202             included in App::CSVUtils also support reading TSV via C<--tsv> flag.
203              
204             =head1 AUTHOR
205              
206             perlancar <perlancar@cpan.org>
207              
208             =head1 COPYRIGHT AND LICENSE
209              
210             This software is copyright (c) 2019 by perlancar@cpan.org.
211              
212             This is free software; you can redistribute it and/or modify it under
213             the same terms as the Perl 5 programming language system itself.
214              
215             =cut