line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
use Catmandu::Sane; |
3
|
7
|
|
|
7
|
|
88170
|
|
|
7
|
|
|
|
|
16
|
|
|
7
|
|
|
|
|
46
|
|
4
|
|
|
|
|
|
|
our $VERSION = '1.2018'; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
use Text::CSV; |
7
|
7
|
|
|
7
|
|
3661
|
use Moo; |
|
7
|
|
|
|
|
74886
|
|
|
7
|
|
|
|
|
250
|
|
8
|
7
|
|
|
7
|
|
44
|
use namespace::clean; |
|
7
|
|
|
|
|
11
|
|
|
7
|
|
|
|
|
40
|
|
9
|
7
|
|
|
7
|
|
2341
|
|
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
39
|
|
10
|
|
|
|
|
|
|
with 'Catmandu::TabularExporter'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has csv => (is => 'lazy'); |
13
|
|
|
|
|
|
|
has quote_char => (is => 'ro', default => sub {'"'}); |
14
|
|
|
|
|
|
|
has escape_char => (is => 'ro', default => sub {'"'}); |
15
|
|
|
|
|
|
|
has always_quote => (is => 'ro'); |
16
|
|
|
|
|
|
|
has quote_space => (is => 'ro'); |
17
|
|
|
|
|
|
|
has sep_char => ( |
18
|
|
|
|
|
|
|
is => 'ro', |
19
|
|
|
|
|
|
|
default => sub {','}, |
20
|
|
|
|
|
|
|
coerce => sub { |
21
|
|
|
|
|
|
|
my $sep_char = $_[0]; |
22
|
|
|
|
|
|
|
$sep_char =~ s/(\\[abefnrt])/"qq{$1}"/gee; |
23
|
|
|
|
|
|
|
return $sep_char; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my ($self) = @_; |
28
|
|
|
|
|
|
|
Text::CSV->new( |
29
|
17
|
|
|
17
|
|
173
|
{ |
30
|
17
|
100
|
|
|
|
251
|
binary => 1, |
|
|
100
|
|
|
|
|
|
31
|
|
|
|
|
|
|
eol => "\n", |
32
|
|
|
|
|
|
|
sep_char => $self->sep_char, |
33
|
|
|
|
|
|
|
always_quote => $self->always_quote, |
34
|
|
|
|
|
|
|
quote_space => $self->quote_space, |
35
|
|
|
|
|
|
|
quote_char => $self->quote_char ? $self->quote_char : undef, |
36
|
|
|
|
|
|
|
escape_char => $self->escape_char ? $self->escape_char : undef, |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my ($self, $data) = @_; |
42
|
|
|
|
|
|
|
my $fields = $self->fields; |
43
|
|
|
|
|
|
|
my $row = [ |
44
|
|
|
|
|
|
|
map { |
45
|
|
|
|
|
|
|
my $val = $data->{$_} // ""; |
46
|
|
|
|
|
|
|
$val =~ s/\t/\\t/g; |
47
|
|
|
|
|
|
|
$val =~ s/\n/\\n/g; |
48
|
|
|
|
|
|
|
$val =~ s/\r/\\r/g; |
49
|
|
|
|
|
|
|
$val; |
50
|
|
|
|
|
|
|
} @$fields |
51
|
|
|
|
|
|
|
]; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
$self->_print_header; |
54
|
|
|
|
|
|
|
$self->csv->print($self->fh, $row); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my ($self) = @_; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# ensure header gets printed even if there are no records |
60
|
|
|
|
|
|
|
$self->_print_header; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my ($self) = @_; |
64
|
|
|
|
|
|
|
if (!$self->count && $self->header) { |
65
|
|
|
|
|
|
|
my $row = $self->columns || $self->fields; |
66
|
|
|
|
|
|
|
$self->csv->print($self->fh, $row) if $row && @$row; |
67
|
|
|
|
|
|
|
} |
68
|
46
|
|
|
46
|
|
76
|
} |
69
|
46
|
100
|
100
|
|
|
208
|
|
70
|
16
|
|
100
|
|
|
59
|
1; |
71
|
16
|
100
|
66
|
|
|
307
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=pod |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 NAME |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Catmandu::Exporter::CSV - a CSV exporter |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# On the command line |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$ catmandu convert XSL to CSV < data.xls |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
$ catmandu convert JSON to CSV --fix myfixes.txt --sep_char ';' < data.json |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
# In a Perl script |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use Catmandu; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('CSV', |
92
|
|
|
|
|
|
|
fix => 'myfix.txt', |
93
|
|
|
|
|
|
|
quote_char => '"', |
94
|
|
|
|
|
|
|
sep_char => ',', |
95
|
|
|
|
|
|
|
escape_char => '"' , |
96
|
|
|
|
|
|
|
always_quote => 1, |
97
|
|
|
|
|
|
|
header => 1); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$exporter->fields("f1,f2,f3"); |
100
|
|
|
|
|
|
|
$exporter->fields([qw(f1 f2 f3)]); |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
$exporter->add_many($arrayref); |
103
|
|
|
|
|
|
|
$exporter->add_many($iterator); |
104
|
|
|
|
|
|
|
$exporter->add_many(sub { }); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$exporter->add($hashref); |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
printf "exported %d items\n" , $exporter->count; |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
This C<Catmandu::Exporter> exports items as rows with comma-separated values |
113
|
|
|
|
|
|
|
(CSV). Serialization is based on L<Text::CSV>. A header line with field names |
114
|
|
|
|
|
|
|
will be included if option C<header> is set. See L<Catmandu::TabularExporter> |
115
|
|
|
|
|
|
|
on how to configure the field mapping and column names. Newlines and tabulator |
116
|
|
|
|
|
|
|
values in field values are escaped as C<\n>, C<\r>, and C<\t>. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 CONFIGURATION |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=over |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=item file |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Write output to a local file given by its path or file handle. Alternatively a |
125
|
|
|
|
|
|
|
scalar reference can be passed to write to a string and a code reference can be |
126
|
|
|
|
|
|
|
used to write to a callback function. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item fh |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Write the output to an L<IO::Handle>. If not specified, |
131
|
|
|
|
|
|
|
L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output |
132
|
|
|
|
|
|
|
handle from the C<file> argument or by using STDOUT. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=item fix |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
An ARRAY of one or more fixes or file scripts to be applied to exported items. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=item encoding |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Binmode of the output stream C<fh>. Set to "C<:utf8>" by default. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=item sep_char |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Column separator (C<,> by default) |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item quote_char |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Quotation character (C<"> by default) |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=item escape_char |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Character for escaping inside quoted field (C<"> by default) |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item fields |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
See L<Catmandu::TabularExporter>. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=item columns |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
See L<Catmandu::TabularExporter>. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item header |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Include a header line with column names. Enabled by default. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=back |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 METHODS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
See L<Catmandu::TabularExporter>, L<Catmandu::Exporter>, L<Catmandu::Addable>, |
171
|
|
|
|
|
|
|
L<Catmandu::Fixable>, L<Catmandu::Counter>, and L<Catmandu::Logger> for a full |
172
|
|
|
|
|
|
|
list of methods. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 SEE ALSO |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
L<Catmandu::Importer::CSV>, L<Catmandu::Exporter::Table> |
177
|
|
|
|
|
|
|
L<Catmandu::Exporter::XLS> |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=cut |