'lo

Andrew Wilson andrew at rivendale.net
Mon Aug 27 12:19:34 CDT 2001


On Mon, Aug 27, 2001 at 05:52:33PM +0100, Stray Toaster wrote:
> OK, I never said it was pretty, merely an example. And it does what I
> want it to do, without throwing errors, and to me, that was what
> mattered. (Pragmatist, from that test that we did when....errr...ignore
> that, it makes no sense.)
> 
> perl, for all its fantastic-ness, allows cheap and nasty and quick
> coding. Which is what this is.
> 
> m. who is no longer defensive over his code!

I didn't really like it either ;-) I've done a slight refactoring
(which I include below) again this isn't trying for the best ever
version of Marc's digicam code, just a casual tidying.  I gone with
defining the number of pics per page and then I work in pages.

I've assumed that your pictures start a 0 if they start at 1 then this
doesn't work properly.  Also if the number of pictures is not a
multiple of pics per page then the last page shows how ever many are
left.

No doubt someone will point out some of the problems with my code
here.

cheers

Andrew



#!/usr/bin/perl -w

use Apache;
use CGI qw(:standard);
use strict;

# Some constants
my $num_pics = 4 # pictures per page


my $q = new CGI;
my $r = Apache->request;
(my $page = $r->path_info || 0) =~ s#^/##;

my $PATH = $r->document_root . "/taz/pics";
chdir $PATH or warn "can't CHDIR to $PATH: $!";

my $addy = "http://www.thefamilykerr.co.uk/taz/";

my @pics = <*.jpg>;

# Work out which pictures to show and what page is next
my $pages = int(@pics / $num_pics);

# If client playing silly buggers (looking for a page that isn't there)
$page = 0 if ($page > $pages);

my $begin = $page * $num_pics;
my $end   = $begin + $num_pics -1;
$end = ($end > @pics) ? @pics : $end;
my $next  = $page + 1;

# if it's the last page, wrap around
$next = ($next > $pages) ? 1 : $next;


print $q->title("Taz - RIP");
print $q->h1("This was Taz, a very lovely (and loved) wee pussy cat");

for my $display ($begin .. $end) {
  my $base = $addy . "pics/$display.jpg";
  print qq{<img src = $base border=0 alt="He was a menace indeed!">};
}

my $url = $page ? $addy . "taz.cgi/$next"
                : $addy . "taz.cgi/$num_pics";
print $q->a({ -href => $url}, "Next pics");



More information about the Belfast-pm mailing list