views:

640

answers:

8

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?

+10  A: 

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

also
Just added that line and it isn't helping.
Paul Janaway
Using print like that is considered bad form. Look into the CGI module.
misterMatt
+14  A: 

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!

Anirvan
+2  A: 

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";
Sinan Ünür
nope didn't work. the path is definately /usr/bin/perl the os is linux, its apache
Paul Janaway
@Paul Are you just stringing people along: You do not get a 500 status from Apache without an entry in the error log. Post the information in the error log. There is no point in constantly trying to guess with no information.
Sinan Ünür
i looked at cpanel >> error log and its not saying anything in there
Paul Janaway
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html: HTTP header fields, which include general-header (section 4.5), request-header (section 5.3), response-header (section 6.2), and entity-header (section 7.1) fields, follow the same generic format as that given in Section 3.1 of RFC 822. Each header field consists of a name followed by a colon (":") and the field value. Field names are case-insensitive.
ysth
@ysth I did not know that. Thank you for pointing it out.
Sinan Ünür
+3  A: 

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.

misterMatt
Don't use the CGI module for output. That's so 1990. Use templates instead. :)
brian d foy
If he needs templates he should be using a framework.
misterMatt
+1  A: 

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.

Rafe
A: 

To echo the fine answer by misterMatt above, when I started Perl development using Carp helped a great deal.

Just this little line.... CGI::Carp qw(fatalsToBrowser);

Julian Cook
+3  A: 

Perhaps you need my Troubleshooting Perl CGI scripts

brian d foy
A: 

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.

Dave Everitt