![](/WDVL/Website/Design/images/spacer.gif) |
Reading and Parsing Form Data with cgi-lib.pl
Reading and Parsing Form Data with
cgi-lib.pl
- Wow. Well that was quite a bit of
technical programming stuff with very little obvious application
to CGI.
- Now that we have the techniques of Perl
under our belts, let's apply them to a very real world CGI
application.
- Specifically, we will learn how to read
and parse the data coming in from a web based HTML form so that
we can use that information for a CGI script.
- To do so, we are going to learn how to
use a public domain CGI library called cgi-lib.pl which has
served thousands of CGI developers simply and securely for the
last few years.
- In particular, we will look at the
ReadParse subroutine in the cgi-lib.pl library which is used to
read and parse form data. Actually we are going to look at a
simplified version of the library with a very simple
implementation of ReadParse to show you the main points of
reading and parsing form data. However, since the ReadParse
method is well hidden, the inner workings of the library will not
affect your code when you use it.
- Let's look at the code:
sub ReadParse {
// Assign the incoming associative
// array to the %in array if there
// is one. Otherwise, we will use
// the %in name. Also, prepare some
// local variables. Since this
// array is passed by reference, we
// will be filling the array for use
// by the calling program.
local (*in) = @_ if @_;
local ($i, $key, $val);
// Test to see if the data is POST
// or GET data and handle it in
// either case. Remember from
// yesterday?
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
}
elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
}
// Create an array of name/value pairs
// by splitting the encoded data on the
// ampersand sign.
@in = split(/[&;]/,$in);
// Now, for each name/value pair,
// we will...
//
// 1. Substitute plus signs for spaces
// 2. Split each name/value pair into
// a name and a value.
// 3. Decode the encoded data for names
// and values. remember, everything
// has been encoded.
// 4. Handle multiple select values.
// 5. Add the name/value pair as
// elements in an associative array
foreach $i (0 .. $#in) {
$in[$i] =~ s/\+/ /g;
($key, $val) = split (/=/,$in[$i],2);
$key =~ s/%(..)/pack("c", hex($1))/ge;
$val =~ s/%(..)/pack("c", hex($1))/ge;
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val;
}
// Now return the number of elements in
// the associative array.
return scalar (@in);
}
- As you can see, now our program will have
an associative array called %form_data that will be filled with
the name/value pairs sent to us from the HTML form.
- So, how does a CGI program actually use
the ReadParse subroutine shown above?
- Well, you use it as you would use any
inter-application library by calling it from your program. For
example, consider the following code which simply prints out all
of the names and values passed to the script:
#!/usr/local/bin/perl
require "cgi-lib.pl";
&ReadParse(*form_data);
print "Content-type: text/html\n\n";
print qq!
<HTML>
<HEAD>
<TITLE>Testing Form Input</TITLE>
</HEAD>
<BODY>
<TABLE>!;
foreach $key (keys(%form_data))
{
print qq!
<TR>
<TD>$key</TD>
<TD>$form_data{$key}</TD>
</TR>!;
}
print qq!
</TABLE>
</BODY>
</HTML>!;
The &ReadParse
function is used to read the form variables whether the form was
called via a GET or by a POST method. If &ReadParse is called
without a parameter, by default the form variables are read into
an associative array called %in. You can use your own associative
array if you pass it by reference to &ReadParse. For example, if
you wanted to read the form variables into an associative array
called %form_data, you would use this convention:
&ReadParse(*form_data);
|
Additional Resources:
Perl Inter-Application Libraries
Table of Contents
Building a Form Processor
| ![](/WDVL/Website/Design/images/spacer.gif) |
|