Presenting Mathematical Concepts on the World Wide Web
Forms & Scripts

Using Scripts: File and Memory Utilities

Persistent Properties

The simplest way of remembering persistant properties is to use HIDDEN input fields in forms. Only a limited amount of data can be stored in this way, but it is quick and easy.

More sophisticated methods might rely on Netscape Cookies, or storing the name of a temporary file in a hidden variable, etc.

The following routines pack and restore pairs of points into a string of the form x1 z y1 q x2 z y2 q .... They use the global variables $history for the string and @hx,@hy for the arrays of points.

sub pack_history (
    $hl = $#hx;
    if ($hl > 10) {$hl = 10;}        # limit to 10 pairs
    $history = "";
    for $i (0..$hl) {
       $history .= $hx[$i]."z".$hy[$i]."q";
    }
}

sub expand_history (

   @pairs = split('q',$history);
   $i = $#pairs;
   foreach $i (0..$#pairs) {
      ($hx[$i], $hy[$i]) = split('z',$pairs[$i]);
   }
}

The Graphing demo has a full example illustrating how to use these routines in a form to implement persistant memory.

File management

Temporary files created by a CGI script are owned by the http daemon. Thus it is best to have the temproary files periodically erased by the script itself. Here is a routine to implement this:
# Scan for old files and erase them.

opendir(WORKDIR, $WORKDIR);
$old = time() - 600;		# "Old" files haven't been touched in 600 sec=10min

foreach $_ (readdir(WORKDIR)) {
    if(/^.+$gifsuffix$/  || /^.+$pssuffix$/  ) { # any file with suffixes
	$thisfile=$WORKDIR . "/" . $_;
	@stat = stat($thisfile);      # stat[8]=mtime; stat[9]=ctime
	unlink($thisfile) if($stat[8] < $old && $stat[9] < $old);
    }
}
close(WORKDIR);


Back: Stragegies for Using Scripts
Up: Forms & Scripts


Presenting Mathematical Concepts on the World Wide Web. Copyright © 1997 by Carol Scheftic. All rights reserved. (This section was originally copyrighted in 1996 by The Geometry Center and is re-used here with permission.) Please send comments on this page, or requests for permission to re-use material from this page, to: scheftic@geom.umn.edu
Page established 1-Jun-97; last updated Thursday, 24-Jul-1997 02:09:33 CDT.