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

This question is really pretty vague and general, I realize. I may be updating it more as I investigate further and add more details.

I am in the process of converting numerous CGI programs written in perl to utilize the CGI.PM module. I've done one, and it went well. I'm about to work on a second. All I've done is added two statements to the top:

use CGI; $CGI = new CGI;

No additional code was added or changed, meaning its still PRINTing HTML statements throughout.

But just from this, we've seen some differences depending on whether it was a POST or GET that called this cgi. Now, in the CGI.PM documentation (in the CGI.PM module itself in perldoc fashion) I've not seen anything that speaks to difference between GET and POST making me believe that somehow, the CGI.PM module is taking care of the differences itself. How does CGI.PM process the form variables before anything else is done? It must be doing something, to explain why we see differences just from adding the use CGI; statements. Can somebody give me a clue?

Replies are listed 'Best First'.
Re: Does CGI.PM process input data
by Joost (Canon) on Feb 21, 2007 at 20:11 UTC
    In a CGI environment POST data is available as an encoded STDIN stream. In order to get that data CGI.pm must read it from STDIN (update: and it will probably do so the moment you create the CGI object) . That means that if any other code also tries to read that POST data it probably won't find it.

    Moral of the story is: use one and only one method of parsing CGI input into parameters.

Re: Does CGI.PM process input data
by GrandFather (Saint) on Feb 21, 2007 at 20:14 UTC

    For an overview of CGI using CGI.pm you should read Ovid's CGI course.

    The cgi documentation helps too. ;)


    DWIM is Perl's answer to Gödel
Re: Does CGI.PM process input data
by zentara (Cardinal) on Feb 21, 2007 at 20:56 UTC
    Try this, it's probably similar to what CGI.pm does internally to switch modes, or read the module itself. It's pretty much self-documenting. Or maybe look at one of the CGI Lite variants for easier reading. The GET POST detection is done in the %ENV hash.
    if ($ENV{'REQUEST_METHOD'} eq "GET") { $form_data = $ENV{'QUERY_STRING'}; }else { $form_data = <STDIN>; }
    Now to do it with CGI, do this:
    #!/usr/bin/perl use CGI; my $q = new CGI; my %param = $q->Vars; #To check whether the params were sent via POST, do: my $method = $q->request_method(); print "$method\n"; #which should give either 'POST', 'GET' or 'HEAD'.

    There are alot of cute CGI scripts out there to help in debugging, just google for them. For instance:

    #!/usr/bin/perl use warnings; # arg_echo.cgi - print all form values use strict; use CGI qw/:standard/; $|=1; print "Content-type: text/plain\n\n"; foreach my $name ( param() ) { print "$name: '", param($name), "'\n"; }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum