KyussRyn has asked for the wisdom of the Perl Monks concerning the following question:

I have been stuck on a problem for 2 days now, and how looked on the iternet and this site for the answer but unable to find it.

The Problem:

I am trying to process information from a form, that is processed by calling another Perl script ("testLoginData.pl"). I 'POST' the information to the script and then call a sub from a module ('parse_data_both.pm'). I have tested this fairly thoughly and I have used the 'parse_form_data' script from "CGI Programming on the World Wide Web" Chap 4.3. I have used the GET method, and that works fine, but with the post method the line: read( STDIN, $query_string, $ENV{'CONTENT_LENGTH'} ); doesn't fill the $query_string with anything.

The code:

HTML Form line

<form method="post" action="../bin/testLoginData.pl" enctype="multipar +t/form-data">

testLoginData.pl code:

#!c:\Perl\bin\perl use warnings; use parse_data_both; use GUI; use CGI; use constant USERNAME_TEST_PASS => 42; my $q = CGI->new; &DataHandler::parse_form_data (*simple_form);

The parse_data_both.pm module code:

#!c:\Perl\bin\perl use warnings; package DataHandler; $webmaster = 'admin@zixsys.com'; use constant USERNAME_TEST_PASS => 42; sub parse_form_data { print "Content-type: text/plain", "\n\n"; local (*FORM_DATA) = @_; local ( $request_method, $query_string, @key_value_pairs, $key_value, $key, $value ); print "Run Test", "\n"; $request_method = $ENV{'REQUEST_METHOD'}; print "The data sent to FORM_DATA is:", @_, "\n"; if ( $request_method eq "GET" ) { $query_string = $ENV{'QUERY_STRING'}; print "GET query_string is:", $query_string, "\n"; } elsif ( $request_method eq "POST" ) { read( STDIN, $query_string, $ENV{'CONTENT_LENGTH'} ); print "POST query_string is: ", $query_string, " and the length is: $ENV{'CONTENT_LENGTH'}\n"; }
Thank you in advance for any help you can offer

Replies are listed 'Best First'.
Re: Problem with read( STDIN, $query_string, $ENV{'CONTENT_LENGTH'} );
by mr_mischief (Monsignor) on Dec 20, 2010 at 05:58 UTC

    Don't do this yourself, even with help from some book, unless you've first used CGI or another module from the Perl core or from CPAN and found it doesn't do what you need.

    The modules made to deal with this have been thoroughly specified, written, debugged, and maintained by several programmers over years of use by thousands of other programmers. They do it the right way. Trust them more than some book (even an O'Reilly book) last updated ten years ago. CGI.pm itself is a core module since Perl 5.004 and is over twelve years old. It was most recently updated on CPAN less than a year ago.

    Hint: one thing that's wrong here is that POST is a different form method than GET, just as the name "method" implies. The data doesn't move from client to server the same way. In GET, it's part of the query string. In POST, the posted data is not part of the query string (which is why you can't find it there).

    You have to get POST data another way. I'm not going into the specifics of that, because I really want you to use a well-vetted module. If you do, you won't have to even worry about how it's done. Besides, it's possible to have a form sent with POST and have data sent in the query string at the very same time as well. If I told you how to get POST data, your solution would probably not get both the POST data and the query string data after it branched between POST and GET. That's a common error even for programmers that have checked into how to do both: their code still won't handle POST data and a query string. CGI.pm will, and you don't even need to know which data came which way in the vast majority of cases. The data is retrieved through the API the very same way regardless.

Re: Problem with read( STDIN, $query_string, $ENV{'CONTENT_LENGTH'} );
by chromatic (Archbishop) on Dec 20, 2010 at 06:32 UTC

    mr_mischief is absolutely right.

    Beyond that, CGI.pm reads from STDIN. When you call parse_form_data() there's nothing left on STDIN from which to read.

      Thank you both for your replies. I was actually hoping for a module. I have found both CGI::Form and HTML::Form. BY reading through the CPAN review of these I think that the HTML::Form is a better module (more tests and better approval). Can you please let me know if I am on the right track, or if you have a better suggestion. Thanks again for all your help and future advice.