There's three modules I've used from CPAN for paging, Data::Page, Data::Pageset, and Data::SpreadPagination (the later two are subclasses of the first). One of the advantages of these modules is they work really well as something to pass into Template Toolkit.
These are three useful modules to add to your CPAN toolkit, each one does things a little differently and solves what at first looks like a simple problem, but saves you from both typing and embarassing off-by-one errors.
Data::Page by Leon Brocard
my $page = Data::Page->new($total_entries, $entries_per_page, $current_page); [% FOREACH page IN [page.first_page .. page.last_page] %] [% IF page == current_page %] [% page %] [% ELSE %] [% page %] [% END %] [% END %]
With total_entries = 100, entries_per_page = 10, current_page = 3, you'll get something looking like:
1 2 3 4 5 6 7 8 9 10
Data::Pageset by Leo Lapworth
One of the issues I ran into with Data::Page is that if you have a large number of pages then you have to somehow control the set of pages you output. Data::Pageset takes care of this for you. In this example $pages_per_set are the number of pages you want to display at any one time.
my $pageset = Data::Pageset->new({ total_entries => $total_entries, entries_per_page => $entries_per_page, current_page => $current_page, pages_per_set => $pages_per_set, }); [% IF pageset.previous_set %] < [% END %] [% FOREACH page IN pageset.pages_in_set %] [% IF page == current_page %] [% page %] [% ELSE %] [% page %] [% END %] [% END %] [% IF pageset.next_set %] < [% END %]
With total_entries = 1000, entries_per_page = 10, current_page = 13, pages_per_set = 10, you'll get something looking like:
< 11 12 13 14 15 16 17 189 20 >
Data::SpreadPagination by Jody Belka
This works in similar way to Data::Pageset, in that you're going to be showing a subset of the total number of pages. The difference is that Data::SpreadPagination allows you to output paging that allows the user to jump to the start and end of the results easily.
my $pagination = Data::SpreadPagination->new({ totalEntries => 1000, entriesPerPage => 10, currentPage => 13, maxPages => 10 }); [% FOREACH page IN pagination.pages_in_spread %] [% IF !page.defined %] ... [% ELSIF page == current_page %] [% page %] [% ELSE %] [% page %] [% END %] [% END %]
With the same data as before, you'll end up with:
1 2 ... 10 11 12 13 14 15 16 ...99 100
I was chatting with Rael Dornfest and asked him if he liked Perl. He said yes, but he was tired of the Perl mindset of "everybody having their pet solution." In Python, there are "SIGs" (Special Interest Groups) that are dedicated to taking a particular technology and making it work properly. As a result, you have a much greater consistency in code and you don't have to hunt around for the 30 ways to do the one thing. I have to admit, that sounds awfully nice even if I am one of the "pet solution" programmers
Re: Many solutions
osfameron on 2004-12-14T08:37:32
Sounds like a good idea. Sort of has equivalents now - DataTime project, DBI, at least 2 email projects (oops!)It would be good if search.cpan.org then linked to the relevant SIG for a module (which would presumably supply a resource page giving overview of the topic, comparisons of different modules, and other recommendations).
Re:Many solutions
gav on 2004-12-20T23:15:14
That sounds dreadful. The only thing committees create is mediocrity. I like the Perl model where it's messy and projects that have merit rise to the top. There's a whole bunch of modules on CPAN that are somebody's pet project for database abstraction, templating, etc, but there are only a couple that are good enough to achive widespread use.
These three modules show the strengths of the Perl model, there isn't a single class, but a base class, and two subclasses that build additional functionality. There's also others such as Data::Page::Tied, Class::DBI::Pager, etc. I'd like to see more subclasses like this on CPAN.Re:Many solutions
jhi on 2004-12-22T05:37:33
> That sounds dreadful. The only thing committees create is mediocrity. I like the Perl model where it's messy and projects that have merit rise to the top.
I no longer believe in this romantic fantasy.