/usr/share/perl5/Data/Page.pm is in libdata-page-perl 2.02-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | package Data::Page;
use Carp;
use strict;
use base 'Class::Accessor::Chained::Fast';
__PACKAGE__->mk_accessors(qw(total_entries entries_per_page current_page));
use vars qw($VERSION);
$VERSION = '2.02';
sub new {
my $class = shift;
my $self = {};
bless( $self, $class );
my ( $total_entries, $entries_per_page, $current_page ) = @_;
$self->total_entries( $total_entries || 0 );
$self->entries_per_page( $entries_per_page || 10 );
$self->current_page( $current_page || 1 );
return $self;
}
sub entries_per_page {
my $self = shift;
my $entries_per_page = $_[0];
if (@_) {
croak("Fewer than one entry per page!") if $entries_per_page < 1;
return $self->_entries_per_page_accessor(@_);
}
return $self->_entries_per_page_accessor();
}
sub current_page {
my $self = shift;
if (@_) {
return $self->_current_page_accessor(@_);
}
return $self->first_page unless defined $self->_current_page_accessor;
return $self->first_page
if $self->_current_page_accessor < $self->first_page;
return $self->last_page
if $self->_current_page_accessor > $self->last_page;
return $self->_current_page_accessor();
}
sub total_entries {
my $self = shift;
if (@_) {
return $self->_total_entries_accessor(@_);
}
return $self->_total_entries_accessor;
}
sub entries_on_this_page {
my $self = shift;
if ( $self->total_entries == 0 ) {
return 0;
} else {
return $self->last - $self->first + 1;
}
}
sub first_page {
my $self = shift;
return 1;
}
sub last_page {
my $self = shift;
my $pages = $self->total_entries / $self->entries_per_page;
my $last_page;
if ( $pages == int $pages ) {
$last_page = $pages;
} else {
$last_page = 1 + int($pages);
}
$last_page = 1 if $last_page < 1;
return $last_page;
}
sub first {
my $self = shift;
if ( $self->total_entries == 0 ) {
return 0;
} else {
return ( ( $self->current_page - 1 ) * $self->entries_per_page ) + 1;
}
}
sub last {
my $self = shift;
if ( $self->current_page == $self->last_page ) {
return $self->total_entries;
} else {
return ( $self->current_page * $self->entries_per_page );
}
}
sub previous_page {
my $self = shift;
if ( $self->current_page > 1 ) {
return $self->current_page - 1;
} else {
return undef;
}
}
sub next_page {
my $self = shift;
$self->current_page < $self->last_page ? $self->current_page + 1 : undef;
}
# This method would probably be better named 'select' or 'slice' or
# something, because it doesn't modify the array the way
# CORE::splice() does.
sub splice {
my ( $self, $array ) = @_;
my $top = @$array > $self->last ? $self->last : @$array;
return () if $top == 0; # empty
return @{$array}[ $self->first - 1 .. $top - 1 ];
}
sub skipped {
my $self = shift;
my $skipped = $self->first - 1;
return 0 if $skipped < 0;
return $skipped;
}
sub change_entries_per_page {
my ( $self, $new_epp ) = @_;
use integer;
croak("Fewer than one entry per page!") if $new_epp < 1;
my $new_page = 1 + ( $self->first / $new_epp );
$self->entries_per_page($new_epp);
$self->current_page($new_page);
}
1;
__END__
=head1 NAME
Data::Page - help when paging through sets of results
=head1 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";
=head1 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.
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.
=head1 METHODS
=head2 new
This is the constructor, which takes no arguments.
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);
=head2 total_entries
This method get or sets the total number of entries:
print "Entries:", $page->total_entries, "\n";
=head2 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";
=head2 current_page
This method gets or sets the current page number (which defaults to 1):
print "Page: ", $page->current_page, "\n";
=head2 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";
=head2 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";
=head2 last_page
This method returns the total number of pages of information:
print "Pages range to: ", $page->last_page, "\n";
=head2 first
This method returns the number of the first entry on the current page:
print "Showing entries from: ", $page->first, "\n";
=head2 last
This method returns the number of the last entry on the current page:
print "Showing entries to: ", $page->last, "\n";
=head2 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";
}
=head2 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";
}
=head2 splice
This method takes in a listref, and returns only the values which are
on the current page:
@visible_holidays = $page->splice(\@holidays);
=head2 skipped
This method is useful 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);
=head2 change_entries_per_page
This method changes the number of entries per page and the current page number
such that the L<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
=head1 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...
=head1 SEE ALSO
Related modules which may be of interest: L<Data::Pageset>,
L<Data::Page::Tied>, L<Data::SpreadPagination>.
=head1 AUTHOR
Based on code originally by Leo Lapworth, with many changes added by
by Leon Brocard <acme@astray.com>.
=head1 CONTRIBUTORS
James Laver (ELPENGUIN)
=head1 COPYRIGHT
Copyright (C) 2000-9, Leon Brocard
=head1 LICENSE
This module is free software; you can redistribute it or modify it
under the same terms as Perl itself.
|