|
This page provides information about Perl (CGI) scripting on Windows 2000 hosting accounts. If you have a Linux hosting account, please refer to the Perl for Linux page.
Introduction to Perl Perl was originally a UNIX scripting language, but has now been ported to Windows. The syntax and structure of Perl is the same on Windows machines as it is on UNIX machines, although there are a few changes that need to be made to UNIX based Perl scripts in order to make them run on Windows machines, and there are some modules or functions specific only to UNIX or Windows.
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.
Version The version of Perl installed on the servers is the latest version of ActiveState Perl. This is freely available and can be downloaded from http://www.activestate.com/, along with extensive documentation. We recommend that you obtain ActiveState Perl for building and testing scripts offline.
Offline Development and Testing If you download and install ActiveState Perl, then you can build and test scripts offline, saving yourself a lot of time and trouble. To test a script offline, open an MSDOS window, change to the directory containing your script, and type in code similar to the following:-
perl -w myscript.pl email=me@mydomain.com message=hello >output.html
In the above example, myscript.pl will be the name of your script, email=me@mydomain.com 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.
Path to Perl UNIX scripts require you to provide the path to Perl. This is not required in Perl scripts in Windows. The first line in a UNIX Perl script will normally be something like #!usr/bin/perl. To run on Windows 2000 servers, this should be changed to #!perl
File Extensions UNIX Perl scripts normally use the .cgi file extension. Perl scripts on our system should use the .pl extension.
File Permissions UNIX documentation will often advise you to set file permissions (CHMOD) to 777 (or similar). This is unnecessary in the Windows version of Perl as scripts inherit permissions from the Windows operating system.
Sendmail UNIX perl scripts often use an external mail program called 'sendmail' for sending email. Windows 2000 servers use Blat for sending email. See the demonstration code below. If you obtain a UNIX based script that uses sendmail, replace all the email code in that script with the appropriate Blat code.
Blat demonstration program for sending email The following script obtains values from a form and sends an email by using the Blat program. If your cgi scripts are not working and you suspect that there is a fault with the servers, please run this script from your website to test whether scripting is working. You should save this script as mailtest.pl
#!perl
use strict;
use CGI qw(:standard);
# Receive values from a form. You could hardcode these into your script
my $sname=param('sendername');
my $saddr=param('senderaddr');
my $rname=param('recpientname');
my $raddr=param('recipientaddr');
my $subject=param('subject');
my $server="smtp.yourdomain.com"; #You should change this to your own domain name
# Receive the message you want to send. You can also hardcode text into your script
# using $message="This is a text message"; . You can also add additional fields to
# the text message by adding lines such as $message .=param('anotherfield');
my $message=param('message');
my $lt='<';
my $gt='>';
# Save the message into a text file. The text file is automatically deleted by blat.
open (messtxt,">message.txt");
print messtxt ($message);
close messtxt;
# Build the Blat command line. This should all be on one line so
# if it has wrapped, edit it to make sure it is on one line
my $blat="blat.exe message.txt -s \"$subject\" -t \"$raddr\" -server $server -f \"$saddr\"";
# Now execture Blat to send the email
system($blat);
# The following section outputs html to your browser to confirm the email was sent.
# You can replace this with your own html code, or you can use redirection to an html file.
print "Content-type: text/html\n\n";
print $blat;
print "Done.";
print "Mail sent to $raddr.";
|
You should use the following html page for testing the above script. Copy and paste the code into your own html editor.
<html>
<head>
<title>Mail Test Page for Blat</title>
</head>
<body>
<form action="cgi-bin/mailtest.pl" method="post" >
<table>
<tr>
<td width="50%" align="right">Sender's Name:</td>
<td width="50%"><input type="text" name="sendername" size="40"></td>
</tr>
<tr>
<td width="50%" align="right">Sender's Address:</td>
<td width="50%"><input type="text" name="senderaddr" size="40"></td>
</tr>
<tr>
<td width="50%" align="right">Recipient's Name:</td>
<td width="50%"><input type="text" name="recipientname" size="40"></td>
</tr>
<tr>
<td width="50%" align="right">Recipient's Address:</td>
<td width="50%"><input type="text" name="recipientaddr" size="40"></td>
</tr>
<tr>
<td width="50%" align="right">Subject:</td>
<td width="50%"><input type="text" name="subject" size="40"></td>
</tr>
<tr>
<td width="50%" align="right">Message:</td>
<td width="50%"><input type=text name="message" size="40"></td>
</tr>
<input type="submit" value="Submit"><input type="reset" value="Reset">
</table>
</form>
</body>
</html>
|
More Information You can find more information about Perl together with some useful scripts and tutorials by following the links in our useful websites page.
|