Perl and Apache on Mac OS X

By Paulus, 22 July, 2008

The other day while I was modifying a client's site I saw that his contact form was in PERL. Recently I just got a Mac Book Pro. I've set up XAMPP and Eclipse on it to do my PHP programming, but I didn't count on getting into any PERL. As a challenge I thought it would be fun to do some PERL programming.

I first installed XAMPP. Right out of the box I was able to do PHP programming and access MySQL databases which saved me some time configuring and trouble shooting issues. Or so I thought. I've written PERL scripts before, but never done anything that would run on a web server. When it comes to writing pages in PHP, just write the code and it works. That's because the headers are sent for you. As a test I put the following into my script:

#!/usr/bin/perl

print("Hello!");

Expecting it to work. However I would get an error message saying Error 500. I looked at the log file /Applications/xampp and saw that the script was 'exiting prematurely' which did not make any sense at all. I could run the PERL script just fine on the command line.

After doing some digging around, about an hours worth I finally found what I was looking for. I needed to print the headers. In order to get my PERL script to work with Apache I needed to add the following:

use strict;
use CGI;

my $cgi = new CGI;

so now the script looks like:

#!/usr/bin/perl

use strict;
use CGI;

my $cgi = new CGI;

print $cgi->header . $cgi->start_html . "hi" . $cgi->end_html;

The key to this script is the $cgi->header function call. With out it, you will get an internal server error. There are other functions associate with the CGI class. The start_html and end_html are the beginning and closing HTML tags.