Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I currently have a need to paginate a database query on my website, kind of like search results. I am using HTML::Template so I thought a good module choice to help me with this would be HTML::Page. I am running into a couple of problems though. When I try to use the sample code from the CPAN site, I get the following error:

syntax error at /var/www/fd/entries.pl line 20, near "my " Execution of /var/www/fd/entries.pl aborted due to compilation errors.

Here is the code:

#!/usr/bin/perl use HTML::Pager; use CGI; my $query = CGI->new(); # create a callback subroutine to generate the data to be paged my $get_data_sub = sub { my ($offset, $rows) = @_; my @return_array; for (my $x = 0; $x < $rows; $x++) { push(@return_array, [ time() ]); } return \@return_array; } # create a Pager object my $pager = HTML::Pager->new( # required parameters query => $query, get_data_callback => $get_data_sub, rows => 100, page_size => 10, debug => 1, ); # make it go - send the results to the browser. print $pager->output;

Now I know that there is nothing in the code about HTML::Template and sending the output to a .tmpl file, but I was kind of expecting an error relating to that, and not the error that I have listed above. What is wrong? I have googled this too.

Once I do get this to work, how do I display results 1-10 on the first page by default with no user input? Do I make the variables $offset and $rows a default value?

I appreciate everyone's help.
Thank you.

Replies are listed 'Best First'.
Re: Pagination with in HTML::Template
by PodMaster (Abbot) on May 01, 2005 at 16:48 UTC
    You're missing a semicolon.
    perl -e"my $ERROR = sub {} my $EEEEK = 1;" --------------------------^

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      That worked, thank you.

      Now how do I populate that with data? I am trying this:

      my $dbh = DBI->connect( "dbi:xxx:xxx", "xxxx", 'xxxx'); my $sth = $dbh->prepare('SELECT title, url FROM fd_ent +ries ORDER BY timestamp DESC LIMIT 200'); $sth->execute(); # create a callback subroutine to generate the data to be paged my $get_data_sub = sub { my ($offset, $rows) = @_; #my @return_array; my $return_array = $sth->fetchrow_array(); for (my $x = 0; $x < $rows; $x++) { push(@return_array, [ $data[$offset + $x]{title}, $data[$offset + $x]{url} ] ); } return \@return_array; } ;
      I know that is way off, but I don't how how far.

      Thank you.

Re: Pagination with in HTML::Template
by eibwen (Friar) on May 01, 2005 at 16:41 UTC

    I believe your error lies in the trailing comma in your $pager definition.

    Additionally, don't forget to use strict; and use warnings (or -w). If you can't make sense of perl's error messages, try use diagnostics;

    To return a subset of the results, you're probably looking for an array splice.

    UPDATE: I didn't (initially) have a copy of HTML::Pager to parse this through strict and warnings. As PodMaster pointed out, the problem lies in the omitted ; Nevertheless, use of strict and warnings would likely have precluded this problem in the first place:

    
    Global symbol "$pager" requires explicit package name at tmp line 36.
    tmp had compilation errors (#1)
        (F) Probably means you had a syntax error.  Common reasons include:
    
            A keyword is misspelled.
            A semicolon is missing.
            A comma is missing.
            An opening or closing parenthesis is missing.
            An opening or closing brace is missing.
            A closing quote is missing.