Why doesn't my Perl CGI script work?

Why doesn't my Perl CGI script work?

I really do not get how to run a Perl file. I have uploaded my .pl to the cgi-bin then chmod to 755. Then when i go to run the file i just get a 500 internal server error.

**/cgi-bin/helloworld.pl**   #!/usr/bin/perl  print 'hello world'; 

Any ideas as to what I am doing wrong?

Why can't I say print $somehash{$var}{fh} “foo”?


How can I find the free space available on mounted points?

How do you create a Perl module?


You probably need something like.
print "Content-type: text/html\n\n"; 
before your print statement.

Take a look at http://httpd.apache.org/docs/2.0/howto/cgi.html#troubleshoot. It would help to know what server you are using, and the exact error message that's showing up in the server's logs.

I'd guess that, if you are using Apache, you'll see something like "Premature end of script headers"..


Look into using CGI::Carp to output fatal errors to the browser.

use CGI::Carp qw(fatalsToBrowser);. Also, please definitely do use the CGI module to output any needed information such as headers/html/whatever.

Printing it all is the wrong way to do it.

. EDIT: You will also definitely be able to check an error log of some sort.



Perhaps you need my Troubleshooting Perl CGI scripts.


First, find out the path to perl on that system and make sure the shebang line is correct.

Giving more information about the system and the web server would also help others diagnose.. Then, try:.
#!/path/to/perl/binary  use strict; use warnings;  $| = 1;  use CGI qw( :default );  print header('text/plain'), "Hello World\n"; 


Make sure that you can run the script from a shell prompt, without invoking it through Perl.

In other words, you should be able to go to your cgi-bin directory and type:.
and get output.

If that doesn't work, fix that.

In looking at the output, the first line must be:.
Content-Type: text/html 
(Or text/plain or some other valid MIME type.). If that's not the case, fix that.. Then you must have an empty line before the body of your page is printed.

If there's no empty line, your script won't work as a CGI script.

So your total output should look like this:.
Content-Type: text/html  hello world 
If you can run your script and that's the output, then there's something weird going on.

If Apache is not logging the error to an error_log file somewhere, then maybe there's some problem with it..


Did you enable Apache to server .pl files as CGI scripts? Check your Apache config file, or (quick but not guaranteed test) try changing the file extension to .cgi.

Also, make sure your shebang line (#!) is at the very top.

Finally, check the line endings are Unix if your server is Linux.

And yes, test it from the command-line, and use strict; for better feedback on potential errors..

85 out of 100 based on 85 user ratings 1285 reviews