tags:

views:

89

answers:

3

I am trying to print a XML file in the browser using a perl script. While I am trying to run the script in my browser I am having the following error.

Server error!
The server encountered an internal error and was unable to complete your request.
Error message: 
Premature end of script headers: get_data.pl

If you think this is a server error, please contact the webmaster.

Error 500

localhost
01/15/10 14:29:28
Apache/2.2.14 (Win32) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l mod_autoindex_color PHP/5.3.1

Here is my script.I checked the syntax too, using a syntax checker. No problem with my perl configuration, since other simple perl scripts are working fine. I think the problem will be with the "use CGI;" Guide me..

#!/usr/local/bin/perl -w 
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $r = new CGI();
my $id  = $r->param('id');
if(!defined($id)) 
{
 print "Content-type: text/html\n\nMissing parameter: id\n";
 exit 1;
}
if($id eq '7007')
{
 print "Content-Type: text/xml\n\n";
 open INPUT, "get_data.xml";
 while(<INPUT>) {
 print $_; }
 close INPUT; 
}

OMG!! I just ranamed the file,then tried..its working!!!! What was the real problem?

+4  A: 

First thing to do with 500 Internal Server Error is to check the web server's error log for the actual error message. It may or may not be Perl-related - one common case is that the server is not set up to execute CGI programs from the location you asked it to.

Edit: I just tried it out here and the Perl works fine. However, if an id other than 7007 is specified, I get a 500 result because the script completes and exits without sending HTTP headers (or anything else, for that matter). You always need to send HTTP headers or else the web server will report "premature end of script headers" because it stopped receiving data before seeing the end of the header.

Dave Sherohman
I tried a simple perl script to print "hello" from the same location of the above script..Its working fine. And in the web server's log too it is as "Premature end of script headers".:-(
Babu
Oops..is it my bad luck..:-(
Babu
A: 

Please check where the perl is installed on your machine. Please execute command "which perl" on the prompt to get the correct location. You might be having the perl installed on "/usr/bin/perl" instead of "/usr/local/bin/perl". Otherwise your scripts seems fine.

GJ
No.The perl config are good.It is installed on "/usr/local/bin/perl" only.I tried some other scripts from the same location of the above script.They are working fine.Only this one making me trouble.Any thing wrong becoz of CGI declaration?
Babu
Its working fine for me http://<IP>/cgi-bin/g.cgi?id=7007 gives me the XML, but if I try http://<IP>/cgi-bin/g.cgi?id=7 it gives the error as you described. Because in that case page "Content-Type" is not defined in any flow.
GJ
+1  A: 

You want my guide to Troubleshooting Perl CGI. It takes you through the steps you should follow to find the problem.

brian d foy