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

Hello...

I'm running into a 500 error with a simple CGI.pm based script and I can't seem to resolve it.

#/user/bin/perl -wT use strict; use CGI ':all'; use CGI::Carp qw(fatalsToBrowser); $| = 1; print header(), start_html('Test'), 'Hello, World!', end_html();

Here's the form I'm using to invoke it.

<HTML> <HEAD> <TITLE>Test</TITLE> </HEAD> <BODY>Click me:<BR> <form action="/cgi-bin/hello.cgi" method="POST"> <input type="Submit" NAME="submit" Value="Do-it!"> </form> </BODY> </HTML>

When run from my telnet window, it runs fine, producing this output:

> perl -wT hello.cgi '' Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML><HEAD><TITLE>Test</TITLE> </HEAD><BODY>Hello, World!</BODY></HTML>

This look right to me; however, when called from my sample form, I get a server 500 error. There is only one line added to the error log: Premature end of script headers: /home/mypath/cgi-bin/hello.cgi

I have checked permissons (755) on the script and the parent directories. Perl 5.05.003, Apache 1.3.12; FreeBSD 4.0; CGI.pm version unknown;

I'm out of ideas; either I'm, like, incredibly lame or something else is wrong. Please help.

Edit: 2001-03-03 by neshura

Replies are listed 'Best First'.
Re: Premature Error using CGI.pm
by epoptai (Curate) on Feb 24, 2001 at 09:35 UTC
    You forgot the ! in your shebang.
    #/user/bin/perl -wT
    So it works from the command line when you specify the perl interpreter, but fails via CGI with that incomplete invocation. You need this:
    #!/user/bin/perl -wT

    It happens =)

    Update: If it still doesn't work you have some other more exotic problem. I tested your code and form and it didn't work until ! was added.

      Just who *did* put the ! in the !#!#!? Let alone the ram in the ramalamadingdong...

      Sorry, it's been a long week...
      Sorry, I mistyped, it's what you'd expect it to be.
      #!/usr/bin/perl -wT

      Any other thoughts?

Re: Premature Error using CGI.pm
by aardvark (Pilgrim) on Feb 24, 2001 at 13:35 UTC
    CGI error debugging can get really whacky. I feel your pain.
    I would try breaking that one long print statement into several smaller print statements.
    Maybe then you can get a more meaningful error.

    You are using the standard syntax when you might want to try the object-oriented syntax,
    Try ...
    my $cgi = new CGI; print $cgi->header(), $cgi->start_html('Test the boundaries of Relaxation'), $cgi->p('Hello Weekend'), $cgi->end_html;
    Also, are you sure you want your shebang line(#!) to be:
    #!/user/bin/perl -wT
    and not
    #!/usr/bin/perl - wT

    I hope this helps.
    Get Strong Together!!

    BTW - does anybody know why a shebang of:
    #!/user/local/bin/perl
    works on my machine?
    I have no /user directory, so I would expect an error, but I got none. Is something going on under the hood?

      BTW - does anybody know why a shebang of:
      #!/user/local/bin/perl
      works on my machine? I have no /user directory, so I would expect an error, but I got none. Is something going on under the hood?

      Assuming you're talking about Apache, it doesn't use the first line, but instead relies on file types that are set up in the configuration file to determine what type of file a request is, and so as long as it's named "something.pl", it doesn't matter to Apache what the first line is. Of course, if you run it from the command line, you'll get the unknown file error.

        Assuming, of course, you're on win32. I think both win32 and apache/win32 use the shebang line for command line options (-wT of course), but not to find the perl exe. On my unix (Solaris/Linii) boxen, a bad shebang path doesn't run, command line or cgi. On win32, it doesn't matter. Does YMV?.

        a

Re: Premature Error using CGI.pm
by bladx (Chaplain) on Feb 25, 2001 at 02:45 UTC
    aardvark I think was the most correct on the answer to this funny problem. It was both the shebang line but also with USR not USER :) so I think arrdvark and epoptai should get the most credit for their great answers =).

    I tested this script out on my own server... and it worked after noticing that the shebang line needed 2 errors fixed, and aardvark really noticed that :) so that's all I have to say...

    bladx ~ king of nothing yet
Re: Premature Error using CGI.pm
by a (Friar) on Feb 24, 2001 at 10:12 UTC
    Do any perl cgi's work (/usr/bin/perl isn't available to the webserver)? Have you tried it w/o CGI.pm (its not accessable to the webserver)? Just put your:
    Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <HTML><HEAD><TITLE>Test</TITLE> </HEAD><BODY>Hello, World!</BODY></HTML>
    in a here doc or something.

    a