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

Greetings monks,

So, I've never written a CGI before, and I'm trying to get my feet wet, so to speak. In playing around with CGI, I've discovered something that I find a bit wierd. First the code:

#!/usr/bin/perl -w use strict; use CGI qw/:standard/; print header(); print start_html("This is a test"); print start_form(); print end_form();
Pretty standard, eh? Well, when I run this, I get the following output:
Content-Type: text/html; charset=ISO-8859-1 <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"><head><title>This is a test</title> </head><body> Use of uninitialized value in length at (eval 9) line 11. <form method="post" action="/test.pl" enctype="application/x-www-form- +urlencoded">
Notice the "uninitialized value" warning. From what I've been able to ascertain, the trouble starts with the start_form sub. I looked at the documentation for CGI, and it seems like it is reasonable to call start_form sans arguments. Also, I looked at CGI.pm's source, specifically at the startform sub. I'm able to clear the warning if I provide a query string, or if I provide an action to the subroutine. Perhaps I'm making too much out of it, but I would like this to run clean under warnings. Any insight would be appreciated.
</form>

thanks in advance,
thor

Replies are listed 'Best First'.
Re: CGI "uninitialized value" question
by Roger (Parson) on Dec 09, 2003 at 05:15 UTC
    That bit of code worked for me under Perl 5.6.1 with an older version of the CGI library. However when I ran it under 5.8 with new CGI library, I get the warnings messages. It turns out that you need to define the 'action' for the form to get rid of the error message. Could be a bug in the new version of CGI module, but then again you should really define the action to be taken when submitting the form.

    #!/usr/bin/perl -w use strict; use CGI qw/:standard/; print header(); print start_html("This is a test"); print start_form(-action=>"/cgi-bin/test.pl", -method=>"GET"); print end_form();
      I would agree that providing an action would be a reasonable fix, except that the documentation says that it provides a reasonable default. I consider this a bug unless there's compeling evidence to the contrary...

      thor

        Well, I guess the documentation has to define what is a reasonable default. From what I saw in the OP, he you got action="/test.pl" as a default, which would not work if the script was stored under "/cgi-bin".

Re: CGI "uninitialized value" question
by Anonymous Monk on Dec 09, 2003 at 13:13 UTC
    When you run the script from the command line, $ENV{QUERY_STRING} is not necessarily defined. startform() refers to QUERY_STRING if the action is unspecified.

    Also, don't forget print end_html();
Re: CGI "uninitialized value" question
by pg (Canon) on Dec 09, 2003 at 05:15 UTC

    Tried your code with Perl 5.8.0 on win98, couldn't repeat the warning message you mentioned.

Re: CGI "uninitialized value" question
by Anonymous Monk on May 08, 2004 at 09:59 UTC
    This bug is fixed in CGI.pm version 3.05. In this part of CGI.pm:
    1721c1643 < if (exists $ENV{QUERY_STRING} && length($ENV{QUERY_STRING})>0 +) { --- > if (length($ENV{QUERY_STRING})>0) {