![]() |
Web Conference 2004Writing Perl/CGI Scripts for ITS/ASET Web servicesInput and Output |
| <- Back - POST method | | | Up | | | Data files - Next -> |
CGI programs have serveral ways of passing messages back and forth with the Web server process (Apache). Here are a few ways of obtaining input or data from Apache:
Environment Variables - A very common way is by use of environment variables, such as:
Environment Variables can be accessed from Perl via the %ENV hash. For example, the following prints the REMOVE_USER variable if a user logged in, followed by all of the environment variables:
#!/usr/local/bin/perl
print <<END;
Content-Type: text/html
<html>
<body>
END
if(defined $ENV{'REMOVE_USER'} and $ENV{'REMOVE_USER'} ne ""){
print "<p>You logged in as <b>$ENV{'REMOVE_USER'}</b></p>\n";
}
print <<END;
<p>Environment variables:</p>
<ul>
END
foreach (keys %ENV){
print "<li><b>$_</b> - $ENV{$_}\n";
}
print <<END;
</ul>
</body>
</html>
END
|
Standard Input (STDIN) - All programs in UNIX start with three special filehandles or input/output channels bound to the terminal, a file or a process. The first is called Standard Input, or STDIN by Perl. CGI programs use STDIN to accept POST data. Without using the CGI module, CGI scripts can read the POST parameters via a script like:
#!/usr/local/bin/perl
# This routine is partially copied from &ReadParse of cgi-lib-1.14.pl
# Perl Routines to Manipulate CGI input
#
# Copyright (c) 1995 Steven E. Brenner
# 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.
#
# For more information, see:
# http://www.bio.cam.ac.uk/web/form.html
# http://www.seas.upenn.edu/~mengwong/forms/
# read CONTENT_LENGTH number of bytes from STDIN into $in
read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
# split query string into list of parameters
@in = split(/[&;]/,$in);
# loop the following block over each parameter
# convert each into name = value pairs, store them in the %in hash
# and convert from URL encoding
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
$in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
$in{$key} .= $val;
}
print <<END;
Content-Type: text/html
<html>
<body>
<p>Your favorite color is $in{'color'}.</p>
</body>
</html>
END
|
Here are a few ways the CGI program passes data back to the Web server process (Apache):
Standard Output (STDOUT) - Like STDIN, STDOUT is a filehandle that is assigned when a program in UNIX is started. STDOUT is used as the main way CGI programs return data to the Web server process and the browser. Any HTML or other file type is returned this way, along with any HTTP headers. The print function sends data to STDOUT by default.
Standard Error (STDERR) - Like STDIN and STDOUT, STDERR is one of the three filehandles assigned when a UNIX program is started. STDERR is used to send errors back to the Web server process, which is stored in the server error log files. A Perl program can send to STDERR directly via print STDERR "message\n";, or indirectly via the die() function or when Perl detects errors.
Return value - All program in UNIX return a numeric code value when they exit. A value of 0 is typically the "all is well that ends well" status. Other values indicate various errors.
| <- Back - POST method | | | Up | | | Data files - Next -> |
If you have any questions, feel free to ask me - mailto:jcd@psu.edu
Content by: Jeff D'Angelo <jcd@psu.edu> © 2004
Last update on: Sunday, 13-Jun-2004 18:40:39 EDT