#!/usr/bin/perl -w # form-test - a simple illustration of forms and Perl CGI # by Richard L. Hawes use strict; # Adapted from cgi-lib.pl by S.E.Brenner@bioc.cam.ac.uk # Copyright 1994 Steven E. Brenner sub ReadParse () { my($h_ref, $i, $in, @in, $key, $val); $h_ref = {}; # create anonymous hash $in = ""; if ( $ENV{'REQUEST_METHOD'} eq "GET" ) { $in = $ENV{'QUERY_STRING'}; } elsif ( $ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); } else { # Added for command line debugging # Supply name/value form data as a command line argument # Format: name1=value1\&name2=value2\&... # (need to escape & for shell) # Find the first argument that's not a switch (-) $in = ( grep( !/^-/, @ARGV )) [0]; $in =~ s/\\&/&/g; } @in = split(/&/,$in); foreach $i (0 .. $#in) { # Convert plus's to spaces $in[$i] =~ s/\+/ /g; # Split into key and value. ($key, $val) = split(/=/,$in[$i],2); # splits on the first =. # Convert %XX from hex numbers to alphanumeric $key =~ s/%(..)/pack("c",hex($1))/ge; $val =~ s/%(..)/pack("c",hex($1))/ge; # Associate key and value. \0 is the multiple separator if (defined($$h_ref{$key})) { $$h_ref{$key} .= "\0" . $val; } else { $$h_ref{$key} = $val; } } return $h_ref; } my($in, $key); print("Content-type: text/html\n\n"); $in = ReadParse(); print("
\$#ARGV = $#ARGV.
\@ARGV = ", join(", ", @ARGV)); print("
Here is the form data:
And here are some of the environment variables: