#!/usr/bin/perl -- -*- C -*- # Perl Routines to Manipulate CGI input # S.E.Brenner@bioc.cam.ac.uk # $Header: /people/seb1005/http/cgi-bin/RCS/cgi-lib.pl,v 1.2 1994/01/10 15:05:4$ # # Copyright 1993 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. # slight modifications in the name of elegance made by # Meng Weng Wong # http://www.seas.upenn.edu/~mengwong/meng.html # fsVersion 2.0 # sample form script using this library: # # &ReadParse; # print &PrintHeader; # print &PrintVariables(%in); # check out http://www.seas.upenn.edu/~mengwong/forms/ for more info. # 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 { if (@_) { local (*in) = @_; } local ($i, $loc, $key, $val); # Read in text if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; } elsif ($ENV{'REQUEST_METHOD'} eq "POST") { # for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) # { $in .= getc; } # now why didn't i think of this before. read(STDIN,$in,$ENV{'CONTENT_LENGTH'}); } for (split(/&/,$in)) { # Convert plus's to spaces s/\+/ /g; # Convert %XX from hex numbers to alphanumeric s/%(..)/pack("c",hex($1))/ge; # Split into key and value. ($key, $val) = split(/=/,$_,2); # splits on the first =. $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator $in{$key} .= $val; } return 1; # just for fun } # PrintHeader # Returns the magic line which tells WWW that we're an HTML document sub PrintHeader { return "Content-type: text/html\n\n"; } # PrintVariables # Nicely formats variables in an associative array passed as a parameter # And returns the HTML string. sub PrintVariables { local (%in) = @_; local ($old, $out); $old = $*; $* =1; $output .= "
"; foreach $key (keys(%in)) { ($out = $in{$key}) =~ s/\n/
/g; $output .= "
$key
$out
"; } $output .= "
"; $* = $old; return $output; } # multiple separators don't seem to be considered in the following code. # oh well, i'll deal with it another time -meng # PrintVariablesShort # Nicely formats variables in an associative array passed as a parameter # Using one line per pair (unless value is multiline) # And returns the HTML string. sub PrintVariablesShort { local (%in) = @_; local ($old, $out); $old = $*; $* =1; $output = "\n"; $* = $old; return $output; } 1; #return true