lib/FP/Text/CSV.pm

filter: Statementstmt 100% 107 / 107 Branchbran 53.1% 17 / 32 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 100% 27 / 27 Podpod 0.0% 0 / 8 totaltotal 78.3% 156 / 199 CC 22 SCAR 8.7
CC
22
Cov
84%
csv_file_to_rows
4.5
rows_to_csv_file
4.2
rows_to_csv_fh
3.2
CRAP 2.4
« meta/FunctionalPerl/Indexing.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/PathUtil.pm »
1 #
2 # Copyright (c) 2015-2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::Text::CSV - functional interface to Text::CSV
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Text::CSV qw(csv_line_xparser
Subroutine BEGIN: called CC=1 SCAR=0.0
17 csv_fh_to_rows
18 csv_file_to_rows
19 rows_to_csv_fh
20 1 rows_to_csv_file);
21  
22 my $csvparams = +{sep_char => ";", eol => "\n"};
23 # $csvparams and any of its entries are optional,
24 # defaults are taken from $FP::Text::CSV::defaults
25  
26 use Chj::xopen qw(xopen_read xopen_write);
27 use FP::List; use FP::Stream; use FP::Equal 'is_equal';
28 mkdir ".tmp";
29  
30 # -- Output: ---
31 my $rows =
32 cons [ "i", "i^2" ],
33 stream_iota->map(sub {
34 my ($i) = @_;
35 [ $i, $i*$i ]
36 })->take(100);
37 rows_to_csv_fh (Keep($rows), xopen_write(".tmp/a1.csv"),
38 $csvparams);
39 # or
40 rows_to_csv_file ($rows, ".tmp/a2.csv", $csvparams);
41  
42  
43 # -- Input: ---
44 my $p = csv_line_xparser $csvparams;
45 my @vals = &$p("1;2;3;4\n");
46 is_equal \@vals, [1,2,3,4];
47  
48 my $itemstream1 =
49 csv_fh_to_rows(xopen_read(".tmp/a1.csv"), $csvparams);
50 # or
51 my $itemstream2 = csv_file_to_rows(".tmp/a2.csv", $csvparams);
52  
53 is_equal $itemstream1, $itemstream2;
54 is_equal $itemstream2->first, [ "i", "i^2" ];
55 is_equal $itemstream2->second, [ 0, 0 ];
56 is_equal $itemstream2->ref(10), [ 9, 81 ];
57  
58  
59 =head1 DESCRIPTION
60  
61 Handle CSV input and output in the form of functional streams (lazily
62 computed linked lists).
63  
64 =head1 NOTE
65  
66 L<Text::CSV> added a 'csv' export somewhat recently. Together with
67 L<FP::autobox> that can make for a nice solution (although not lazy),
68 too.
69  
70 This is alpha software! Read the status section in the package README
71 or on the L<website|http://functional-perl.org/>.
72  
73 =cut
74  
75 package FP::Text::CSV;
76 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 7 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
79 7 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 7 our @EXPORT = qw();
82 7 our @EXPORT_OK = qw(
83 new_csv_instance
84 csv_line_xparser
85 csv_fh_to_rows
86 csv_file_to_rows
87 csv_printer
88 rows_to_csv_fh
89 rows_to_csv_file
90 );
91 7 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
92  
93 7 ▶use FP::List ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
94 7 ▶use FP::Lazy ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
95 7 ▶use FP::Stream 'Weakened', 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
96 7 ▶use Text::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
97 7 ▶use FP::HashSet 'hashset_union';
Subroutine BEGIN: called CC=1 SCAR=0.0
98 7 ▶use Chj::xopen 'xopen_read';
Subroutine BEGIN: called CC=1 SCAR=0.0
99 7 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 7 ▶use FP::Stream "stream_for_each";
Subroutine BEGIN: called CC=1 SCAR=0.0
101 7 ▶use Chj::xtmpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 7 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
103  
104 7 our $defaults = +{ binary => 1, sep_char => ",", eol => "\r\n", };
105  
106 11 ▶sub params {
Subroutine params: called CC=3 SCAR=11.7
Pod undocumented
107 11 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 10 0 10
108 11 my ($maybe_params) = @_;
109 11 ▶ defined $maybe_params ? hashset_union($maybe_params, $defaults) : $defaults
Branch: 1 branch
BranchTrueFalseTotal
defined $maybe_params ? : 10 0 10
110 }
111  
112 11 ▶sub new_csv_instance {
Subroutine new_csv_instance: called CC=3 SCAR=11.4
Pod undocumented
113 11 __ 'new_csv_instance($maybe_params) -> Text::CSV';
114 11 ▶ @_ == 1 or fp_croak_arity "1";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 10 0 10
115 11 my ($maybe_params) = @_;
116 11 ▶ Text::CSV->new(params $maybe_params)
Branch: 1 branch
BranchTrueFalseTotal
"Text::CSV"->new(params($maybe_params)) or die "could not create a Text::CSV instance" 10 0 10
117 or die "could not create a Text::CSV instance";
118 }
119  
120 2 ▶sub csv_line_xparser {
Subroutine csv_line_xparser: called CC=2 SCAR=7.1
Pod undocumented
121 2 __ 'csv_line_xparser($maybe_params) -> sub($line) -> fields';
122 2 ▶ @_ == 1 or fp_croak_arity "1";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
123 2 my ($maybe_params) = @_;
124 2 my $csv = new_csv_instance $maybe_params;
125  
126 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
127 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
128 1 my ($line) = @_;
129 1 ▶ $csv->parse($line)
Branch: 1 branch
BranchTrueFalseTotal
$csv->parse($line) or die "CSV parsing failure" 1 0 1
130 or die
131 "CSV parsing failure"; # XX how to get error message from Text::CSV?
132 1 $csv->fields
133 }
134 2 }
135  
136 7 ▶sub csv_fh_to_rows {
Subroutine csv_fh_to_rows: called CC=3 SCAR=11.1
Pod undocumented
137 7 __ 'csv_fh_to_rows($in, $maybe_params) -> stream';
138 7 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1-2") 7 0 7
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
070
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
139 7 my ($in, $maybe_params) = @_;
140 7 my $csv = new_csv_instance($maybe_params);
141 7 my $next;
142 224 ▶ $next = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 224 my $next = $next;
144 224 ▶ lazy {
Subroutine __ANON__: called CC=2 SCAR=6.9
145 224 ▶ if (my $row = $csv->getline($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $csv->getline($in)) { } 217 7 224
146  
147 # XX error checks?
148 217 cons $row, &$next;
149 } else {
150 7 $in->xclose;
151 7 null
152 }
153 }
154 7 };
155 7 &{ Weakened $next}
156 }
157  
158 6 ▶sub csv_file_to_rows {
Subroutine csv_file_to_rows: called CC=4 SCAR=15.0
Pod undocumented
159 6 __ 'csv_file_to_rows($path, $maybe_params) -> stream';
160 6 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1-2") 6 0 6
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
060
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
161 6 my ($path, $maybe_params) = @_;
162 6 my $in = xopen_read $path;
163 6 ▶ binmode($in, ":encoding(utf-8)") or die "binmode";
Branch: 1 branch
BranchTrueFalseTotal
binmode $in, ":encoding(utf-8)" or die "binmode" 6 0 6
164 6 csv_fh_to_rows $in, $maybe_params
165 }
166  
167 # -- Output: ---
168  
169 2 ▶sub csv_printer {
Subroutine csv_printer: called CC=3 SCAR=11.7
Pod undocumented
170 2 __ 'csv_printer($fh, $maybe_params) -> sub ($row) -> ()';
171 2 ▶ @_ == 1 or @_ == 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or @_ == 2 or fp_croak_arity("1-2") 2 0 2
Condition33%1/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
020
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
172 2 my ($fh, $maybe_params) = @_;
173 2 my $csv = new_csv_instance($maybe_params);
174 202 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
175 202 my ($row) = @_;
176 202 ▶ $csv->print($fh, $row)
Branch: 1 branch
BranchTrueFalseTotal
$csv->print($fh, $row) or die 'could not write CSV row: ' . $csv->error_diag 202 0 202
177 or die "could not write CSV row: " . $csv->error_diag;
178  
179 # XX ok?
180 }
181 2 }
182  
183 2 ▶sub rows_to_csv_fh {
Subroutine rows_to_csv_fh: called CC=3 SCAR=11.8
Pod undocumented
184 2 __ 'rows_to_csv_fh($s, $fh, $maybe_params) -> ()';
185 2 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 2 0 2
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
020
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
186 2 my ($s, $fh, $maybe_params) = @_;
187 2 weaken $_[0];
188 2 stream_for_each csv_printer($fh, $maybe_params), $s
189 }
190  
191 1 ▶sub rows_to_csv_file {
Subroutine rows_to_csv_file: called CC=4 SCAR=14.5
Pod undocumented
192 1 __ 'rows_to_csv_file($s, $path, $maybe_params) -> ()';
193 1 ▶ @_ == 2 or @_ == 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or fp_croak_arity("2-3") 1 0 1
Condition33%1/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
010
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table33%1/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
194 1 my ($s, $path, $maybe_params) = @_;
195 1 weaken $_[0];
196 1 my $out = xtmpfile $path;
197 1 ▶ binmode($out, ":encoding(utf-8)") or die "binmode";
Branch: 1 branch
BranchTrueFalseTotal
binmode $out, ":encoding(utf-8)" or die "binmode" 1 0 1
198 1 rows_to_csv_fh($s, $out, $maybe_params);
199 1 $out->xclose;
200 1 $out->xputback(0666 & ~umask);
201 }
202  
203 7 1
« meta/FunctionalPerl/Indexing.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/PathUtil.pm »