John Drago > Iterator-Paged > Iterator::Paged

Download:
Iterator-Paged-1.001.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  1
Open  0
View/Report Bugs
Module Version: 1.001   Source  

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.

PUBLIC PROPERTIES ^

next

Returns the next record.

page_number

Gets the current page number the iterator is on.

PUBLIC METHODS ^

reset

Sets the page number and internal index to 0.

ABSTRACT METHODS ^

The following methods should be implemented by subclasses of Iterator::Paged.

next_page( )

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:

  # No records found:
  return;

PROTECTED METHODS ^

The following methods may be implemented by subclasses of Iterator::Paged.

_init( )

If your iterator class should be initialized after new and before next_page, use _init for that purpose.

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.

syntax highlighting: