line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Importer::TSV; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
106352
|
use Catmandu::Sane; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
7
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
485
|
use Catmandu::Importer::CSV; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
37
|
|
8
|
1
|
|
|
1
|
|
8
|
use Moo; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
393
|
use namespace::clean; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
13
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Importer'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has header => (is => 'ro', default => sub {1}); |
14
|
|
|
|
|
|
|
has sep_char => ( |
15
|
|
|
|
|
|
|
is => 'ro', |
16
|
|
|
|
|
|
|
default => sub {"\t"}, |
17
|
|
|
|
|
|
|
coerce => sub { |
18
|
|
|
|
|
|
|
my $sep_char = $_[0]; |
19
|
|
|
|
|
|
|
$sep_char =~ s/(\\[abefnrt])/"qq{$1}"/gee; |
20
|
|
|
|
|
|
|
return $sep_char; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
has fields => ( |
24
|
|
|
|
|
|
|
is => 'rwp', |
25
|
|
|
|
|
|
|
coerce => sub { |
26
|
|
|
|
|
|
|
my $fields = $_[0]; |
27
|
|
|
|
|
|
|
if (ref $fields eq 'ARRAY') {return $fields} |
28
|
|
|
|
|
|
|
if (ref $fields eq 'HASH') {return [sort keys %$fields]} |
29
|
|
|
|
|
|
|
return [split ',', $fields]; |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
has csv => (is => 'lazy'); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _build_csv { |
36
|
3
|
|
|
3
|
|
32
|
my ($self) = @_; |
37
|
3
|
|
|
|
|
54
|
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
|
|
|
|
|
22
|
$csv->{fields} = $self->fields; |
45
|
3
|
|
|
|
|
59
|
$csv; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub generator { |
49
|
|
|
|
|
|
|
my ($self) = @_; |
50
|
|
|
|
|
|
|
$self->csv->generator; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
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 |