Re: [MacPerl-WebCGI] Un-webifying HTML User Data Question

[email protected] (Bruce Van Allen) Thu, 10 May 2001 12:34:04 -0700
Newsgroups perl.macperl.webcgi
Message-ID <p05100c00b72086b86294@[205.179.215.186]>
At 6:46 AM -0700 5/10/01, Gregory Smith wrote:
>Hello,
>
>Sorry if this issue has been beaten into the ground before.  I looked
>through the webcgi archives and didn't see anything that I thought could
>help me here.
>
>I'm wondering if one of you kind folks could point me to how to "un-webify"
>data passed from an HTML form for use in a MacPerl CGI?  My Perl/CGI

[snip]

>Here's a test script using my usual  *nix "un-webify" code which will crash
>or freeze MacPerl on my WebStar server:
>-=-=-=-=-=-=-=-=-=-=-
>
>#!perl -w
>use CGI;
>my($cgi) = new CGI;
>
>read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
>@pairs = split(/&/, $buffer);
>foreach $pair (@pairs) {
>    ($name, $value) = split(/=/, $pair);
>    $value =~ tr/+/ /;
>    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
>    $value =~ s/<!--(.|\n)*-->//g;
>    $FORM{$name} = $value;
>}
>
>print $cgi->header();
>print "Hello $FORM{'real_name'}\n\n"; # Print user data
>print "$ENV{'CONTENT_LENGTH'}\n";     # Print string passed from HTML form
>0;
>
>-=-=-=-=-=-=-=-=-=-=-
>
>At this point, I'm just trying to pass the data from the HTML form to the
>CGI and then simply printing out the data back to the browser.
>

Your form's data should be retrieved directly from the CGI object 
$cgi you created on your third line.

I'm not sure if this would cause MacPerl to hang, but it might cause 
it to ponder a bit:  it seems to me that once CGI.pm has processed 
the form input, which it does when you invoke the object $cgi, the 
data isn't necessarily available any longer from standard input 
(STDIN). MacPerl simulates some aspects of the original Unix CGI 
environment, including STDIN, so this might cause the problem.

But let me address a bigger question from your script above: why are 
you doing all that processing ("un-webifying") of the HTML form's 
input, when you have CGI.pm to do it for you, more thoroughly, and 
best of all, transparently? I'm asking this question with no heat: 
your code above looks exactly like some I was paid for in years past 
as I pieced together scripts and code snippets and growing 
comprehension of the CGI process and Perl.

Now I work and teach to a higher standard. My desire to roll my own 
code is channeled toward just three areas: the truly unique and 
original parts of my programs and architecture; creative 
component-assembling with others' modules; and my own learning 
process, which never ends. But for the standard stuff, I rely on 
proven tools provided by the smart and adorable folks from the 
world-wide Perl community. And actually, in this crowd, "standard 
stuff" goes pretty far...

So, my suggestions are biased toward your script more fully using 
CGI.pm, and then testing to see if there's something else about your 
machine's configuration that's causing the hangs. You should 
certainly check the memory allocation to MacPerl under Get Info, and 
I hope you have read the rest of the basics on MacPerl and CGI.

1. Once you invoke CGI.pm, don't read in and process standard input 
as you do above.

2. Check the version number of the CGI.pm module in your Macperl 
installation. You might want to update it (use CPAN-Mac, not the 
*nix-style installation process).

3. If you're concerned with taint checking, see the CGI.pm docs, plus 
use your own routines to process the input out of $cgi before you 
store it or print it out.

4. See if the following works (written with care but not tested):

#!perl   -w
use strict;
use CGI;
my $cgi              = new CGI;


print $cgi->header();

foreach $name ( $cgi->param() ) {
     $value = $cgi->param($name);
     print "The value of $name is $value.<br>\n"
}

# Or, populate a data hash
my %form_data = map {  $_  =>  $cgi->param($_)  } $cgi->param();
print "Hello $form_data {'real_name'}\n\n";

# Make a header array, and use it to index through the data hash
my @fields = $cgi->param();
for (@fields) {
     print "$_ => $form_data{$_}<br>\n"
}

__END__

CGI.pm has lots of documentation.

1;