Quick and dirty example. It takes a parameter 'current', that keeps track which page you're looking at (for now, a regular variable so you can test the script in a shell).
I used an array instead of a database because I don't have any databases running here, but the point should be the same. Just skip the initialization part, and don't forget to add input checks!
#! /usr/bin/perl -Tw
#
# -wvh- quick and dirty pager example
#
use strict;
my ($i, $current, $start_limit, $count);
my (@hello, %param);
# initiate variables (you use db code)
$count = 200;
$start_limit = 20;
$param{'current'} = 46; # <-- test here to simulate page views
# initiate array with page contents
for ($i = 0; $i <= $count; $i++) {
$hello[$i] = "This is record $i";
}
##### cut here #####
$current = $param{'current'}; # <-- do input checks here!
$current = 0 if (! $param{'current'});
# optional: round to closest number if input number has been manually
# changed to a number not divisable by $start_limit
if ($current % $start_limit != 0 && $current != 0) {
$current -= $current % $start_limit; }
print "$count matches found for your query: \n";
# print pager
for ($i = 0; $i <= $count; $i += $start_limit) {
if ($i != $current) {
print "<a href=\"query.cgi?current=$i\">$i</a>"; }
else {
print $i; }
print " | " if ($i <= ($count - $start_limit));
}
print "\n";
# print records for current page
for ($i = $current; $i < ($current + $start_limit); $i++) {
print $hello[$i] . "\n" if ($i <= $count);
}
Feel free to ask questions or point out mistakes. ;)
Modified a small error with the pager seperators if the number of results wasn't divisable by start_limit.