NAME

Data::Page - help when paging through sets of results

SYNOPSIS

use Data::Page;

my $page = Data::Page->new($total_entries, $entries_per_page, $current_page);

print "         First page: ", $page->first_page, "\n";
print "          Last page: ", $page->last_page, "\n";
print "First entry on page: ", $page->first, "\n";
print " Last entry on page: ", $page->last, "\n";

DESCRIPTION

When searching through large amounts of data, it is often the case that a result set is returned that is larger than we want to display on one page. This results in wanting to page through various pages of data. The maths behind this is unfortunately fiddly, hence this module.

The main concept is that you pass in the number of total entries, the number of entries per page, and the current page number. You can then call methods to find out how many pages of information there are, and what number the first and last entries on the current page really are.

METHODS

new

This is the constructor. It currently takes two mandatory arguments, the total number of entries and the number of entries per page. It also optionally takes the current page number (which defaults to 1).

my $page = Data::Page->new($total_entries, $entries_per_page, $current_page);

my $page = Data::Page->new(134, 10);

my $page = Data::Page->new(134, 10, 5);

total_entries

This method returns the total number of entries.

print "Entries:", $page->total_entries, "\n";

entries_per_page

This method returns the total number of entries per page.

print "Per page:", $page->entries_per_page, "\n";

entries_on_this_page

This methods returns the number of entries on the current page.

print "There are ", $page->entries_on_this_page, " entries displayed\n";

current_page

This method returns the current page number.

print "Page: ", $page->current_page, "\n";

first_page

This method returns the first page. This is put in for reasons of symmetry with last_page, as it always returns 1.

print "Pages range from: ", $page->first_page, "\n";

last_page

This method returns the total number of pages of information.

print "Pages range to: ", $page->last_page, "\n";

first

This method returns the number of the first entry on the current page.

print "Showing entries from: ", $page->first, "\n";

last

This method returns the number of the last entry on the current page.

print "Showing entries to: ", $page->last, "\n";

previous_page

This method returns the previous page number, if one exists. Otherwise it returns undefined.

if ($page->previous_page) {
  print "Previous page number: ", $page->previous_page, "\n";
}

next_page

This method returns the next page number, if one exists. Otherwise it returns undefined.

if ($page->next_page) {
  print "Next page number: ", $page->next_page, "\n";
}

splice

This method takes in an listref, and returns only the values which are on the current page.

@visible_holidays = $page->splice(\@holidays);

NOTES

It has been said before that this code is "too simple" for CPAN, but I must disagree. I have seen people write this kind of code over and over again and they always get it wrong. Perhaps now they will spend more time getting the rest of their code right...

AUTHOR

Based on code originally by Leo Lapworth, with many changes added by by Leon Brocard <acme@astray.com>.

COPYRIGHT

Copyright (C) 2000-2, Leon Brocard

This module is free software; you can redistribute it or modify it under the same terms as Perl itself.