

Improved Server Push in Perl  Here is generally the same program as animate.c in Perl source code form. The overall concept of this script was derived from the doit.c code and the animate.c enhancement, some concepts were derived from Brian Valente's cycleim.pl, which I found to be an excellent Perl server-push script.
  The perl version employs all of the enhancements found in the animate.c program with the addition of defined sleep times for each phase of the animation. The program is easily configurable for your machine, provided you have a Perl interpreter.
  If you have ever played around with Perl you should know it is an interpreted language rather than a compiled language like C or Pascal. The main difference between a compiled and interpreted language is the existance of binary object code. When you create a C program, like animate.c, it must be compiled before you can run it (which is essentially what the Makefile's job is). A Perl program such as the one listed below doesn't need compilation prior to running it. In fact it is executed one line at a time, with the Perl interpreter figuring out what each line should do. There are advantages and disadvantages to this, an advantage being the debugging aspect is much easier, and a disadvantage being the program will generally run slower. The slower executaion time is in fact only noticable in the animate.pl script during the program startup. That is, once the program has been loaded the delay involved is really quite small and will not be obvious enough to make the Perl version less desirable.
The configuration for animate.pl is really quite easy, and
takes only a few minutes! Everything you need to make
it work on a Unix system is listed here:
which perl
at your prompt and the system should spit out the location. Then
change the first line of the program to represent this, if its a
different location then /usr/local/bin/perl. If which returns
perl: Command not found. then you do not
have Perl installed on your machine and you will need to have your
system administrator make the installation.
$Continuous = 1
$TYPE = $JPGTYPE.
#!/usr/local/bin/perl
# ****************************************************************
# Program: Perl Animation Routine (animate.pl)
# Author: Andrew Cowan
# Date: Aug. 23, 1995
#
# animate.pl (c) 1995 GlobalMedia Design Inc.
# This code is released into the public domain.
# ****************************************************************
# Overall Concept derived from doit.c by Rob McCool
# doit.c (c) 1995 Rob McCool
#
# Some concepts derived from cycleim.pl by Brian Valente
# cycleim.pl (c) 1995 Brian Valente (which is released
# into the public domain, so long as this copyright
# notice is attached).
# ****************************************************************
# ****************************************************************
# Configuration1 - No Need to Change These
# ****************************************************************
$ANHEAD="Content-type: multipart/x-mixed-replace;boundary=ThisRandomString\n";
$BOUNDARY="\n--ThisRandomString\n";
$ENDBOUNDS="\n--ThisRandomString--\n";
$GIFTYPE="Content-type: image/gif\n\n";
$JPGTYPE="Content-type: image/jpeg\n\n";
$HTMLTYPE="Content-type: text/html\n\n";
$agent = $ENV{'HTTP_USER_AGENT'};
# ****************************************************************
# Configuration2 - These need to be modified for your needs
# ****************************************************************
$IMAGEDIR = "/usr/users/cowana/images";
$Continuous = 0;
@images = ("image1.gif","image2.gif","image3.gif","image4.gif");
@sleep_time = (4,2,2,2);
$TYPE = $GIFTYPE;
# ****************************************************************
# Main Routine
# ****************************************************************
undef $/; # Undefine the perl input record seperator.
$|= 1; # Force a flush on every print for the specified filehandle (STDOUT).
# New browser check routine which excludes Mozilla/1.0
# while allowing Mozilla/2.0
$_ = $agent;
if ((/^Mozilla\/1.[1-9]/i) || (/^Mozilla\/[23].[0-9]/i)){
&animate;
}else{
&single_image;
}
exit(0);
# ****************************************************************
# Subroutines Declarations Section
# ****************************************************************
# ****************************************************************
# Animate - Routine to display server push animation
# ****************************************************************
sub animate{
print $ANHEAD;
print $BOUNDARY;
$count = 0;
while(1){
print $TYPE;
$image_file = "$IMAGEDIR/$images[$count]";
($size) = (stat("$image_file"))[7];
open(IFILE, "$image_file");
read(IFILE, $image, $size);
close(IFILE);
sleep($sleep_time[$count]);
print $image;
print $BOUNDARY;
$count++;
if ($count == $#images+1){
if ($Continuous == 0){
last;
print $ENDBOUND;
}else{
$count = 0;
}
}
}
}
# ****************************************************************
# Single_Image - Routine to display a single image
# ****************************************************************
sub single_image{
print $TYPE;
$image_file = "$IMAGEDIR/$images[$#images]";
($size) = (stat("$image_file"))[7];
open(TFILE, "$image_file");
read(TFILE, $image, $size);
print $image;
close(TFILE);
}
# ****************************************************************
# End of Perl Animation Script
# ****************************************************************
Back to the Tutorial on Server Push Animation




