Pagination help please

Hi, I’m running a list of a flat file db in a Perl script and would like to paginate it. I don’t want to use a module as it probably would be overkill as there will be no more than 70-80 records. A next/back solution will probably be the most likely thing.

How would I do that?

sub arcin {
if ( ($QS{'action'} eq "thisarc") && ($QS{'mdb'} =~ /[0-9999999]/) ) {
 $themdb = $QS{'mdb'}; $fl = "$datdir/2000.db";

print qq ~<div>~;

open (RFL,"$fl") || die("Cannot open $fl");
 @rd = <RFL>;
close (RFL);

 foreach $line (@rd) {
  @fd=split /\\|/, "$line";
  $flp = "$fd[1]";
  $sbt = "$fd[2]";
  print qq ~<h2>$sbt</h2>~;
  print qq ~<div>~;
  $line =~ s/ /\\&nbsp\\;/g;
   if ($archshow eq Full) {
   print qq ~$fd[3]~;}
    else {
     @words = split(/\\s+/,$fd[3]);
     $ash = join(" ",@words[0..11]);
   print qq ~$ash.....~;
   print qq ~<br></div><br><br>~;
print qq ~</div>~;


} else {print "Invalid String!\
"; exit;} # end QS

Hope someone can help - thanks

What do your input data and the desired output look like? There’s not really enough information in your post to answer it, unless you’re just asking “how do I insert a break every n lines?”, which is just a matter of

my $lines_per_page = 10;
my $line_counter;

print '<div>';
for my $line (@rd) {
  print $line;
  if ($line_counter >= $lines_per_page) {
    print '</div><div>';
    $line_counter = 0;
print '</div>';

A few style tips, though:

  • Always start your Perl code with
    use strict; use warnings;
    It will be painful to start with, as you clean up all the problems they find, but it will help immensely in the long run. Don’t omit them or turn them off unless a) you know exactly what they each do, b) you can coherently explain why a certain section of code needs one/both of them to be turned off and c) you know how to turn it off for only that section of code. a) and c) are easy; b) doesn’t happen all that often.
  • Don’t prefix function calls with ‘&’ unless you have a specific reason to - it’s a holdover from Perl 4 and has non-obvious side-effects. Use “qsParse();” instead of “&qsParse;”.
  • You should use lexical file handles and the three-argument form of “open”. Lexical filehandles help to avoid problems with one part of the program messing up another part’s data, while three-arg open prevents security problems. In practical terms, this means changing
    open(RFL, "$fl") || die("Cannot open $fl"); @rd = <RFL>; close(RFL);
    open(my $rfl, '<', $fl) || die("Cannot open $fl"); @rd = <$rfl>; close($rfl);

  • Although they don’t hurt anything, the double quotes in
    @fd = split /\\|/, "$line"; $flp = "$fd[1]"; $sbt = "$fd[2]";
    print qq ~$fd[3]~;
    are useless. There’s no reason to put quotes around a variable unless there’s something else inside the quotes along with it.

Hi dsheroh, thanks for replying…

It will be painful to start with, as you clean up all the problems

I will attend to this, but it may take some time :wink:

I understand about the use strict, but the scripts are from an old forum that I have converted into a blog and incorporated flat-file databases - and it didn’t have that. The only errors I ever see in my editor are those where a variable is only used once in that particular script. This is because they are all defined in a config file and the editor is not relating to that.

I’ll go thru it all I could do a search and replace and put my in front of all the $'s and @'s - hmmmm.

The way files are opened: I didn’t know that was Perl 4, only that it worked. Security wise is a good idea.

The blog is one where I will only be posting about 12-15 entries per year for about 3-5 years (if I last that long - I’m retired). So there would be a max of around 70-80 posts. 2 FFDB’s handle the stuff. One for entries and one for comments. They relate on certain screens with the QS running from SSI’s on pages.

This particular code is for my archive page and while I could run it on one screen (link with short show for each entry), I just thought it would be nicer to paginate it so I have 10 entries per screen. I don’t really need the Query string parse here as I have discovered - since starting this thread.

I believe the 2-arg open is insecure enough that they were planning on removing this from some upcoming version?? Or is it already deprecated in 5.14?

I think it’s -w warnings that lets you know for example if you think you’re reusing a var but it turns out you misspelled it… which would look the same as “this variable isn’t being used” but also with “this variable was never declared”. Typo-cleanup ftw.

I’ll go thru it all I could do a search and replace and put my in front of all the $'s and @'s - hmmmm.

It’s a good idea, though I wouldn’t blindly search and replace, since likely you have some subs etc who are now relying on your vars being global (unless you’re going to rewrite them too, which would not be a bad idea!).

Oh dear, this is, “Duh - did I really do that”, time. :blush:

The idea of pagination on this will not work at all, because the script is called by an SSI in an HTML page. I can do something else within the script a whole bunch easier (I really must take more breaks).

Sorry guys.

The opening of a file to read is a good deal though - thanks for that.