Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

gav (2710)

gav
  (email not shown publicly)
http://www.estey.com/
AOL IM: flufflegavin (Add Buddy, Send Message)

Hacker in NYC.

Journal of gav (2710)

Monday December 13, 2004
06:03 PM

Paging Modules on CPAN

[ #22293 ]

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 %]
        <b>[% page %]</b>
    [% ELSE %]
        <a href="?page=[% page %]">[% page %]</a>
    [% 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 %]
    <a href="?page=[% page.previous_page %]">&lt;</a>
[% END %]
[% FOREACH page IN pageset.pages_in_set %]
    [% IF page == current_page %]
        <b>[% page %]</b>
    [% ELSE %]
        <a href="?page=[% page %]">[% page %]</a>
    [% END %]
[% END %]
[% IF pageset.next_set %]
    <a href="?page=[% page.next_set %]">&lt;</a>
[% 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 18 9 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 %]
        <b>[% page %]</b>
    [% ELSE %]
        <a href="?page=[% page %]">[% page %]</a>
    [% END %]
[% END %]

With the same data as before, you'll end up with:

1 2 ... 10 11 12 13 14 15 16 ... 99 100

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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" progr

    • 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).

      --

      osfameron

    • 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. Ther
      • > 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.