/usr/share/perl5/Catmandu/ArrayIterator.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 | package Catmandu::ArrayIterator;
use namespace::clean;
use Catmandu::Sane;
use Catmandu::Util qw(check_array_ref);
use Role::Tiny::With;
with 'Catmandu::Iterable';
sub new {
bless check_array_ref($_[1]), $_[0];
}
sub generator {
my ($self) = @_;
my $i = 0;
sub {
$self->[$i++];
};
}
sub to_array {
[@{$_[0]}];
}
sub count {
scalar @{$_[0]};
}
sub each {
my ($self, $cb) = @_;
$cb->($_) for @$self;
$self->count;
}
sub first {
$_[0]->[0];
}
1;
=head1 NAME
Catmandu::ArrayIterator - Convert an arrayref to an Iterable object
=head1 SYNOPSIS
use Catmandu::ArrayIterator;
my $it = Catmandu::ArrayIterator->new([{n => 1}, {n => 2}, {n => 3}]);
$it->each( sub {
my $item = $_[0];
# Very complicated routine
....
});
$it->[0];
# => {n => 1}
$it->first;
# => {n => 1}
$it->map(sub { $_[0]->{n} + 1 })->to_array;
# => [2, 3, 4]
$it->count
# => 3
=head1 METHODS
=head2 new($arrayRef)
Create a new iterator object from $arrayRef.
=head2 to_array
Return all the items in the Iterator as an ARRAY ref.
=head2 each(\&callback)
For each item in the Iterator execute the callback function with the item as first argument. Returns
the number of items in the Iterator.
=head2 count
Return the count of all the items in the Iterator.
=head2 first
Return the first item from the Iterator.
=head1 SEE ALSO
L<Catmandu::Iterable>, L<Catmandu::Iterator>
=cut
|