This blog is about anything that interests me. On the whole, as I'm a web developer, there will be a lot of webbie posts, but I may well stray off topic every now and again.

Thursday, May 24, 2007

Running a CGI Program

For many years I have been writing Perl scripts to process ASCII files of one sort or another on my computer. I typically do this when I need to reformat or tidy up a series of HTML pages, for example.

To run a Perl script that is installed on your computer, which needs to process one or more files on your computer, and where the Perl interpreter is also installed on your computer, is very simple - you just need to double-click the perl script and it does the business - assuming that everything is set up correctly of course, for example, the location of the perl.exe program is defined in your path. You can also open a DOS window and type perl perlfile.pl to run a script (where perlfile.pl is the name of the Perl script you want to run).

However, when it comes to running a Perl script, or CGI script, on a web server, things can be a bit trickier - not too tricky, but a bit trickier.

In this article I'll look at two versions of the same script: one that will run quite happily on a local machine (by double-clicking the script, for example), and one that will run on a web server.
You can download a zip file containing all the files used in this article from http://www.computernostalgia.net/downloads/cgi.zip

The script itself is very simple - it opens a file, changes some text inside the file, and then saves the file under a different name.

Version 1 - the local version

Here is version 1 of the script. This is the version that will run locally on a computer, without a web server is sight. Note that I've inserted spaces at appropriate places to prevent the code from being processed by your browser. I've done this wherever necessary in this article. For this reason, if you want to run either of the scripts, you should probably use the versions contained in the zip file (http://www.computernostalgia.net/downloads/cgi.zip) as these are the scripts I used when testing that they worked!

localScript.pl

$name = "before.htm" or die "cannot assign to variable: $!";
rename $name, "$name.bak" or die "cannot rename: $!";
open (IN, "<$name.bak") or die "cannot open: $!"; open (OUT, ">$name") or die "cannot create: $!";
undef $/;
while ($line = ) {
$line =~ s/hello world/goodbye cruel world/s;
(print OUT $line);
}
close (OUT);
close (IN);
rename "before.htm", "after.htm";
rename "before.htm.bak", "before.htm";

This script opens a file called before.htm, uses a regular expression to change the string 'hello world' to 'goodbye cruel world', and writes the contents to a file called after.htm. If after.htm does not exist, it is created.

The file before.htm simply contains one line - hello world. So it's not even a proper HTML file in fact, but that doesn't matter for this exercise as it's the script that's important, not the file that's being processed.

Version 2 - the web server version

Here's the web server version of the script. It contains everything that's in version 1, plus a bit more. Again, I've inserted spaces where appropriate so it's best to use the zipped version if you want to run the script.

webScript.cgi

#!/usr/bin/perl -w
use CGI qw(:all);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
warningsToBrowser(1);
use strict;
print header;
my $name = "before.htm" or die "cannot assign to variable: $!";
rename $name, "$name.bak" or die "cannot rename: $!";
open (IN, "<$name.bak") or die "cannot open: $!"; open (OUT, ">$name") or die "cannot create: $!";
undef $/;
my $line;
while ($line = ) {
$line =~ s/hello world/goodbye cruel world/s;
(print OUT $line);
}
close (OUT);
close (IN);
rename "before.htm", "after.htm";
rename "before.htm.bak", "before.htm";

A couple of initial points to note:

1. Why does the script have a .cgi extension instead of a .pl extension? CGI is an abbreviation for Common Gateway Interface, which is a specification for transferring information between a web server and a CGI script. So CGI iteself is not a language, but CGI scripts can be written in a number of languages of which Perl is one. If you write a Perl script with a .pl extension, and then change that extension to .cgi, the script becomes a CGI script, and providing it conforms to the CGI specification, it will run on a web server.

2. If you call this script webScript.pl it will run without any problems on a local disk - just as version 1 did. That's to say, all the extra code will not prevent it from running locally.
Ok, lets go through the script line by line to see what's going on.

Running the script

To run the script you need to first upload the script and the file before.htm into the cgi-bin directory on your web server. On your web server the cgi-bin directory might be called something else, but it will probably be recognizable as the place where cgi scripts need to be located.

By default, when you upload a file onto your web server it will probably have permissions of 644. You need to change these to 755 so that the script can be run by anyone. Your ISP should provide you with a way to do this. If not, contact me at john@dixondevelopment.co.uk and I'll send you a script to change the permissions for you.

Once you have uploaded the files and changed the permissions, all you need to do is browse to the script in your favorite browser. If the browser window is blank, then everything has probably worked OK. Check in your cgi-bin directory to see if the file after.htm has been created and that it contains the words 'goodbye cruel world'.

For a fast and reliable web development service, visit:

http://www.dixondevelopment.co.uk

For great articles and photos relating to the history of the computer, visit:

http://www.computernostalgia.net

Labels: , ,

Tuesday, May 15, 2007

Introduction to Object Oriented Programming

Object Oriented Programming (OOP) is something that for many years I struggled to get to grips with. I often found when reading a programming book I would be fine until I got to the chapter that introduced OOP, and then after that things would go downhill very quickly. It wasn't until I started learning to program in PHP two or three years ago that I finally started to get my head around OOP - well at least the PHP 4 version of OOP.

In this article I'll try to describe (at a very simple level) my interpretation of OOP.

OK, let's get started with OOP. Basically, there are things called classes. Just think of a class as being a 'type' of thing. For example, if you have a website that lets users register and log on to the site, you might set up a user class to enable them to do this.

Classes can be described and they can also do things. So, for example, for our user class we might describe (identify) users by means of their names, passwords, and email addresses. 'Name', 'password' and 'email address' are called data members. The user will also be able to do things, for example, register with the site, log on to the site and, say, create articles. 'Register', 'log on' and 'create article' are called methods.

When a user attempts to log on to the website, probably by entering their name and password into a form, the 'log on' method can be called to check that the user is allowed to access the site. This will probably be done by checking the user's name and password against a list of users stored in a database.

When a user attempts to log on, they create a particlar occurrence or instance of a class. So, for example, if the class is called 'users', an instance of that class might be 'john'. The class describes the users, whereas john is an actual user - he's an object.

For an efficient web development service, visit:

http://www.dixondevelopment.co.uk

Remember the good old days - visit:

http://www.computernostalgia.net


Earnings Tracker - FREE Accounting Software

Friday, May 11, 2007

HTML Tables versus Div Layers

I've been developing web sites since the mid nineties. In those early days life was a lot simpler than it is today. When I look back at sites I developed then, I'm surprised at how plain they look, although I was very pleased with them at the time - and I guess I still am really.

In those days it was simply a case of putting HTML tags around bits of text. There was no talk of CSS files or Div Layers or anything complicated like that. Nowadays, a lot of the techniques we used then are frowned upon, and we are all encouraged to use the latest techniques - I suppose there's nothing wrong with that though.

Up until recently I had always used tables to lay out blocks of text, etc in a browser window. But for the last site I did, I decided to take the plunge and use Div Layers (defined within a CSS file) throughout. My reasons I guess were peer pressure - ie people telling me that I should do it - and the belief that I would be able to lay out items very precisely so that things appeared the same from browser to browser.

It's true that the code you need to enter onto individual web pages is a lot simpler than if you use tables, but I have to admit I found some of the CSS coding a bit tricky. For example, I had real difficulty in positioning layers inside a centered container layer. Now I've got it working it doesn't seem too bad, but I had to scratch my head for a while in order to get it to work.

I'm just about to start on a new site and, yes, I'm planning to use CSS layers again. I'm not anti tables though - I'm a firm believer in developers using whatever techniques work for them. Tables have served me well over the years and I guess I will still use them from time to time.

Computer history articles and photos - relive the good old days at http://www.computernostalgia.net

Earnings Tracker - FREE Accounting Software

Labels: , ,