| 1 | package Catmandu::Importer::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::Importer::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::Importer'; | |||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | has header => (is => 'ro', default => sub {1}); | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 14 | has sep_char => ( | ||||||||||
| 15 | is => 'ro', | ||||||||||
| 16 | 2 | ▶ | default => sub {"\t"}, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 17 | coerce => sub { | ||||||||||
| 18 | 3 | ▶ | my $sep_char = $_[0]; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||
| 19 | 3 | $sep_char =~ s/(\\[abefnrt])/"qq{$1}"/gee; | |||||||||
| 20 | 3 | return $sep_char; | |||||||||
| 21 | } | ||||||||||
| 22 | 1 | ); | |||||||||
| 23 | has fields => ( | ||||||||||
| 24 | is => 'rwp', | ||||||||||
| 25 | coerce => sub { | ||||||||||
| 26 | 0 | ▶ | my $fields = $_[0]; | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 27 | 0 | ▶ | if (ref $fields eq 'ARRAY') {return $fields} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | ▶ | if (ref $fields eq 'HASH') {return [sort keys %$fields]} | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 0 | return [split ',', $fields]; | |||||||||
| 30 | }, | ||||||||||
| 31 | 1 | ); | |||||||||
| 32 | |||||||||||
| 33 | 1 | has csv => (is => 'lazy'); | |||||||||
| 34 | |||||||||||
| 35 | sub _build_csv { | ||||||||||
| 36 | 3 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
_build_csv: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 3 | my $csv = Catmandu::Importer::CSV->new( | |||||||||
| 38 | header => $self->header, | ||||||||||
| 39 | sep_char => $self->sep_char, | ||||||||||
| 40 | quote_char => undef, | ||||||||||
| 41 | escape_char => undef, | ||||||||||
| 42 | file => $self->file, | ||||||||||
| 43 | ); | ||||||||||
| 44 | 3 | $csv->{fields} = $self->fields; | |||||||||
| 45 | 3 | $csv; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | sub generator { | ||||||||||
| 49 | 3 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
generator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 50 | 3 | $self->csv->generator; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 1 | 1; | |||||||||
| 54 | |||||||||||
| 55 | __END__ | ||||||||||
| 56 | =pod | ||||||||||
| 57 | |||||||||||
| 58 | =head1 NAME | ||||||||||
| 59 | |||||||||||
| 60 | Catmandu::Importer::TSV - Package that imports tab-separated values | ||||||||||
| 61 | |||||||||||
| 62 | =head1 SYNOPSIS | ||||||||||
| 63 | |||||||||||
| 64 | # From the command line | ||||||||||
| 65 | |||||||||||
| 66 | # convert a TSV file to JSON | ||||||||||
| 67 | catmandu convert TSV to JSON < journals.tab | ||||||||||
| 68 | |||||||||||
| 69 | # Or in a Perl script | ||||||||||
| 70 | |||||||||||
| 71 | use Catmandu; | ||||||||||
| 72 | |||||||||||
| 73 | my $importer = Catmandu->importer('TSV', file => "/foo/bar.tab"); | ||||||||||
| 74 | |||||||||||
| 75 | my $n = $importer->each(sub { | ||||||||||
| 76 | my $hashref = $_[0]; | ||||||||||
| 77 | # ... | ||||||||||
| 78 | }); | ||||||||||
| 79 | |||||||||||
| 80 | =head1 DESCRIPTION | ||||||||||
| 81 | |||||||||||
| 82 | This package imports tab-separated values (TSV). The object | ||||||||||
| 83 | fields are read from the TSV header line or given via the C<fields> parameter. | ||||||||||
| 84 | |||||||||||
| 85 | =head1 CONFIGURATION | ||||||||||
| 86 | |||||||||||
| 87 | =over | ||||||||||
| 88 | |||||||||||
| 89 | =item file | ||||||||||
| 90 | |||||||||||
| 91 | Read input from a local file given by its path. Alternatively a scalar | ||||||||||
| 92 | reference can be passed to read from a string. | ||||||||||
| 93 | |||||||||||
| 94 | =item fh | ||||||||||
| 95 | |||||||||||
| 96 | Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to | ||||||||||
| 97 | create the input stream from the C<file> argument or by using STDIN. | ||||||||||
| 98 | |||||||||||
| 99 | =item encoding | ||||||||||
| 100 | |||||||||||
| 101 | Binmode of the input stream C<fh>. Set to C<:utf8> by default. | ||||||||||
| 102 | |||||||||||
| 103 | =item fix | ||||||||||
| 104 | |||||||||||
| 105 | An ARRAY of one or more fixes or file scripts to be applied to imported items. | ||||||||||
| 106 | |||||||||||
| 107 | =item fields | ||||||||||
| 108 | |||||||||||
| 109 | List of fields to be used as columns, given as array reference, comma-separated | ||||||||||
| 110 | string, or hash reference. If C<header> is C<0> and C<fields> is C<undef> the | ||||||||||
| 111 | fields will be named by column index ("0", "1", "2", ...). | ||||||||||
| 112 | |||||||||||
| 113 | =item header | ||||||||||
| 114 | |||||||||||
| 115 | Read fields from a header line with the column names, if set to C<1> (the | ||||||||||
| 116 | default). | ||||||||||
| 117 | |||||||||||
| 118 | =item sep_char | ||||||||||
| 119 | |||||||||||
| 120 | Column separator (C<tab> by default) | ||||||||||
| 121 | |||||||||||
| 122 | =back | ||||||||||
| 123 | |||||||||||
| 124 | =head1 METHODS | ||||||||||
| 125 | |||||||||||
| 126 | Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are | ||||||||||
| 127 | inherited. The methods are not idempotent: CSV streams can only be read once. | ||||||||||
| 128 | |||||||||||
| 129 | =head1 SEE ALSO | ||||||||||
| 130 | |||||||||||
| 131 | L<Catmandu::Exporter::TSV> | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||