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
|
package Catmandu::Exporter::Count;
use Catmandu::Sane;
use Catmandu::Util qw(is_hash_ref is_array_ref is_able);
our $VERSION = '1.2024';
use Moo;
use namespace::clean;
with 'Catmandu::Exporter';
# add is a noop since an Exporter is already a Counter
sub add { }
sub commit {
my $self = $_[0];
$self->fh->print($self->count . "\n");
}
# optimize counting
around add_many => sub {
my ($orig, $self, $many) = @_;
if (is_hash_ref($many)) {
$self->inc_count;
return 1;
}
if (is_array_ref($many)) {
my $n = scalar @$many;
$self->inc_count($n);
return $n;
}
if (is_able($many, 'count')) {
my $n = $many->count;
$self->inc_count($n);
return $n;
}
$orig->($self, $many);
};
1;
__END__
=pod
=head1 NAME
Catmandu::Exporter::Count - a exporter that counts things
=head1 SYNOPSIS
# From the commandline
$ catmandu convert JSON to Count < /tmp/data.json
=head1 DESCRIPTION
This exporter exports nothing and just counts the number of items found
in the input data.
=head1 SEE ALSO
L<Catmandu::Cmd::count>
L<Catmandu::Exporter::Null>
=cut
|