Data::Page(3pm)
π Data::Page β the Perl manual, but make it make sense π
okay so bestie, this is the manual page for Data::Page (that's the Data::Page(3pm) you're holding rn) β the Perl module that does your paging maths for you so you never have to count on your fingers again. it's giving "I have 4,000 rows and I only wanna show 20 at a time". let's get into it. π¦
β¨ NAME β¨
Data::Page β help when paging through sets of results π
like. it helps you page. through sets of results. that's the whole vibe.
β¨ SYNOPSIS β¨
use Data::Page;
my $page = Data::Page->new();
$page->total_entries($total_entries);
$page->entries_per_page($entries_per_page);
$page->current_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";
see how it just works? π no cap.
β¨ DESCRIPTION β¨
so like, when you're searching through a huge amount of data, sometimes you get back a result set that is wayyy bigger than what you actually wanna display on one page. and then you're like "okay cool I guess we're paging through this". the maths behind all of that is fiddly (genuinely, it's a nightmare, ask anyone) β hence this module. π
the main concept is simple, bestie: you hand over the number of total entries, the number of entries per page, and the current page number. then you can 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. like, the actual numbers. β¨
for example β say we wished to page through the integers from 1 to 100 with 20 entries per page. the first page would consist of 1-20, the second page from 21-40, the third page from 41-60, the fourth page from 61-80 and the fifth page from 81-100. this module would help you work this out. π§ and you would not have to cry about it.
β¨ METHODS β¨
π«Ά new
this is the constructor, and it takes no arguments. iconic behavior honestly.
my $page = Data::Page->new();
there is also an old, deprecated constructor, which 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:
my $page = Data::Page->new($total_entries, $entries_per_page, $current_page);
β οΈ bestie, that second form is deprecated. it still works but it's giving legacy. use the no-arg one and set things with the setters, mmkay?
π«Ά total_entries
this method gets or sets the total number of entries:
print "Entries:", $page->total_entries, "\n";
π«Ά entries_per_page
this method gets or sets the total number of entries per page (which defaults to 10):
print "Per page:", $page->entries_per_page, "\n";
π«Ά current_page
this method gets or sets the current page number (which defaults to 1):
print "Page: ", $page->current_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";
π«Ά first_page
this method returns the first page. it's put in for reasons of symmetry with last_page, because it always returns 1:
print "Pages range from: ", $page->first_page, "\n";
yes it's just 1. yes it's still useful. don't @ me. π
π«Ά 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";
}
that if is doing SO much heavy lifting btw. respect it. π
π«Ά 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 a listref, and returns only the values which are on the current page:
@visible_holidays = $page->splice(\@holidays);
obsessed with the variable name @visible_holidays fr fr. ποΈ
π«Ά skipped
this method is useful for paging through data in a database using SQL LIMIT clauses. it is simply $page->first - 1:
$sth = $dbh->prepare(
q{SELECT * FROM table ORDER BY rec_date LIMIT ?, ?}
);
$sth->execute($page->skipped, $page->entries_per_page);
this is the one that saves your database from an absolute meltdown. π chef's kiss.
π«Ά change_entries_per_page
this method changes the number of entries per page and the current page number such that the first item on the current page will be present on the new page.
$page->total_entries(50);
$page->entries_per_page(20);
$page->current_page(3);
print $page->first; # 41
$page->change_entries_per_page(30);
print $page->current_page; # 2 - the page that item 41 will show in
the way that item 41 just follows you to the new page?? main character energy honestly. π
β¨ 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...
π the way this note has been holding a grudge since forever. and it's CORRECT. everybody thinks paging is easy and then everybody's off-by-one. every single time. periodt.
β¨ SEE ALSO β¨
related modules which may be of interest: Data::Pageset, Data::Page::Tied, Data::SpreadPagination.
β¨ AUTHOR β¨
based on code originally by Leo Lapworth, with many changes added by by Leon Brocard <acme@astray.com>. π«Ά
(and yes, the "by by" is in the original. I'm keeping it. it's iconic now.)
β¨ CONTRIBUTORS β¨
James Laver (ELPENGUIN) π§
β¨ COPYRIGHT β¨
Copyright (C) 2000-9, Leon Brocard
β¨ LICENSE β¨
this module is free software; you can redistribute it or modify it under the same terms as Perl itself. πΈ
okay that's the whole page bestie, you're officially a paging expert, go forth and paginate responsibly πβ¨
perl v5.30.0 β 2019-11-17 β Data::Page(3pm) β that's the version and the date this lil manual came from, stamped right at the bottom like a lil time capsule. slay. ππ¦π
π¦ where this page comes from
- debian bookworm: libdata-page-perl 2.03-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libdata-page-perl 2.03-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libdata-page-perl 2.03-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libdata-page-perl 2.03-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms