Categories:

Ada's Introduction to CGI Programming

As a student who's learned C programming at school, I had a little head start when taking on CGI programming. Having said that, I'm still fairly new to the game, so, as the title suggests, this is simply an introduction to CGI programming. Here, I'm going to discuss the basics of CGI programming (In Perl)- the structure of it, how to define variables in Perl, open close files, and print out pages.

Introduction

There are basically two ways to write a CGI script- in C, or Perl. The advantage of C is that it's a compiled language, so it tends to be more efficient than Perl. However, Perl, with it's advanced data handling capabilities (such as regular expressions) and open source nature, makes it the choice of most people when it comes to CGI programming. This tutorial is on how to program in CGI using Perl.

Before you can begin programming using Perl, you'll need a copy of the Perl interpreter on your computer. The interpreter is needed to interpret your Perl code; like Borland C++ is to your C code. You can get a copy of it at Perl.com. If you don't want to install the interpreter onto your own computer, and your web host has Perl installed, you can alternatively test out your scripts by uploading them and testing them remotely.

-Basic structure of a CGI script

CGI scripts are really just text files saved with an extension of either .cgi or .pl. The extension tells the Perl interpreter that this is a CGI script, and that the interpreter should process it as such. A basic CGI script looks like the following:

#!/usr/bin/perl

print "Content-type: text/html\n\n";
print "<HTML><HEAD>";
print "<TITLE>This is a test</TITLE>";
print "</HEAD><BODY>";
print "My first CGI script!";
print "</BODY></HTML>";

The first line of your script, as always, should point to where the Perl interpreter on your machine is. Following that is a basic Perl code that dynamically generates and displays a webpage. This is done by using the "print" command of perl to write out the underlining HTML code for the page. Notice the line:

print "Content-type: text/html\n\n";

This is required whenever you wish to use Perl to print out a webpage- it tells the browser that the following is HTML content.

Like any other CGI script, to install and test it, simply save the code with an extension of .cgi or .pl, upload it, chmod it, and run it in your browser. If you need help on how to install a CGI script, please refer to my tutorial on it.

-Variables in Perl

Variables are used to hold static values and help further manipulate it in your program. Perl supports most of the well known variable types, such as scaler or array variables. Let's have a look at variables, and how to use them in your Perl script.

-Scaler variables

Scaler variables are variables that can hold only one value at a time (unlike an array). A scaler variable is defined in Perl by using "$", then the name of the variable itself. For example:

$myname="Ada";

Along with the "print" command, I can now use Perl to print out my name:

print "My name is $myname\n";

Scaler variables can obviously contain numbers as well:

$myage=17;

Moreover, scaler variables can contain the value of other scaler variables as well:

$mycar="Toyota";
$yourcar="Mercedes;
$mycar=$yourcar;

Thanks for the swap!

-Array variables

Perl also support array variables. An array holds lists of values, kind of like one big storage tank divided into many compartments. Arrays are defined by using the character "@" in front of the variable name. For example:

@fruits=("oranges", "bananas", "apples");

The variable @fruits holds three distinct values. So how do you access and retrieve each of these values? Watch:

print $fruits[0];          #PRINTS "oranges"
print $fruits[1];         #PRINTS "bananas"
print $fruits[2];         #PRINTS "apples"

As you can see, it's pretty simple. The important thing to realize here is that the first value inside the array is referenced by the index number "0", NOT "1". Also, note that "$", NOT "@", proceeds each individual array element. This makes sense, since each array element only contains ONE value.

You can assign a new variable to an array by using a subscript that's 1 index position higher than the last variable inside the array. Let's say I decide, after declaring the above array, that I want to add a new fruit, "pears" to it. Just do the following:

$fruit[3]="pears";

The array automatically expands to accommodate this new fruit.

-Putting it together

Let's incorporate what we've learned thus far and create a simple CGI script that uses variables in it:

#!/usr/bin/perl

$myname="Ada";
$myage=17;
@myhobbies=("computers", "basketball". "reading");
print "Content-type: text/html\n\n";
print "<HTML><HEAD><TITLE>This is a test</TITLE></HEAD><BODY>";
print "Hi. My name is $myname. I\'m $myage years old, and my hobbies
include @myhobbies[0]. @myhobbies[1], and @myhobbies[2].";
print "</BODY></HTML>";

-Decision Trees in Perl

Like most other programming languages, you can use decision trees such as if-else statements in your program to allow it to make crude decisions. Let me show you how to use some of the more common decision trees.

-If-else statement

Syntax:

if (condition){
#do this
}
else
#dothat

Example:

$thisyear=1999

if ($thisyear>=2000){
print " Welcome to the new millennium!";
}
else
print "Be patient. The millennium is coming soon";

-While loop

Syntax:

while (condition){
#do this
}

Example:

$x=5
while ($x<5){
print "$x ";
$x++
}

-For loop

Syntax:

for (begin; end; increment){
#do this
}

Example:

@animals=("horse", "sheep", "dog", "cat", "monkey", "chicken");
for ($i=0; $i<=5; $i++){
print "$animals[$i] ";
}

Opening and reading files in Perl

A very cool thing about Perl is it's ability to open up any file and read data from it. Here's the basic syntax:

open (FHANDLE, "mydata.dat");

FHANDLE is a temporary variable that's used to represent "mydata.dat" You can name this variable anything you like. mydata.dat represents the file name of the data file itself.

The following example prints out the contents of "ada.dat":

open (FHANDLE, "ada.dat");
while (FHANDLE){
print;
}

The line

while (FHANDLE)

basically means "while NOT END OF FILE."

The above is just a very basic example of how Perl can open and read external files. More complex examples are: searching the file for a specific match, writing to it, search-and-replace operations, and more.

Well, that's a wrap for now. As I learn more about Perl, I'll bring them to you through this site. Until then mi amigo!