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