Scripting - Part 1

PC Pro logo Posted: 1st April 2001 | Filed under: Press Articles, Technical
Author: Paul Ockenden
First Appeared in PC Pro 2001

Welcome to a brand-new Real World column that isn't really new at all. Regular readers will know that we've been covering server-side programming in the Web Business RWC column for the past few years. But splitting this content out into a new column will allow us to cover topics in much greater depth and detail - the programming in here and the business side of things in our other column. For the first couple of months we'll start by presenting a broad overview of server-side programming for Web sites, taking a look at what it lets you do and at some of the various technologies available.

The choice of a server-side programming technology can be a difficult decision. The bean counters will always try to win the argument on price alone, but other factors such as scalability of the solution and the ability of your chosen language to do what you need are far more important. Another factor to consider is what in-house skills your team may already have: if you employ an abundance of PHP programmers, then it might make sense to use their skills rather than forcing them to churn out Visual Basic (which they'll almost certainly hate). Let's begin our look at some of the languages that are available.


Perl was the original scripting language of the Internet, and a few years back it was just about the only scripting language you ever saw. This has now changed, but Perl's 'Grand daddy of the Web' heritage ensures it a certain geek status. There are huge archives of existing code available, one of the most famous being Matt's Script Archive (

Do be careful about the quality of some of these scripts though, as much of the freeware Perl code floating around the Internet is pretty ropey. In particular, watch out for multi-user aspects such as file locking, which unfortunately are often completely ignored, presumably because when most of these scripts were written the chances of two people hitting the same Web site at the same time were remote.

Perl has a free and open distribution policy, with licensing terms that are less stringent than the typical GNU software agreement. There are commercial versions available though, with full and often expensive support packages, which are particularly useful if you work in one of those organisations where the management subscribes to the 'we only use software we can pay for' philosophy.

When people talk about CGI code they often mean Perl scripts, and on every typical Web server you'll find a few Perl scripts held in a directory called /cgi-bin. Many Web hosting facilities provide a global cgi-bin directory shared among all of their users, which contains a number of useful scripts. Demon, for example, supplies scripts for adding a counter to your page, doing server-side image maps and another for emailing the results of a form.

Form data collection is the most common application for server-side scripts, especially for Perl. Such scripts are used to extract data from an HTML form and process the results. Forms may use GET or POST processing: GET forces all of the form's data onto the end of the URL, while POST encodes it within the HTML request, and Perl scripts can be used to process either type of form. Here's a bit of Perl that retrieves name, address and phone number data from a form and appends these details to a file:

#! /user/bin/perl -w
use strict;
my($dataFile) = "data.txt";
saveData(\%fields, $dataFile);
print("Content-type: text/html\n\n");
print("<HTML>\n<HEAD><TITLE>Thank you</TITLE></HEAD>\n");
print("<BODY>Thank You.</BODY>\n</HTML>\n");

sub getData {
my($stuff) = shift;
my($buffer) = "";
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$buffer = $ENV{'QUERY_STRING'};
else {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
foreach (split(/&/, $buffer)) {
my($key, $value) = split(/=/, $_);
$value =~ tr/+/ /;
$value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;
%{$stuff}->{$key} = $value;

sub saveData {
my($stuff) = shift;
my($file) = shift;
open(FILE, ">>$file") or die("Problems opening the data file.");
print FILE ("$stuff->{'name'}~");
print FILE ("$stuff->{'email'}~");
print FILE ("$stuff->{'telephone'}");
print FILE ("\n");

Most of what's going on here should be fairly obvious, except for a few lines within the getData function. The code first establishes which processing method was specified by the HTML form calling this script. If it was GET, then the script grabs the data from the query string (for example, everything that's after the question mark in, whereas if the form used POST, then the script reads the data from the standard input stream instead. It then splits the data at each ampersand separator character and uses a foreach loop to cycle through each key/value pair, splitting it at the equal sign, converting any plus characters to spaces and then decoding any hexadecimal-encoded characters remaining in the string.

In this simple example, the results from the form are then written to a file called data.txt (and for our example we've adopted the usual Perl file locking method; that is, none), but there's no reason why the data couldn't be sent via an email, added to a database, entered into an order-processing system, or even used to create more Web pages. Given this last option, it shouldn't require too great a stretch of the imagination to see how form processing, in whatever language, might be used to create a simple forum or chat room system.

Although, as you can see from the example, Perl is a very powerful language, it's pretty much out of fashion in the Web development world. You'll find more appropriate software for most tasks, although Perl is still often useful for 'quick hack' solutions to small problems.


ASP (Active Server Pages) is Microsoft's server-side scripting technology, and as such will be detested by a significant proportion of the programming community for no other reason. That's actually an important consideration: people seemingly born with 'Remember, you hate Bill Gates' tattooed inside their eyelids are unlikely to get much job satisfaction if you force them to use one of the Evil Empire's products.

ASP runs as part of PWS (Personal Web Server) on Windows 9x and NT Workstation, and Internet Information Server (IIS) on NT Server and Windows 2000, and in its most basic form it comes free as part of the operating system. Windows 2000 users should find IIS on their installation CDs. Those running NT or Windows 9x should look out for a recent version of the NT4 Option Pack.

ASPs contain the script code within the HTML page itself, as in the following example:

<TITLE>A simple ASP example</TITLE>
The date is <%=Now%>

As you'll see this is all standard HTML, except for the part between the special <% %> tags, and what sits in there is pretty much standard VBScript, which should be familiar to anyone who uses the main Visual Basic product, or who has produced scripts or macros for any of the Microsoft Office products. Here we're simply using the Now function to return the current date and time. We'll use this same example for several of the languages covered over the next few months. In a way it's a revelation to see just how similar many of the server side programming languages actually are.

When the server gets a request for an ASP file, it pre-processes that file looking for these scripting tags, and the code within them is executed and the results merged back into the HTML. For example, the following fragment of ASP script:

<% FOR x = 2 to 6 %>
<FONT size=<=x%>>Font size <%=myx%></FONT><BR>
<% NEXT %>
will generate the following HTML:
<FONT size=2>Font size 2</FONT><BR>
<FONT size=3>Font size 3</FONT><BR>
<FONT size=4>Font size 4</FONT><BR>
<FONT size=5>Font size 5</FONT><BR>
<FONT size=6>Font size 6</FONT><BR>

Most examples, like those above, suggest that ASP is an embedded VB technology, but although ASP defaults to VBScript as its scripting language it's equally capable of supporting other server-side languages such as JavaScript and PerlScript. The fact that most ASPs are created using VBScript brings advantages, given the number of people out there in jobseekerland with VB skills. On the other hand, VB doesn't exactly encourage an elegant coding style. Another plus point for ASP is that because it has become such an established technology, many HTML editing programs have got at least some support for it, even if this mostly means detecting the <% %> tags and displaying everything between them in a different colour.

The biggest problem for ASP is that the script code and the HTML are all mixed up within a single file, which goes completely against the usual rule of keeping content separate from form. Even if you're not one for following the rules, it should be pretty obvious that those with graphic design skills might not be the world's best programmers and vice versa, so you'll wind up with two people working on one file - a potential nightmare scenario which will require a very good source code control system.

The problem can be alleviated to some extent by keeping most of the script code on each page within one block, then moving that block out into an include file, which gives the designer and the programmer their own file to work on.

Such included code files work well enough for pure processing and database retrieval code, but they're not ideal when looping through a dataset or spitting out tables. Take a look at the following example:

<% Option Explicit %>
<TITLE>Web safe colour chart</TITLE>
<% Dim r,g,b,cr,cg,cb,c
for r = 0 to 15 step 3
for g = 0 to 15 step 3
for b = 0 to 15 step 3
cr = string(2-len(hex(r*16+r)),"0")+hex(r*16+r)
cg = string(2-len(hex(g*16+g)),"0")+hex(g*16+g)
cb = string(2-len(hex(b*16+b)),"0")+hex(b*16+b)
c = cr&cg&cb
response.write "<tr><td width=""100"" bgcolor=""#"
response.write c
response.write """> </td><td>#"
response.write c
response.write "</td></tr>"

This code displays the Web-safe HTML colours in a very simple tabular format, and a quick glance will show that it would be counterproductive to separate the programming parts from the plain HTML here. Where you to ask a designer to make this page look pretty, they'd struggle with the construction and formatting of that table.

Despite such problems, ASP continues to gain ground among our corporate clients - particularly those who've declared themselves to be a 'Microsoft shop' - and it's also often used to display example code in magazine programming columns. The logic is that it gives beginners something they can copy verbatim, whereas those who have skills in other Web scripting languages will also have the skills to translate the ASP into their chosen language.

Next month we'll continue our look at server-side programming, examining more languages and technologies including ColdFusion, PHP, JavaServer Pages and others. As with all Real World Computing columns, if there's anything in particular that you'd like us to cover, or if there are new products or technologies that you think we should know about, please feel free to drop either of us a quick email.