/usr/share/perl5/Catmandu/TabularExporter.pm is in libcatmandu-perl 1.0700-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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package Catmandu::TabularExporter;
use Catmandu::Sane;
our $VERSION = '1.07';
use Catmandu::Util qw(:is :check);
use Moo::Role;
sub _coerce_array {
my $fields = $_[0];
if (ref $fields eq 'ARRAY') {return $fields}
if (ref $fields eq 'HASH') {return [sort keys %$fields]}
[split ',', $fields];
}
use namespace::clean;
with 'Catmandu::Exporter';
has fields => (is => 'rwp', coerce => \&_coerce_array,);
has columns => (is => 'rwp', coerce => \&_coerce_array,);
has collect_fields => (is => 'ro',);
has header => (is => 'ro', default => sub {1});
around add => sub {
my ($orig, $self, $data) = @_;
$self->_set_fields($data) unless $self->fields;
$orig->($self, $data);
};
around add_many => sub {
my ($orig, $self, $many) = @_;
if ($self->collect_fields && !$self->fields) {
my $coll;
if (is_array_ref($many)) {
$coll = $many;
}
elsif (is_hash_ref($many)) {
$coll = [$many];
}
else {
if (is_invocant($many)) {
$many = check_able($many, 'generator')->generator;
}
check_code_ref($many);
$coll = [];
while (defined(my $data = $many->())) {
push @$coll, $data;
}
}
my $keys = {};
for my $data (@$coll) {
for my $key (keys %$data) {
$keys->{$key} ||= 1;
}
}
$self->_set_fields($keys);
$many = $coll;
}
$orig->($self, $many);
};
1;
__END__
=pod
=head1 NAME
Catmandu::TabularExporter - base role for tabular exporters like CSV
=head1 DESCRIPTION
See L<Catmandu::Exporter> for the base functionality of this role. This role
adds some functionality tailored to tabular or columnar exporters.
=head1 CONFIGURATION
=over
=item fields
The fields to be mapped. Can be an arrayref, example hashref or comma
separated string. If missing, the fields of the first record encountered will
be used. If C<collect_fields> is true, all fields names in the record stream
will be collected first.
=item columns
Optional custom column labels. Can be an arrayref, example hashref or comma
separated string.
=item collect_fields
See C<fields> for a description. Note that this option will cause all records
in the stream to be buffered in memory.
=item header
Include a header with column names. Enabled by default.
=back
=cut
|