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.

Will the Perl version run differently?

  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.

What is needed to make animate.pl run?

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:

Tell animate.pl where Perl is
Notice the first line of the script says #!/usr/local/bin/perl. This is the location of the Perl interpreter on my machine, you may need to modify this line to reflect your Perl location if it differs from this. If you don't know where your Perl interpreter lives Unix makes it very easy to find out. Just type 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.

$IMAGEDIR setting
The next 5 definitions are all located in the Configuration2 section of the program. The $IMAGEDIR variable defines the full directory name where your image files live. Be sure to include all '/'s where they should be!

$Continuous setting
This defines whether you want to cycle through the images repeatedly, without end. As with the animate.c program we do not recommend this but we do make the option available. The default value is for non-continuous animation, the program will exit after the first cycle. To allow the program to cycle continuously change the line to read $Continuous = 1

@images
This declares an array containing the filenames of every image you want included in the animation sequence. Be sure to include each filename in a pair of quotes! Note - The last imagefile defined in the array will be the single-image displayed for non-Netscape browsers.

@sleep_time
This declares an array of seconds for which the program should sleep between each image. Notice in the program below the first entry is 4, this means the program will wait 4 seconds after displaying the first image (image1.gif).

$TYPE
This variable tells the program which type of image you want to display in the animation sequence. For the included program this variable is set to type $GIFTYPE, if your image files are type jpeg then modify the line to read $TYPE = $JPGTYPE.

Make it executable
The last step is to make the program executable by the web server. To do this type the following at your prompt chmod 755 animate.pl. Afterwards you can move the program to a directory which can run cgi scripts and you are all set!


The Animate.pl Source Code


#!/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


The Radiation Zone is copyright © 1995 by its creators. All rights are reserved.