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