Beginning CGI Programming with Perl

Learning Perl

Each “Learning Perl” section teaches you a new Perl fundamental. In this section, you’ll work through a complete Perl programming example. It’s just two lines of Perl so that you can concentrate on the things that make a program work. Lots of times, when you’re working with a programming language, you miss the basics of making a program work because you get lost in the syntax of the programming language. Hello World is a simple and complete example of implementing a Perl program on your computer and moving it to your Internet service provider for testing.

Also in this section, you will be introduced to Perl’s basic storage containers: variables. Variables are explained in a language that the non-programmer can understand. This section is rounded out with an exercise in using the first and simplest of Perl’s storage containers: the scalar variable.

Hello World

Because your programs often act as interfaces to other, larger programs (such as databases), your gateway program’s job is to interface between the larger programs and HTML. Your interface or gateway program performs this task by translating the incoming HTML data to database queries and the outgoing database results into HTML. Perl is an excellent tool for doing this type of data translation because it makes file, text, and other data manipulations easy.

Let’s start with something simple. This program doesn’t have any CGI in it-it’s just straight Perl. Type the following code in your regular editor and then save it to a file named Hello.pl:

01: #!/usr/local/bin/perl02: print "Hello World\n";

The first line of Hello.pl tells your computer where the Perl interpreter is located. You should change this line to the directory path where Perl is located on your computer. If you don’t know where Perl is on your computer, you can find out by asking your System Administrator or by using one of the UNIX commands (whereis, which, or find), which are explained later in Exercise 1.1.

The second line of the program tells your computer to print to the screen Hello World.

In the next portion of this section, you will learn how to make the program print Hello World to your computer screen.

First, you must be logged onto a computer that has Perl on it. Telnet into your Internet service provider and, using FTP, copy the file from your computer to your user account’s home directory. Alternatively, you can have Perl installed on the computer you normally use. In either case, after you are on a machine with Perl installed and you are in the same directory as your Hello.pl file, type the following:

perl Hello.pl

That’s all there is to it. You can make this even simpler by making Hello.pl executable. Type the following:

> chmod 777 Hello.pl

Now just type this:

>Hello.pl

You should see the same Hello World on your screen as before. If you don’t see Hello World, read on to get a better understanding of CGI and UNIX. Don’t forget the “Q&A” section at the end of this Tutorial for some possible solutions if your Hello.pl file does not work.

Tip

When you copy files between UNIX and MS-Windows 95 or 3.1, set the FTP mode to ASCII.

Usually, you transfer files in binary mode so that the computer doesn’t change the file between two computers. But when moving text files between UNIX and Microsoft machines, you want the computer to modify the files.

UNIX and Microsoft use different formats for defining the end of a line. If you transfer your HTML and Perl files using ASCII mode, the FTP transfer will format the end-of-line character(s) to the correct format for the receiving computer.

The Hello World example showed you how easy it is to get Perl to work for you. Now you will learn how easy Perl makes it for you to work with and print data.

Leave a Reply

Your email address will not be published. Required fields are marked *