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

Hello!
#!/usr/bin/perl use strict; use warnings; print "Content-type: text/html\n\n"; print '<html><head></head><body>'."\n"; print 'Some content here'."\n"; print '</body></html>'."\n";

I just can't understand what exactly is wrong with this script. Browser receives message 'Internal server error' and apache error log has line 'Premature end of script headers'.

I understand that this error could be logical if I had missed following line:

print "Content-type: text/html\n\n";

So I'm really confused.

Replies are listed 'Best First'.
Re: Premature end of script headers
by marto (Cardinal) on May 11, 2010 at 08:47 UTC
Re: Premature end of script headers
by Corion (Patriarch) on May 11, 2010 at 08:45 UTC

    HTTP headers are delimited by \r\n, not \n.

      Yes, and? The script isn't talking HTTP. HTTP is the protocol between the server and the browser. This is conversation between the server and a program. They are using the CGI protocol. I'm not aware of any server that doesn't know how to deal with "\n" being used instead of "\r\n" in the CGI protocol.
Re: Premature end of script headers
by Skriptke (Acolyte) on May 11, 2010 at 12:11 UTC
    Many times the error "Premature end of script headers" occur not because, our heads are bad, but because a previous error written "\n\n" to the output before our "\n\n".

    Checks, file permissions, missing libraries, server config, etc ...
      I'd like to add that it's often interesting to just start the script from the command line - often it's obvious from the output what the problem is.

      (Yes, you can run a CGI script without CGI. No, it won't kill any kittens.)

        you're right, but in this case I vote for a bug in file permissions, without running the script on any site :-)
Re: Premature end of script headers
by Anonymous Monk on May 11, 2010 at 13:12 UTC
Re: Premature end of script headers
by zitic (Initiate) on May 11, 2010 at 14:00 UTC

    Thank you all for your answers

    I finally figured out where the error really was. Turns out the script file had wrong extension.

    All I needed to do was to rename my_file.pl to my_file.cgi -- and it worked just fine!

      If so, that's an issue with how Apache is configured; not an issue with Perl itself.