1MooseX::Iterator(3) User Contributed Perl Documentation MooseX::Iterator(3)
2
3
4
6 MooseX::Iterator - Iterate over collections
7
9 Access the Iterator directly:
10
11 use Moose;
12 use MooseX::Iterator;
13
14 my $iter = MooseX::Iterator::Array->new( collection => [ 1, 2, 3, 4, 5, 6 ] );
15
16 my $count = 1;
17 while ( $iter->has_next ) {
18 print $iter->next;
19 }
20
21 Or use the meta class:
22
23 package TestIterator;
24
25 use Moose;
26 use MooseX::Iterator;
27
28 has collection => (
29 is => 'ro',
30 isa => 'HashRef',
31 default => sub { { one => '1', two => '2', three => '3' } },
32 );
33
34 has iter => (
35 metaclass => 'Iterable',
36 iterate_over => 'collection',
37 );
38
39 no Moose;
40
41 package main;
42 use Data::Dumper;
43
44 my $test = TestIterator->new;
45
46 my $iter = $test->iter;
47
48 while ( $iter->has_next ) {
49 my $next = $iter->next;
50 print $next->{'key'} . "\n";
51 print $next->{'value'} . "\n";
52 }
53
55 This is an attempt to add smalltalk-like streams to Moose. It currently
56 works with ArrayRefs and HashRefs.
57
58 next
59 The next method provides the next item in the colletion.
60
61 For arrays it returns the element of the array
62
63 For hashs it returns a pair as a hashref with the keys: key and value
64
65 has_next
66 The has_next method is a boolean method that is true if there is
67 another item in the colletion after the current item. and falue if
68 there isn't.
69
70 peek
71 The peek method returns the next item without moving the state of
72 the iterator forward. It returns undef if it is at the end of the
73 collection.
74
75 reset
76 Resets the cursor, so you can iterate through the elements again.
77
78 Subclassing MooseX::Iterator::Meta::Iterable
79 When subclassing MooseX::Iterator::Meta::Iterable for your own
80 iterators override
81 MooseX::Iterator::Meta::Iterable::_calculate_iterator_class_for_type
82 to returns the name of the class that iterates over your new
83 collection type. The class must implement the
84 MooseX::Iterator::Role role.
85
87 Robert Boone <rlb@cpan.org>
88
89 And thank you to Steven Little (steven) and Matt Trout (mst) for the
90 help and advice they gave.
91
93 Johannes Plunien
94
96 Git - http://github.com/rlb3/moosex-iterator/tree/master
97
99 This library is free software; you can redistribute it and/or modify it
100 under the same terms as Perl itself.
101
103 Hey! The above document had some coding errors, which are explained
104 below:
105
106 Around line 96:
107 '=item' outside of any '=over'
108
109
110
111perl v5.32.0 2020-07-28 MooseX::Iterator(3)