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

Good day bretheren. I am trying to use mod CGI with POST input and am getting a stange error. Here is the (simplified) code:
#!/usr/bin/perl -w use strict; use HTTP::Daemon; use CGI qw/:standard/; my $d = HTTP::Daemon->new(LocalPort => 8080) || die; while (my $c = $d->accept) { while (my $r = $c->get_request) { my $uri = $r->uri; $ENV{REQUEST_METHOD} = $r->method; $ENV{CONTENT_TYPE} = join('; ', $r->content_type) || ''; $ENV{CONTENT_LENGTH} = $r->content_length || ''; $ENV{SCRIPT_NAME} = $uri->path || 1; $ENV{QUERY_STRING} = $uri->query || ''; my $q = new CGI; my $reply; my $scriptname = $ENV{'SCRIPT_NAME'}; #$q = new CGI; if ($scriptname =~ /start/) { $reply = $q->header; open(IN,"test.htm") or die "Can't open input: $!\n"; while(<IN>) { $reply .= $_; } close IN; } elsif ($scriptname eq '/process') { my @params = $q->param; $reply = $q->header; $reply .= $q->start_html.$q->h2("Parameters").join('<br>', +@params).$q->end_html; } $c->send_response($reply); } $c->close; undef($c); }
and here is the test.htm it uses
<html> <body> <form method="post" action="process" enctype="multipart/form-data"> <input type="checkbox" name="cb" value="on" /><input type="text" name= +"label" value="foobar" size="40" /> <input type="submit" name=".submit" /> </form> </body> </html>
I get the initial page with localhost:8080/start. Upon clicking SUBMIT I get error Use of uninitialized value in pattern match (m//) at (eval 22)[C:/Perl/site/lib/CGI.pm:829] line 4.. If I step through the code with the (Komodo) debugger, it goes into an infinite wait at my $q = new CGI;. I found that if I delete the enctype argument from the form tag in the htm file, I get no error and the infinite wait, debugger or no.

I found this thread that seems to discuss a similar problem, but it's talking about file uploads and anyway is four years old so I would think that this particular bug has been worked out by now.

Anyone know what the problem is here?

Thx...Steve

Edit: g0n - corrected link format

Update: In the code above I commented out the second CGI constructor, a mistake caught by Chromatic. But that didn't fix the problem. Also based on his opinion that CGI didn't have the proper filehandle, and the old thread linked above, I tried inserting local *main::STDIN = $c; right before the CGI constructor, and that didn't fix things either.

Replies are listed 'Best First'.
Re: Mod CGI problems with POST
by chromatic (Archbishop) on May 31, 2007 at 00:33 UTC

    The problem is that you're creating two CGI objects:

    my $q = new CGI; ... q = new CGI;

    Only the first one can read the POST body from STDIN. When the second one tries to read it, the stream is gone, and you've overwritten the original object, so the data is gone too.

      Good catch, but I removed the second one and the problem persists.

        Oh, right. I overlooked HTTP::Daemon. The problem now is that CGI expects to read from STDIN, but you have another filehandle from which you want to read. I sent Lincoln a patch a long, long time ago to pass in a filehandle from which CGI should read, but I don't know if he ever applied it.

        I remember doing something similar with CGI::Simple instead.

Re: Mod CGI problems with POST
by derby (Abbot) on May 30, 2007 at 23:54 UTC

    Well ... what's on line 829 of CGI.pm (on my version 3.15 it's nothing). More than likely it's the line where $ENV{CONTENT_TYPE} is checked as to whether or not the data is application/x-www-form-urlencoded or multipart/form-data. Is that being sent by your client (or set by your server)?

    -derby

    Update: Normally that infinite loop in CGI is when CGI thinks it's in DEBUG mode and no GET nor POST data is sent (or none was recognized - bad content type) - it's waiting for input on STDIN.

      I've got ver 3.20 and lines 827 thru 831 are
      eval "package $pack; $code"; if ($@) { $@ =~ s/ at .*\n//; croak("$AUTOLOAD: $@"); }
      this is in sub _compile.

      If the content_type is as set by the FORM tag in the sample HTML file, the the content_type that comes through in the environment variable is multipart/form-data; boundary=---------------------------24805226817341 and the behavior is as I described with either the pattern match error or infininte wait if I debug. If I delete that parameter from the FORM statement the content_type is application/x-www-form-urlencoded and I get the infinite wait whether I run straight through or debug.

Re: Mod CGI problems with POST
by stonecolddevin (Parson) on May 30, 2007 at 22:36 UTC

    Ignore this b.s. didn't check to make sure it was accurate.

    You can't create a CGI object when you're using the :standard function oriented interface.

    Change use CGI qw/:standard/; to use CGI; and your object will be created properly

    meh.