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