MGT Computer Solutions >>
Services >>
Custom Programming >>
Perl >> Perl Excerpt
These snippets of Perl v.5 code are from routines which display individual pictures from a photo gallery. They develop the URLs for "previous" and "next" links that aid browsing single pictures in the gallery.
The following snippet only determines the required URLS.
#!/usr/bin/perl -w
use strict ;
use CGI ':standard';
.
.
.
#calculate previous and next gallery item links
my ($Prev, $Next) = ("Previous", "Next");
my $GalleryFile = "+<../image.htm" ;
if ( $ImgPath eq "/cards/" ) {
$GalleryFile = "+<../card.htm"
}
#if we can't open the gallery's html file, we are done with links
if (open(GALLERYFILE, $GalleryFile) ) {
#complete list of gallery item links is right in the gallery's html file
local $/;
binmode(GALLERYFILE) ;
my $FileBuf = <GALLERYFILE> ;
close(GALLERYFILE) ;
#pick out the links we want - look for neighbors of $EncodedURL
if ( $FileBuf =~ m#cgi-bin(/v\.pl\?.{1,40}?nm=.{1,40}?)">.{1,300}?cgi-bin/v\.pl\?.{1,40}?nm=$EncodedURL"#s ) {
$Prev = "<a href=.$1><strong>Previous</strong></a>" ;
}
if ( $FileBuf =~ m#cgi-bin/v\.pl\?.*?nm=$EncodedURL">.*?cgi-bin(/v\.pl\?.*?nm=.*?)"#s ) {
$Next = "<a href=.$1><strong>Next</strong></a>" ;
}
.
.
.
The following snippet is from another version of the photo gallery routine. This version also parses the gallery URLs into an array for later reference.
.
.
.
#calculate previous and next gallery item links
my ($Prev, $Next) = ('Previous', 'Next') ;
#define link count and pointer to current page for later reference
my ($count, $current) = (0, -1) ;
#if we can't open the gallery's html file, we are done with links
if (open(GALLERYFILE, "+<../image.htm") ) {
#create list of gallery item links from gallery's html file
local $/;
binmode(GALLERYFILE) ;
$FileBuf = <GALLERYFILE> ;
close(GALLERYFILE) ;
#split the file on "cgi directory/script name" because it's handy
@arr = split(m#cgi-bin/#, $FileBuf) ;
#lose the first piece as it starts with <html etc.
shift @arr ;
#remaining elements all start with the significant part of the desired url
$count = (@arr) ;
$FileBuf = '' ;
#search the list for the current item's entry
for (my $ix=0; $ix < $count; $ix++) {
#clean up array entries as we go
$arr[$ix] =~ s/>.*$//s ;
# test for current page
if ( $arr[$ix] =~ /=$EncodedItem"/ ) {
#remember this location
$current = $ix ;
}
}
if ( $current != -1 ) {
#current item found, build references for its neighbors
if ( $current < $count - 1 ) {
$Next = '<a href="./' . $arr[$current+1] . '><strong>Next</strong></a>' ;
}
if ( $current > 0 ) {
$Prev = '<a href="./' . $arr[$current-1] . '><strong>Previous</strong></a>' ;
}
}
.
.
.
