Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

On debugging "Premature End of Script" errors

by merlyn (Sage)
on May 06, 2003 at 14:42 UTC ( #255899=note: print w/ replies, xml ) Need Help??


in reply to Premature End of Script Headers Reasons?

Follow this easy sequence of steps:

  1. Make sure this works:
    #!/bin/sh echo content-type: text/plain echo date
    (If you're not on Unix, you can skip this step, but this reveals a lot about the CGI setup without having to introduce the unknown of Perl.)
  2. Make sure this works:
    #!/your/path/to/perl print "Content-type: text/plain\n\n"; print "Hello world at " . localtime() . "\n";
  3. Now take your original script, and insert these right after the #! line:
    #!/your/path/to/perl BEGIN { print "Content-type: text/plain\n\n--OUTPUT--\n"; } ... rest of your script ...
    and make sure your code passes perl -c.
You should now see as the first few lines of output the exact headers and body sent by your program to the server. If the first lines up to the blank line are not nicely formatted headers, fix your script as needed. Once the headers are fixed, you can remove the BEGIN block.

Also, every time you see that sort of error, a sometimes-useful error message has been written to your web server's error log, so find that as well. If you don't have access to the server log, a simple fix is to change step three's suggestion to:

BEGIN { print "Content-type: text/plain\n\n--OUTPUT--\n"; open STDERR, ">&STDOUT"; # errors go to browser now } ... rest of your script ...
And now the error messages show up in your browser, as if your browser was the result of invoking the script as a command line.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.


Comment on On debugging "Premature End of Script" errors
Select or Download Code
Re: On debugging "Premature End of Script" errors
by halley (Prior) on May 06, 2003 at 17:23 UTC

    Though the questioner said 'intermittent,' I'll also add the tip to make sure your script isn't using inconsistent line endings. A DOS-style \x0D\x0A at the end of the shebang line tends to confuse some Unix and Linux kernels. Perl doesn't mind but the kernels do.

    Try running (dos2unix) on the script file, or the equivalent (perl -pie 's/\x0D//g' file).

    Many editors often hide this problem *unless* the line endings are mixed within the same file: they figure you know what you're doing, or you don't know what line ending differences are.

    --
    [ e d @ h a l l e y . c c ]

Re: On debugging "Premature End of Script" errors
by markjugg (Curate) on May 06, 2003 at 18:00 UTC
    I think this an excellent answer to what seems to be a common question. I was surprised that a question like this wasn't already in the Categorized Q & A. I'd like to this the question with this answer appear there.

    Mark

      Depending on which version of perl you got, you will get one of the following from perldoc -q 500
      My CGI script runs from the command line but not the browser.  (500 Server Error)
                Several things could be wrong. You can go through the
                "Troubleshooting Perl CGI scripts" guide at
      
                        http://www.perl.org/troubleshooting_CGI.html
      
                If, after that, you can demonstrate that you've read the FAQs
                and that your problem isn't something simple that can be easily
                answered, you'll probably receive a courteous and useful reply
                to your question if you post it on
                comp.infosystems.www.authoring.cgi (if it's something to do with
                HTTP or the CGI protocols). Questions that appear to be Perl
                questions but are really CGI ones that are posted to
                comp.lang.perl.misc are not so well received.
      
                The useful FAQs, related documents, and troubleshooting guides
                are listed in the CGI Meta FAQ:
      
                        http://www.perl.org/CGI_MetaFAQ.html
      
      older
       My CGI script runs from the command line but not the browser.  (500 Server Error)
                 If you can demonstrate that you've read the following FAQs and
                 that your problem isn't something simple that can be easily
                 answered, you'll probably receive a courteous and useful reply
                 to your question if you post it on
                 comp.infosystems.www.authoring.cgi (if it's something to do with
                 HTTP, HTML, or the CGI protocols). Questions that appear to be
                 Perl questions but are really CGI ones that are posted to
                 comp.lang.perl.misc may not be so well received.
      
                 The useful FAQs and related documents are:
      
                     CGI FAQ
                         http://www.webthing.com/tutorials/cgifaq.html
      
                     Web FAQ
                         http://www.boutell.com/faq/
      
                     WWW Security FAQ
                         http://www.w3.org/Security/Faq/
      
                     HTTP Spec
                         http://www.w3.org/pub/WWW/Protocols/HTTP/
      
                     HTML Spec
                         http://www.w3.org/TR/REC-html40/
                         http://www.w3.org/pub/WWW/MarkUp/
      
                     CGI Spec
                         http://www.w3.org/CGI/
      
                     CGI Security FAQ
                         http://www.go2net.com/people/paulp/cgi-security/safe-cgi.txt
      
      This should be the first response to every question dealing with perl and error 500. The 2nd response should be "where's the sourcecode?"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://255899]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-25 16:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (145 votes), past polls