Debugging internal server errors (premature end of script headers) part 5

Debugging your script

I am assuming that your script is still generating an error page, even though you have the chmod and path to perl set right. Otherwise, you would have closed the browser down and not clicked through to this page. Right? Right. I'm good, aren't I. Should have gone in for being a psychic.

The first thing we need to do is put a few lines into your script to help show where the problem might be coming from. Under the shebang (you know, the path to perl? #!/usr/bin/perl) put the following lines, using copy and paste as typing it in leads to mistakes when you *think* a character is one thing, but it's really another:

    print "Content-type: text/htmlnn";
    use diagnostics;
    use CGI::Carp qw(fatalsToBrowser);
The first line tells the browser to print any output as text/html. Without this line, nothing gets printed to the browser and it will just error still.The second line tells the script to run under diagnostics, or warn you of any problems with the code.The third line tells the script to print any errors out into the browser instead of the error logs.

You may already have the line 'print "Content-type: text/htmlnn";' right at the top of the script. If this is the case, you needn't put it in again. However, if this line isn't right under the shebang in the script, put it in again. We need to catch any errors right from the start of the script, and nothing will get printed to screen until after the 'content-type' line. This is also why you need the 'carp' line right near the top. It won't start reporting errors until after that line, so if this line is down the bottom, there's no errors after it to catch!

So the first few lines of your script should now look like this:
    #!/usr/bin/perl
    print "Content-type: text/htmlnn";
    use diagnostics;
    use CGI::Carp qw(fatalsToBrowser);

Once you'd done that and saved it, open up the browser to your script again and see if it says anything. If you still get an 'internal server error' message, and you're damn sure your path to perl is right and chmod is set correctly, the issue may be where the script is located. If it's inside the cgi-bin directory, it should be able to work. Try changing the extension to the script - it can be either .cgi or .pl and it should still work fine. Your host might have set cgi up to use only .pl extensions. So, right click the file in ftp and choose 'rename' from the dropdown list. Change the .cgi to .pl, or the other way around if your script has a .pl extension. Try it in the browser again.

If nothing works still, take another look at the code I told you to put in. Compare it with what I have above. Make sure EVERY letter is EXACTLY the same. Make sure there is NO space at all before the #!/usr/bin/perl or #!/usr/local/bin/perl, depending on your path to perl. There should be NO WHITESPACE at all before this line (no blank lines, no spaces, no nothing!!).

Houston, we have an error!

Using the lines above, it will write errors to the browser so you don't need to go digging through error logs. There are so many errors that can be reported, there's no way I could cover them all. Hopefully you'll be able to look at the error, and work out where the problem is from there.

A common problem is failing to find files that are 'used' or 'required'. Your script will first look into the same directory as the your script for these files - then it will look into the server libraries for them. If it can't find them in the server libraries (known as @inc), it fails with messages like the following:


Can't locate imagemaker.pm in @INC (@INC contains: /usr/lib/perl5/5.6.1/i386-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i386-linux .) at test.cgi line 7.BEGIN failed--compilation aborted at test.cgi line 7.

This means that in your script on line 7 it has 'use imagemaker' - imagemaker.pm is a file that it's looking for that it can't find. If this is something that came with your script, make sure it's uploaded into the same folder as your script. If the message was something like 'Can't locate directory/imagemaker.pm' then the directory it's looking for needs to be in the directory as the script, and make sure the imagemaker.pm file is inside it.


Can't locate imageparser.pm in @INC (@INC contains: /usr/lib/perl5/5.6.1/i386-linux /usr/lib/perl5/5.6.1 /usr/lib/perl5/site_perl/5.6.1/i386-linux .) at test.cgi line 10.

This means that in your script on line 11 it has 'require imageparser'. Same as above, check to see whether the file came with the script, and if it's where the script is looking for it.

It may be that these are modules that your host will need to install on the server. Have a hunt around on http://www.cpan.org and see if you can find the module it's looking for. If so, download it and upload it into the same folder as the script. There's a good chance this will fix the problem.