pjau
pjau

Reputation: 925

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?

Upvotes: 1

Views: 5205

Answers (7)

Dave Everitt
Dave Everitt

Reputation: 17876

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.

Upvotes: 0

brian d foy
brian d foy

Reputation: 132802

Perhaps you need my Troubleshooting Perl CGI scripts

Upvotes: 3

Sinan Ünür
Sinan Ünür

Reputation: 118128

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";

Upvotes: 2

Rafe
Rafe

Reputation: 3066

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:

./helloworld.pl

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.

Upvotes: 1

user107498
user107498

Reputation:

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.

Upvotes: 3

Anirvan
Anirvan

Reputation: 6354

Read the official Perl CGI FAQ.

That'll answer this, and many other questions you may have.

For example: "My CGI script runs from the command line but not the browser. (500 Server Error)"

Hope this helps!

Upvotes: 15

also
also

Reputation: 974

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".

Upvotes: 10

Related Questions