File entries are stored one per line as
variable=value pairs, with any funny characters
URL-escaped. Each record is separated by a line with a single equals
sign. These are typically read back by invoking the
CGI->new method with a filehandle argument that
manages all of the unescaping automatically, as described later.
If you want to add extra information to your query before you save
it, the param function (or method, if you're using
the object-oriented interface) can take more than one argument,
setting the value(s) of a form parameter. For example, here's how to
save a time stamp and the entire environment:
param("_timestamp", scalar localtime);
param("_environs", %ENV);
Once you have the forms in a file, process them by using the object
interface.
To load a query object from a filehandle, call the
new method with a filehandle argument. Each time
you do this, it returns a complete form. When end of file is hit, the
returned form has no parameters. The following code demonstrates this
approach. It keeps a running total of all "items
request" parameters, but only if the form was not
submitted from a perl.com site. Remember, we
added the _environs and
_timestamp parameters when we wrote the file.
use CGI;
open(FORMS, "< /tmp/formlog") or die "can't read formlog: $!";
flock(FORMS, 1) or die "can't lock formlog: $!";
while ($query = CGI->new(*FORMS)) {
last unless $query->param( ); # means end of file
%his_env = $query->param('_environs');
$count += $query->param('items requested')
unless $his_env{REMOTE_HOST} =~ /(^|\.)perl\.com$/
}
print "Total orders: $count\n";
File ownership and access permissions are an issue here, as for any
files created by CGI scripts.