The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Iterator::Paged - Simple iterator with events for accessing more records.

SYNOPSIS

  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

DESCRIPTION

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.

AUTHOR

John Drago <jdrago_999@yahoo.com>

COPYRIGHT

Copyright 2009 John Drago <jdrago_999@yahoo.com> all rights reserved.

LICENSE

This software is free software and may be used and redistributed under the same terms as Perl itself.