1Sort::MergeSort::IteratUosre(r3)Contributed Perl DocumenStoartti:o:nMergeSort::Iterator(3)
2
3
4
6 Sort::MergeSort::Iterator - iteration object
7
9 my $input = Sort::MergeSort::Iterator->new( sub { ... } );
10
11 while (<$input>) {
12 }
13
14 while (my $i = $input->next) {
15 }
16
17 my $ahead = $input->peek;
18
20 This class implements a simple iterator interface for iterating over
21 items. Just pass a code reference to the constructor that returns the
22 next value over which to iterate. Sort::MergeSort::Iterator will do the
23 rest.
24
25 FileHandle Interface
26 You can pretend that an Iterator object is a filehandle. It doesn't
27 support all filehandle operations, but it does allow you to iterate in
28 the natural way:
29
30 while (<$input>) {
31 }
32
33 Instance Methods
35 Constructors
36 new
37
38 my $iter = Sort::MergeSort::Iterator->new(\&code_ref);
39 my $iter = Sort::MergeSort::Iterator->new(\&code_ref, \&destroy_code_ref);
40
41 Constructs and returns a new iterator object. The code reference passed
42 as the first argument is required and must return the next item to
43 iterate over each time it is called, and "undef" when there are no more
44 items. "undef" cannot itself be an item. The optional second argument
45 must also be a code reference, and will only be executed when the
46 iterator object is destroyed (that is, it will be called in the
47 DESTROY() method).
48
49 current
50
51 my $current = $iter->current;
52
53 Returns the current item in the iterator--that is, the same item
54 returned by the most recent call to next().
55
56 next
57
58 while (my $thing = $iter->next) {
59 # Do something with $thing.
60 }
61
62 Returns the next item in the iterator. Returns "undef" when there are
63 no more items to iterate.
64
65 peek
66
67 while ($iter->peek) {
68 $iter->next;
69 }
70
71 Returns the next item to be returned by next() without actually
72 removing it from the list of items to be returned. The item returned by
73 peek() will be returned by the next call to next(). After that, it will
74 not be available from peek() but the next item will be.
75
76 position
77
78 my $pos = $iter->position;
79
80 Returns the current position in the iterator. After the first time
81 next() is called, the position is set to 1. After the second time, it's
82 set to 2. And so on. If next() has never been called, position()
83 returns 0.
84
85 all
86
87 for my $item ($iter->all) {
88 print "Item: $item\n";
89 }
90
91 Returns a list or array reference of all of the items to be returned by
92 the iterator. If next() has been called prior to the call to all(),
93 then only the remaining items in the iterator will be returned. Use
94 this method with caution, as it could cause a large number of items to
95 be loaded into memory at once.
96
97 do
98
99 $iter->do( sub { print "$_[0]\n"; return $_[0]; } );
100 $iter->do( sub { print "$_\n"; return $_; } );
101
102 Pass a code reference to this method to execute it for each item in the
103 iterator. Each item will be set to $_ before executing the code
104 reference, and will also be passed as the sole argument to the code
105 reference. If next() has been called prior to the call to do(), then
106 only the remaining items in the iterator will passed to the code
107 reference. Iteration terminates when the code reference returns false,
108 so be sure to have it return a true value if you want it to iterate
109 over every item.
110
112 David Wheeler <wheeler@searchme.com>
113
115 Copyright (c) 2004-2008 Kineticode, Inc. <info@kineticode.com>.
116
117 This work was based on Object::Relation::Iterator module developed by
118 Kineticode, Inc. As such, it is is free software; it can be
119 redistributed it and/or modified it under the same terms as Perl
120 itself.
121
122
123
124perl v5.36.0 2023-01-20 Sort::MergeSort::Iterator(3)