Up: Strategies for using scripts

File and Memory Utilities

Persistant 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);


Up: Strategies for using scripts

[HOME] The Geometry Center Home Page

Comments to: webmaster@www.geom.uiuc.edu
Created: May 09 1996 --- Last modified: Tue Jun 4 22:14:18 1996