Perl + Apache

Here's a real blast from the past: getting Perl scripts to work with a web server.

Nowadays, a setup like this would be a mere historical curiosity, but the reason this came up is because I'm teaching a class on open source technologies at Ateneo de Davao. I gave my students a set of exercises on Perl. Nothing fancy, just connect to a database and print something out. Then the thought came to mind: as an extra challenge, how about I also include web output.

Easier said than done, apparently. There is the libapache2-mod-perl2 module, but once you get that installed, what do you do next? Some minor Apache surgery is still required.

First, you need to add the following lines to /etc/apache2/apache2.conf:

AddHandler cgi-script .pl
<Files ~ "\.pl$">
Options +ExecCGI
</Files>


This tells Apache that anything with the .pl extension has to be treated as a CGI script.

Then, restart the Apache web server.

As an additional caveat, any Perl script that generates a web page must always send out the right headers. Otherwise an Internal Server Error will occur. Actually, it's really as simple as adding the following lines before sending out any other data.

print "Content-type: text/html\n\n";

The script must also be marked executable.