Example 9


#!/usr/local/bin/perl # The above must appear as the first line of the script if Perl is to be invoked, # and must appear as written, except for the path to the Perl executable. # Standard subroutine to parse the string sent from the Web client. This is # freely available (see below) and will process strings sent by both the POST # and the GET methods. Typically you would call this as a function from a # separate library as we shall see in the next example. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # Perl Routines to Manipulate CGI input # S.E.Brenner@bioc.cam.ac.uk # $Header: /cys/people/seb1005/http/cgi-bin/RCS/cgi-lib.pl,v 1.6 1994/07/13 15:00:50 seb1005 Exp $ # # Copyright 1994 Steven E. Brenner # Unpublished work. # Permission granted to use and modify this library so long as the # copyright above is maintained, modifications are documented, and # credit is given for any use of the library. # # Thanks are due to many people for reporting bugs and suggestions # especially Meng Weng Wong, Maki Watanabe, Bo Frese Rasmussen, # Andrew Dalke, Mark-Jason Dominus. # see http://www.seas.upenn.edu/~mengwong/forms/ or # http://www.bio.cam.ac.uk/web/ for more information # The following is a minimal example for using ReadParse and is # included as a comment. # Example of minimalist http form and script # (http://www.bio.cam.ac.uk/web/minimal.cgi): # if (&MethGet) { # print &PrintHeader, # '<form method=POST><input type="submit">Data: <input name="myfield">'; # } else { # &ReadParse(*input); # print &PrintHeader, &PrintVariables(%input); # } # ReadParse # Reads in GET or POST data, converts it to unescaped text, and puts # one key=value in each member of the list "@in" # Also creates key/value pairs in %in, using '\0' to separate multiple # selections # If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse, # information is stored there, rather than in $in, @in, and %in. sub ReadParse { local (*in) = @_ if @_; local ($i, $loc, $key, $val); # Read in text # The text is equated to the variable "in" # For the GET method in comes from the environment variable # QUERY_STRING # For the POST method it is read from standatd input. Note that since # new line characters are escaped there is no telling where this variable # ends. The CONTENT_LENGTH environment variable specifies # exactly how many bytes are to be read into in from STDIN. if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); } @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))/eg; $val =~ s/%(..)/pack("c",hex($1))/eg; # Associate key and value # This gives us an associative array (a neat feature in Perl, C and some other # languages, which has entries of the form, in this example, of: # in(name) = phil # in(email) = bourne@sdsc.edu $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator $in{$key} .= $val; } return 1; } #+++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # Now our specific application code which is simply going to write out # the contents of the associative array in the desired format. &ReadParse(); # Open the file comments.txt which resides in the same directory as the CGI script # and which already contains comments and append to it. open (COMMENTS, ">>comments.txt") || die "Cannot open comment.txt: $!\n"; # Enter the date and time taken from the UNIX date command. $date = `/bin/date +%D`; print COMMENTS "##########################################################\n\n"; print COMMENTS "$date\n"; if ($in{'name'} ne '') { print COMMENTS "Posted by: $in{'name'}\n"; } if ($in{'email'} ne '') { print COMMENTS "Email: $in{'email'}\n"; } if ($in{'phone'} ne '') { print COMMENTS "Phone number: $in{'phone'}\n"; } $in{'comments'} =~ s/\//g; print COMMENTS "\nComments:\n\n$in{'comments'}\n\n"; # That does it for the output file now we worry about what to pass back to # the client Web browser that indicates that the comments have been registered. # To do this we simply write to standard output. We first tell the browser # that we are passing back HTML and it should be interpreted accordingly. print "Content-type: text/html\n\n"; print "<TITLE>Posting Successful</TITLE>"; print "<BODY BACKGROUND=\"bg.gif\">"; print "<CENTER><P>"; print "<B>Your comments were succesfully logged.</B>"; print "<P><img ALIGN=middle SRC=\"images/hrblue.gif\"><p>"; print "<a href = \"./Databases.html\"> Databases</a> |"; print "<a href = \"./Software.html\"> Software</a> |"; print "<a href = \"./Research.html\"> Research</a> |"; print "<a href = \"./People.html\"> People</a> |"; print "<a href = \"./Pubs.html\"> Publications</a> |"; print "<a href = \"./Grants.html\"> Grants</a> |"; print "<a href = \"./Comments.html\"> Comments</a> |"; print "<a href = \"./Help.html\"> Help</a> <BR>"; print "<a href = \"./Search.html\"> Text Search</a> |"; print "<a href = \"http://www.sdsc.edu/\"> SDSC Home Page</a> <P>"; print "<img ALIGN=middle SRC=\"images/hrblue.gif\">"; print "</CENTER>"; The comment that appears in the file comments.txt resulting from the execution of this script is ########################################################## 05/24/96 Posted by: phil Email: bourne@sdsc.edu Phone number: (xxx) yyy-zzzz Comments: My Comments