File: Iterator.pm

package info (click to toggle)
libcatmandu-perl 1.2024-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,552 kB
  • sloc: perl: 17,037; makefile: 24; sh: 1
file content (74 lines) | stat: -rw-r--r-- 983 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
package Catmandu::Iterator;

use Catmandu::Sane;

our $VERSION = '1.2024';

use Role::Tiny::With;
use namespace::clean;

with 'Catmandu::Iterable';

sub new {
    bless $_[1], $_[0];
}

sub generator {
    goto &{$_[0]};
}

1;

__END__

=pod

=head1 NAME

Catmandu::Iterator - Base class for all Catmandu iterators

=head1 SYNOPSIS

  package My::MockIterator;

  use Catmandu;
  use Moo;

  with 'Catmandu::Iterable';

  sub generator {
    sub {
        # Generator some random data
        +{ random => rand };
    }
  }

  package main;

  my $it = My::MockIterator->new;

  my $first = $it->first;

  $it->each(sub {
  my $item = shift;

  print $item->{random} , "\n";
  });

  my $it2 = $it->map(sub { shift->{random} * 2 });

=head1 METHODS

=head2 generator

Should return a closure that generates one Perl hash.

=head1 INHERIT

If you provide a generator, then the class will generator all methods from L<Catmandu::Iterable>.

=head1 SEE ALSO

L<Catmandu::Iterable>

=cut