CSCI 4230
Internet Applicaiton Development
Summer 2001
Suggested Solution to Homework #3



 
(1)    For example:

use strict;
use CGI::Pretty qw(-nosticky);
$|++;
#
# BasicSlideShow.pl
#
# Kwok-Bun Yue  March 9, 2001
#
# This cgi program takes an image description file and
# displays the image file one by one with prev and next
# buttons.
#
# HTTP parameters:
#  dir  directory storing the image description file
#    and the image files.
#  file the image description file.
#  curr the current image number being displayed,
#    starting from 1.
#
# The first line of the image description file stores
# the title of the image collection.  The remaining line
# store the information of one image per line in the format
#
# filename::description
#

# Porting constants.
my $DIR_SEPARATOR = "\\";
my $AUTHOR_EMAIL = 'yue@cl.uh.edu';
my $COPYRIGHT = 'copyright 2001 Kwok-Bun Yue';
my $STYLE_FILE = 'styles/BasicSlideShow.css';
my $IMAGES_DESCRIPTION_SEPARATOR = '::';

my $SPACE = " ";

# Global CGI object.
my $q = new CGI::Pretty;

# This file name without path.
my $SELF = $q->script_name();
# Remove path.
if ($SELF =~ /([^\\\/]*)$/) {
 $SELF = $1;
}
# Remove query string.
$SELF =~ s/\?.*//;
# HTTP parameters for form submissions.
my $DIR ='dir';
my $FILE = 'file';
my $CURRENT_SLIDE = 'curr';

# Get HTTP parameters
my $dir = $q->param($DIR);
my $file = $q->param($FILE);
my $currentSlide = $q->param($CURRENT_SLIDE);

if ($FILE) {
 $currentSlide = $currentSlide ? $currentSlide : 1;
 # open file.
 my $path = $q->path_translated();
 # Remove file name
 $path =~ s/(.*[\\\/]).*/\1/;
 my $imageFilename = $path . ($dir ? $dir . $DIR_SEPARATOR : '') . $file;
 open (DATA, "<$imageFilename")
  || (displayErrorPage("The selected gallery file cannot be opened."), exit 0);
 displayImageFile($currentSlide, *DATA, $dir, $file);
}
else {
 displayErrorPage("A gallery file has not been selected.");
}

exit 0;  # main

# Display the image with buttons.
sub displayImageFile {
 my ($currentSlide, $imageFH, $dir, $file) = @_;
 my $title = <$imageFH>;
 chomp $title;
 my @lines = <$imageFH>;
 my $lastSlide = @lines;
 if ($currentSlide < 1 || $currentSlide > $lastSlide) {
  displayErrorPage("Slide number $currentSlide does not exsit in the collection.");
  return;
 }
 chomp $lines[$currentSlide-1];
 my ($imageSrc, $imageDesc) = split /$IMAGES_DESCRIPTION_SEPARATOR/, $lines[$currentSlide-1];

 print $q->header,
    $q->start_html(-title=>'Basic Slide Show',
                         -author=>$AUTHOR_EMAIL,
                         -meta=>{'keywords'=>'program code survey',
                                 'copyright'=>$COPYRIGHT},
                         -style=>{'src'=>$STYLE_FILE}),
    $q->h2("Slide Show: $title"),
    $q->p,
    $q->a({href=>"$SELF?$DIR=$dir&$FILE=$file&$CURRENT_SLIDE=1"},'First Slide'),
    $SPACE x 4;
 if ($currentSlide == 1) {
  print "Previous Slide";
 }
 else {
  print $q->a({href=>"$SELF?$DIR=$dir&$FILE=$file&$CURRENT_SLIDE=" . ($currentSlide-1)},'Previous Slide');
 }
 print $SPACE x 4;
 if ($currentSlide == $lastSlide) {
  print "Next Slide";
 }
 else {
  print $q->a({href=>"$SELF?$DIR=$dir&$FILE=$file&$CURRENT_SLIDE=" . ($currentSlide+1)},'Next Slide');
 }
 print $SPACE x 4,
      $q->a({href=>"$SELF?$DIR=$dir&$FILE=$file&$CURRENT_SLIDE=$lastSlide"},'Last Slide'),
    $q->p,
    $q->h3($imageDesc),
    $q->p,
    $q->img({-src=>"$dir/$imageSrc"}),
    $q->end_html;
}

# Error Page that should never be displayed.
sub displayErrorPage {
 my $error = shift;
 print $q->header,
    $q->start_html(-title=>'Error Page for Basic Slide Show',
                         -author=>$AUTHOR_EMAIL,
                         -meta=>{'keywords'=>'basic slide show',
                                 'copyright'=>$COPYRIGHT},
                         -style=>{'src'=>$STYLE_FILE}),
    $q->h2('Basic Slide Show: Error Page'),
    $q->p("Sorry, an error has occurred: $error"),
    $q->end_html;
} # displayErrorPage