Advertisment

eight simple steps to scripting with perl

author-image
DQI Bureau
New Update

Many people feel that Common Gateway
Interface (CGI) is esoteric. In reality, it isn't-all that CGI does is pass on information
to a program, and depend on the program to do something about it.

Advertisment

Perl is a fairly popular programming
language on the web when it comes to writing CGI scripts. Here, we shall discuss the
basics of writing a Perl script. Note that you don't need to actually understand Perl
in-depth to get a Perl script up and running. But, as the Raymond's punch-line goes, it
helps.

Determine What You Need

The first thing to determine, without getting unnecessarily and unduly hassled by
the nitty-gritties of Perl programming and CGI scripting, is what exactly you want to
achieve. Try to determine how what you want to do can help your web site, and in what
manner.

For this issue's article, we first started
off by developing a small Perl application which takes one or more email IDs from any
visitor to our Dr A Kula site , and then emails to
them a standard file with some content on it. That, in essence, is the technicality of it
which, understandably, does not reveal the importance of adding the script. But what it
does for our site is very important.

Advertisment

Consider this. You visit a site, and find
it interesting. Your first desire, once you have thoroughly enjoyed the contents of the
site, would be to tell your friends about the site, so that they too can view it.
Normally, human beings being only too human, we all tend to forget to either email or call
up the friend in question. And, ka-boom! Your friend does not get a chance to know about a
wonderful site, not to mention the loss of one potential visitor to the site, insofar as
the web master is concerned.

Adding the Perl script to our site gave us
one major advantage-any visitor who liked our site had an automatic, quick, and painless
way to ensure that information about our site reaches his friends. This, in turn, allows
us to increase the number of visitors to our site. Thus, for us, the script becomes very
valuable-in fact, it ceases to be a mere script, and becomes a device for increasing our
coverage and hits on the web.

As you can see, once you define what you
want to do in terms of a crisp objective, things become easier.

Advertisment

Buy A Good Book

Once your objective is clear, you can get down to the actual scripting.
Primarily, you will need a good book on Perl. The one that we ourselves tried was Perl 5
For Dummies by Paul E Hoffman, an excellent reference source for the beginner. This book
retails for Rs 299, and comes with a bundled CD-ROM, which justifies the cost.

Try Some Autocode Generator

Those who grandiosely call themselves "Real Programmers" are going to
hate me for saying this, but there are times when an autocode generator can be useful. As
the name implies, an autocode generator allows you to generate a script from the
information concerning fields within your input form, and a few mouse clicks. This is a
fairly quick and dirty way to do things, but hey, in the beginning, you ain't gonna write
software to control rockets, right?

One good tool we found was called Perl
Builder, which can be downloaded from . Using this
tool, we could generate the script fast, without many hassles.

Advertisment

Note that the presence of a good autocode
generator does not imply that you don't need a book on Perl. The two actually go hand in
hand, and knowledge gleaned from the book can often prove useful when you try to tweak
your script (aw, c'mon, you don't expect the autocoder to do everything, do you?).

Debug Locally

Run your script locally. Perl Builder allows you to do this if you have Perl
installed (the book Perl 5 For Dummies has the CD which contains Perl). Though you can't
check out everything, you get a fair idea of what can be expected to happen.

Upload The Script

Now that you have a fair idea that your script might work, it is time for the
real test. Upload the Perl script to your CGI directory, normally called
"cgi-bin"; check with your administrator for the actual path; and chmod your
file to 755. What's a chmod, you ask? Well, think of it as an advanced attrib command.
Basically, chmoding your Perl script to 755 gives read, write, and execute powers to the
user (that's you), and enables groups and others to only read and execute the script. You
do this to ensure that nobody else gets dangerous write powers for your precious file.

Advertisment

Test

A Perl script is called from a form, and executes on the server. It is for this
reason that ideally, the Perl script should run from the server for testing purposes also.
Fortunately, the form that passes data to the Perl script can be a local file on your own
hard disk. This, in fact, is the ideal way to test your script-as long as the form is
local, there is only a remote chance that anybody can execute a buggy and potentially
hazardous Perl script.

If ever you need to remember King Bruce and
the stubborn spider, this is it. The most painful process about Perl scripting for the web
is that, after each change to your script, you need to upload it to the web server before
testing it out. But patience, my young Jedi, soon your journey will be complete.

Understanding The Security Aspect

Okay, so you have tested the script completely, you say? Well, what about
security? Do you verify your user input before passing it as a parameter to some
unsuspecting external program? No? Well, sorry buddy, but you still ain't finished yet.
Open up your favorite web browser, and point it to
, and go through the
whole e-text there, especially the part concerning Perl scripting. A few glasses of Royal
Salute on the rocks may be used to ease the mild tremors of agitation you'll face as you
realize the evil sins that can be perpetuated with your humble, harmless Perl script by
the big bad world out there.

Advertisment

Now, Run!

After you fully recover from your hangover, and have successfully fixed all the
bugs, glitches, security hazards, and fuses (the last are a contribution from the friendly
neighborhood electricity board, which suddenly decided that the electrical equipment in
your house should run at 500 volts), it is time to upload the final, fixed, working
version to your site-one last time. Now is also the time to upload the form to your site,
so that the whole world can use it to send data to your dear old Perl script.

If everything collapses, relax. Try the
whole process again, and sooner or later, you will succeed. And if all else fails, you can
still bide your time by singing "Chanced I still, infatuated gambolling spirit"
from The Innocent Adulterer.

Advertisment