Dave,
I just noticed one significant error in your code which will cause errors. You left he dollar sign off whichmethod when you set it.
Should read [tt]$whichmethod = $ENV{'REQUEST_METHOD'};[/tt].
To be honest though, I have never bothered using any of the routines for manually parsing the incoming form because CGI.pm does it for me automatically, so I only have to worry about what I need to do with the form field values and not how to put them into a form that the script can use. It is really worth the time to give the CGI.pm documents (
or (assuming that your perl implementation is your path) issue the command [tt]perldoc CGI[/tt] to read the docs specific to your version and get a feel for it, it will more than earn you back the time you save reading it.
Here is an explanation of how to do what I think you want to do with CGI.pm, the code in red is code using CGI.pm and blue is the code from your example that it makes unnecessary. I will assume that your form fields are named text1 and text2 and you want to strip the spaces from text1.
[tt]
#!perl -w
use strict;
use CGI;
my $cgi_object = new CGI; #this one line
# brings in all the form data and sets up a
# hash containing all the form field names and
# values which are accessible through param().
# CGI.pm recognizes whether the form action
# was GET or POST so you don't have to test for it.
whichmethod = $ENV{'REQUEST_METHOD'};
if($whichmethod eq "GET"
{
$forminfo = $ENV{"QUERY_STRING"};
}else
{
$forminfo = <STDIN>;
}
@key_value_pairs = split(/&/,$forminfo);
foreach $pair (@key_value_pairs){
($key,$value) = split(/=/,$pair);
$value =~ s/\+//g;
$value =~ s/%([0-9a-fA-F][0-9a-fA-F])/pack("C", hex($1))/eg;
$FORM_DATA{$key} = $value;
}
# basically all of the above is negated using CGI.pm
# now the fields are available to play with
# so I will set up script variables to hold
# the values of text1 and text2
my $text1 = $cgi_object->param('text1');
my $text2 = $cgi_object->param('text2');
# now, strip the spaces from the value in text1
$text1 =~ s/ //g;
# print the values back out to your web page to see what it looks like
print $cgi_object->header;
print $cgi_object->start_html(-'title'=>"My Form Results"
;
print $cgi_object->p("\$text1 contains the following value: $text1"
;
print $cgi_object->p("\$text2 contains the following value: $text2"
;
print $cgi_object->end_html;
[tt]
Here is a simple html page that I made to test this out:
[tt]
<html>
<head>
<title>Test the form response</head>
<body bgcolor="white">
<h2>Just want to test out field value processing.</h2>
<form action="/cgi-bin/strip_space.pl" method="GET">
<p>Enter a value in this field: <input type="text" name="text1" length="25"></p>
<p>Enter a value in this field: <input type="text" name="text2" length="25"></p>
<input type="submit" value="Do It"><input type="reset" value="Clear the form">
</body>
</html>
[/tt]
The output I get after entering "This is a test" into each field and submitting the form is as follows:
[tt]
$text1 contains the following value: Thisisatest
$text2 contains the following value: This is a test
[/tt]
Derek