JDT

 

John Dixon
Technology
Limited

 
Google

Running a CGI Script on a Web Server


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

To run a Perl script that is installed on your computer, which needs to process one or more files that are also on your computer, and where the Perl interpreter is 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.

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.

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 = <IN>) {
   $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 the file after.htm does not exist, the script creates it.

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

Version 2 - the web server version

Here is the web server version of the script. It contains everything that is in version 1, plus a bit more.

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 = <IN>) {
    $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 is to say, all the extra code will not prevent it from running locally.

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 johndixon@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'.


Author: John Dixon
John Dixon Technology Ltd







Go back to Perl Tutorials home page

Go back to Tutorials home page



Earnings Tracker is John Dixon Technology's FREE open source accounting / bookkeeping software tool.

Aimed at UK contractors and freelancers, Earnings Tracker enables you to perform many bookkeeping and accounting tasks, helping you to keep track of your company's earnings and outgoings.

The software is written in PHP and MySQL and is available to use for FREE online, or as a FREE download.

Earnings Tracker can also be used simply as a dividend, corporation tax, or VAT calculator.

Need free accounting software
 


JDT

© 2007-2009 - John Dixon Technology Ltd

Privacy Statement

Terms & Conditions