This page provides information about using Perl (CGI) scripting on our Linux servers. If you have a Windows web hosting account, you should read the Windows Perl page instead.
Introduction to Perl
Perl was originally a UNIX scripting language, but has now been ported to Linux. There are many free Perl / CGI scripts available to download and use these with very few or even no changes to the code.
Every Linux hosting account comes with it's own cgi-bin directory. Although authors may recommend that you place your scripts in the cgi-bin, you should be able to run your scripts from any directory on your account. You may also rename the cgi-bin to something else, such as myscripts.
Please note that it is your responsibility to create and install your scripts and to debug them. If you have difficulty with your scripting, we can recommend a web developer that can create your scripts for you.
The version of Perl installed on the servers is Perl 5.005. This is freely available and can be downloaded from http://www.perl.com, along with extensive documentation. If you wish to test your scripts offline on a Windows based computer, you will need to download ActiveState Perl. See also Offline Development and Testing below.
Ensure you are using a text editor that saves files in plain text format with correct Unix style line endings. These line endings are usually invisible in text editors. Using a Windows program may save the file with Windows style line endings and may cause your script to fail. If you are writing your scripts on a Windows PC, try using WordPad and using Save As and selecting Text. If you would prefer a text editor with more features, try a search on Google.
Offline Development and Testing
If you download and install ActiveState Perl, then you can build and test scripts offline using a Windows computer, saving yourself a lot of time and trouble. To test a script offline on a Windows computer, open an MSDOS window, change to the directory containing your script, and type in code similar to the following:-
perl -w myscript.pl email@example.com message=hello >output.html
In the above example, myscript.pl will be the name of your script, firstname.lastname@example.org message=hello are two name=value pairs that will be read as parameters by your script, and >output.html will create an html file called output.html, which can be viewed in your browser.
You can also set up Personal Web Server in Windows 98+, which will then allow you to test scripts from web pages as if the scripts and pages were on a real Internet server. Building and testing in this manner overcomes a lot of problems quickly and easily.
Please note that there are differences between Perl for Linux and Perl for Windows. If you have developed and tested a script offline on a Windows system, you will need to modify it prior to use on your Linux hosting account. See the Perl for Windows page for these differences.
Path to Perl
All Perl scripts on Unix / Linux servers require you to give the path to Perl on the first line of the script. On our Linux hosting accounts, this will be #!/usr/bin/perl
UNIX Perl scripts normally use the .cgi file extension. Perl scripts on our Linux hosting accounts can use either the .pl or the .cgi extension.
When uploading scripts, you need to ensure you set the correct permissions (CHMOD). As a general guide, use the following permissions:
755 rwxr-xr-x for scripts and executables
711 rwx--x--x for the cgi-bin and directories beneath it
644 rw-r--r-- for data file or configuration files used by the CGI script.
You can use your FTP client to set these permissions. See the FTP page for instructions on uploading files and setting permissions with your FTP client.
Using Downloaded Scripts
You should be able to download and use most Perl / CGI scripts available from scripting resource sites such as HotScripts.com. Follow the instructions provided very carefully and make any changes necessary. If you have any difficulty using the script, contact the script author or check the website that you downloaded the script from for support information.
Please be aware that there are issues with some ready to use scripts such as older versions of Matt Wright's Formmail. Spammers use automated tools to search websites for Formmail so that they can relay email through this script. If you need to use Matt Wright's Formmail script, please ensure you are using the latest version (v1.9 or above) and that you have configured "referers" correctly to prevent spamming. Please also rename the script to something obscure and place it in a different directory, not the cgi-bin.
It is also a wise idea to take similar precautions with other downloaded scripts just in case there are issues you may not know about. Where possible, rename the script and place it in a different directory. Always use the latest versions of any code, and check back every now and then for updated versions.
Unix / Linux scripts often require you to provide the path to Sendmail in order to allow a script to send an email. On our servers, the path to Sendmail is /usr/sbin/sendmail. The line you need to modify in your code may look like this:
$mailprog = '/usr/lib/sendmail';
Demonstration script for sending email
You can use the following script to send yourself an email from a web form. This is very useful for a simple contact form on a web page. Please only use this for sending email to your own email addresses, not to other email addresses, as no protection is built in to prevent spammers from relaying through this script. You should save this file as mailtest.cgi and upload it in ASCII mode. Remember to set the correct permissions.
use CGI qw(:standard);
print MAIL "To: $recipient\n";
print MAIL "From: $fromaddress\n";
print MAIL "Subject: Enquiry from website\n\n";
print MAIL "Name: $name\n";
print MAIL "Email: $email\n\n";
print MAIL "Enquiry: $enquiry\n";
print "Content-type: text/html";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
You should use the following html page for testing the above script. Copy and paste the code into your own html editor, save it as mailtest.html and upload it to your webspace in ASCII mode.
<title>Mail Test Page</title>
<form action="mailtest.cgi" method="post" >
<td width="50%" align="right">Name:</td>
<td width="50%"><input type="text" name="name" size="40"></td>
<td width="50%" align="right">Email Address:</td>
<td width="50%"><input type="text" name="email" size="40"></td>
<td width="50%" align="right">Enquiry:</td>
<td width="50%"><input type=text name="enquiry" size="40"></td>
<input type="submit" value="Submit"><input type="reset" value="Reset">
Troubleshooting and Debugging
If you are not careful, your CGI script will fail. Common errors are "File not found", "CGIWrap error" or "500 Internal Server Error".If you receive one of these errors, use the following checklist first:
Did you save the file correctly?
Did you upload the file in ASCII mode?
Did you upload to the correct directory?
Did you set the correct permissions on the file?
Did you set the correct permissions on the directory?
Did you use the correct path to Perl?
Did you use the correct path to Sendmail?
Have you used the correct filename? Remember that filenames on Linux are case sensitive, so XYZ.cgi is different to xyz.cgi.
The above checklist should cure most problems quickly and easily. If you have done everything correctly as above, it is likely there is an error in your script. Check that you have used semi-colons at the end of statements, that you have declared all variables and that there are no other obvious errors or omissions.
If your script still doesn't run, try using CGIWrap to debug your script. This runs your script and forces display of any errors instead of displaying Internal Server Error or CGIWrap Error and so on.
To use CGIWrap, you need to enter cgiwrapDir/cgiwrapd/ into the URL in your browser, between the domain name and the path and/or filename. For example:
When you run the script, the script output will be displayed in the browser window. If the browser attempts to send you a download of the file, you probably need to change the HTML output or provide the content type. See the example script above to see how to use and space the content type, doctypes, title, head and body tags.
If the output shows a series of error messages, then the first one will often indicate where the error is in the script and what the error is. For example, if you see "bareword" then it is likely that you have failed to correctly declare a variable. Correct the first error you see and try running the script again as one simple code error can cause many other errors to be displayed. Keep working until all errors are cleared.
If you see the script outputs correctly using CGIWrap but still fails without CGIWrap, then you have made a mistake with permissions, uploading, saving, and so on. Go back and correct these.
If your script is still not working after checking all of the above, then you haven't checked all of the above properly or your code is wrong in another way. Check code very throughly. Sooner or later you will find the error that is causing you so many problems.
You can find more information about Perl together with some useful scripts and tutorials by following the links in our useful websites page.