Today Els asked me on Live Messenger if I knew of a way to check if a webserver has Perl installed and available via CGI. So I replied to her question with the following small Perl program:
#!/usr/bin/perl
print "Content-type: text/plain\n\nHet werkt!\n";
She copied the Perl snippet to the editor she was using, probably TextPad, and saved the Perl program to a file named test.pl After she had uploaded this Perl program to the cgi-bin directory of the webserver, and making the program executable (chmod 755), Els entered the URL leading to the program on the webserver in a browser and was greeted with:
Het werkt!
which is Dutch for "It works!".
There are several reasons other then no Perl available that might make the above CGI script not work. In many cases the first thing to check, especially when you get a "500 Internal Server Error", is the error_log which is normally located in the same directory as the access_log of your web site.
The first line of the Perl program contains the path to the perl (notice the lower case 'p') program, which is wat the webserver is going to use to execute the Perl program (script) when it has the executable bit set (thanks to chmod 755). It might be the (rare) case that perl is located in a different directory, for example /usr/local/bin/. If in doubt ask your hosting provider.
If you wrote and tested your script on a operating system that uses different character(s) for a line ending compared to the operating system the webserver is running on, the script doesn't work if you upload it using binary mode.
If the webserver is running on GNU/Linux you' might see the following issues when using "PC" line endings instead of "UNIX":
bash: ./test.pl: bad interpreter: No such file or directory.
Either correct the line ending issue after you upload or before.
If you're using TextPad to edit your Perl programs you can either manually set the file format in the Save As dialogue window to "UNIX" or, more permanently, set "Create New Files as" to "UNIX" in the Perl document class.
When the webserver is running on a UNIX or similar operating system you might have to change the permission of the Perl program after uploading. If you don't, you might get a "403 Forbidden" when you try to access the URL of the script.
The Apache HTTP server normally uses the same method as a shell to run a Perl program: if the file has the execute bit set and the file starts with a shebang (#!) the part after the shebang specifies the program which should be used to process the file. For a program written in the Perl language this is normally the perl program located in /usr/bin/ so the first line becomes:
#!/usr/bin/perl
In order for the Perl program to work, the Apache HTTP server must be able to read the program (script), and the program must have the execute bit set unless Apache uses the extension of the program to decide how it's executed. On UNIX (or similar operating systems) chmod is used to set the permissions of the Perl program in the former case.
In most FTP clients you can change the permission of a file. For example in FileZilla:
Note that 755 is an optimistic value, and depending on the configuration of the web server it might be too optimistic.
Normally CGI programs have a dedicated directory called cgi-bin which in my opinion should be located outside the document root. In other words: the directory containing the index.html (for example) of your website shouldn't contain the cgi-bin directory. If you upload the program to a directory other then cgi-bin the program might not work at all, again which in my opinion is a good thing. If you want a nice URL you can often achieve this with URL rewriting, for example mod_rewrite if you host your site on an Apache HTTP server.
A simple extension to the given CGI program is adding the following line after the print statement:
print "$_ = '$ENV{ $_ }'\n" for sort keys %ENV;
which outputs a list of all environment variables visible to the CGI Perl program and associated values. Note that single quotes are printed to the left and the right of the value and are not part of the value itself.