File: TSV.pm

package info (click to toggle)
libcatmandu-perl 1.2020-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,552 kB
  • sloc: perl: 16,933; makefile: 24; sh: 1
file content (133 lines) | stat: -rw-r--r-- 2,783 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package Catmandu::Importer::TSV;

use Catmandu::Sane;

our $VERSION = '1.2020';

use Catmandu::Importer::CSV;
use Moo;
use namespace::clean;

with 'Catmandu::Importer';

has header   => (is => 'ro', default => sub {1});
has sep_char => (
    is      => 'ro',
    default => sub {"\t"},
    coerce  => sub {
        my $sep_char = $_[0];
        $sep_char =~ s/(\\[abefnrt])/"qq{$1}"/gee;
        return $sep_char;
    }
);
has fields => (
    is     => 'rwp',
    coerce => sub {
        my $fields = $_[0];
        if (ref $fields eq 'ARRAY') {return $fields}
        if (ref $fields eq 'HASH')  {return [sort keys %$fields]}
        return [split ',', $fields];
    },
);

has csv => (is => 'lazy');

sub _build_csv {
    my ($self) = @_;
    my $csv = Catmandu::Importer::CSV->new(
        header      => $self->header,
        sep_char    => $self->sep_char,
        quote_char  => undef,
        escape_char => undef,
        file        => $self->file,
    );
    $csv->{fields} = $self->fields;
    $csv;
}

sub generator {
    my ($self) = @_;
    $self->csv->generator;
}

1;

__END__
=pod

=head1 NAME

Catmandu::Importer::TSV - Package that imports tab-separated values

=head1 SYNOPSIS

    # From the command line

    # convert a TSV file to JSON
    catmandu convert TSV to JSON < journals.tab

    # Or in a Perl script

    use Catmandu;

    my $importer = Catmandu->importer('TSV', file => "/foo/bar.tab");

    my $n = $importer->each(sub {
        my $hashref = $_[0];
        # ...
    });

=head1 DESCRIPTION

This package imports tab-separated values (TSV).  The object
fields are read from the TSV header line or given via the C<fields> parameter.

=head1 CONFIGURATION

=over

=item file

Read input from a local file given by its path. Alternatively a scalar
reference can be passed to read from a string.

=item fh

Read input from an L<IO::Handle>. If not specified, L<Catmandu::Util::io> is used to
create the input stream from the C<file> argument or by using STDIN.

=item encoding

Binmode of the input stream C<fh>. Set to C<:utf8> by default.

=item fix

An ARRAY of one or more fixes or file scripts to be applied to imported items.

=item fields

List of fields to be used as columns, given as array reference, comma-separated
string, or hash reference. If C<header> is C<0> and C<fields> is C<undef> the
fields will be named by column index ("0", "1", "2", ...).

=item header

Read fields from a header line with the column names, if set to C<1> (the
default).

=item sep_char

Column separator (C<tab> by default)

=back

=head1 METHODS

Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are
inherited.  The methods are not idempotent: CSV streams can only be read once.

=head1 SEE ALSO

L<Catmandu::Exporter::TSV>

=cut