/usr/share/perl5/Catmandu/Importer/CSV.pm is in libcatmandu-perl 0.9206-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Catmandu::Importer::CSV;
use namespace::clean;
use Catmandu::Sane;
use Text::CSV;
use Moo;
with 'Catmandu::Importer';
has csv => (is => 'ro', lazy => 1, builder => '_build_csv');
has sep_char => (is => 'ro', default => sub { ',' });
has quote_char => (is => 'ro', default => sub { '"' });
has escape_char => (is => 'ro', default => sub { '"' });
has allow_loose_quotes => (is => 'ro', default => sub { 0 });
has allow_loose_escapes => (is => 'ro', default => sub { 0 });
has header => (is => 'ro', default => sub { 1 });
has fields => (
is => 'rw',
coerce => sub {
my $fields = $_[0];
if (ref $fields eq 'ARRAY') { return $fields }
if (ref $fields eq 'HASH') { return [sort keys %$fields] }
return [split ',', $fields];
},
);
sub _build_csv {
my ($self) = @_;
Text::CSV->new({
binary => 1,
sep_char => $self->sep_char,
quote_char => $self->quote_char ? $self->quote_char : undef,
escape_char => $self->escape_char ? $self->escape_char : undef,
allow_loose_quotes => $self->allow_loose_quotes,
allow_loose_escapes => $self->allow_loose_escapes,
});
}
sub generator {
my ($self) = @_;
sub {
state $fh = $self->fh;
state $csv = do {
if ($self->header) {
if ($self->fields) {
$self->csv->getline($fh);
} else {
$self->fields($self->csv->getline($fh));
}
}
$self->csv->column_names($self->fields);
$self->csv;
};
$csv->getline_hr($fh);
};
}
=head1 NAME
Catmandu::Importer::CSV - Package that imports CSV data
=head1 SYNOPSIS
use Catmandu::Importer::CSV;
my $importer = Catmandu::Importer::CSV->new(file => "/foo/bar.csv");
my $n = $importer->each(sub {
my $hashref = $_[0];
# ...
});
=head1 METHODS
=head2 new(file => $filename, fh = $fh, fields => \@fields, quote_char => "\"", sep_char => ",", fix => [...])
Create a new CSV importer for $filename. Use STDIN when no filename is given. The
object fields are read from the CSV header line or given via the 'fields' parameter.
Strings in CSV are quoted by 'quote_char' and fields are separated by 'sep_char'.
The constructor inherits the fix parameter from L<Catmandu::Fixable>. When given,
then ech fix or fix script will be applied to imported items.
=head2 count
=head2 each(&callback)
=head2 ...
Every L<Catmandu::Importer> is a L<Catmandu::Iterable> all its methods are
inherited. The Catmandu::Importer::CSV methods are not idempotent: CSV streams
can only be read once.
=head1 SEE ALSO
L<Catmandu::Iterable>
=cut
1;
|