Iterator::Paged - Simple iterator with events for accessing more records.
use Iterator::Paged; my $iter = Iterator::Paged->new(); while( my $item = $iter->next ) { warn $iter->page_number . ": $item\n"; last if $iter->page_number > 100; }# end while()
Or, more likely, in a subclass:
package My::Iterator; use strict; use warnings 'all'; use base 'Iterator::Paged'; sub next_page { my ($s) = @_; # Return an arrayref of the next "page" of data: return if $s->{page_number}++ >= 4; return [ $s->{idx}.. $s->{idx} + 5 ]; }# end get_page()
Then, using that class:
use My::Iterator; my $iter = My::Iterator->new(); while( my $item = $iter->next ) { warn "Page " . $iter->page_number . ": $item\n"; }# end while()
That last example will print the following:
Page 1: 0 Page 1: 1 Page 1: 2 Page 1: 3 Page 1: 4 Page 1: 5 Page 2: 6 Page 2: 7 Page 2: 8 Page 2: 9 Page 2: 10 Page 2: 11 Page 3: 12 Page 3: 13 Page 3: 14 Page 3: 15 Page 3: 16 Page 3: 17 Page 4: 18 Page 4: 19 Page 4: 20 Page 4: 21 Page 4: 22 Page 4: 23
Iterator::Paged provides a simple (subclassable) iterator that will attempt to fetch the next "page" of results when the current set is exhausted.
For example, suppose you have an iterator for results on Google.com that fetches the first page of results and upon the next call to next fetches the second page, then third page, fourth and so on.
next
Returns the next record.
Gets the current page number the iterator is on.
Sets the page number and internal index to 0.
0
The following methods should be implemented by subclasses of Iterator::Paged.
This method should somehow fetch the next "page" of records and upon success, return an arrayref of records.
If no more records are available, the method should return undef like so:
undef
# No records found: return;
The following methods may be implemented by subclasses of Iterator::Paged.
If your iterator class should be initialized after new and before next_page, use _init for that purpose.
new
next_page
_init
John Drago <jdrago_999@yahoo.com>
Copyright 2009 John Drago <jdrago_999@yahoo.com> all rights reserved.
This software is free software and may be used and redistributed under the same terms as Perl itself.
To install Iterator::Paged, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Iterator::Paged
CPAN shell
perl -MCPAN -e shell install Iterator::Paged
For more information on module installation, please visit the detailed CPAN module installation guide.