12.5 The Common Gateway Interface (CGI)


A Web server can serve static or dynamic content. Static content, or static pages, usually consist of data associated with a URL that does not change very often and does not depend on any client input. This content is usually in the form of some markup language, such as Hypertext Markup Language (HTML) or Extensible Markup Language (XML) (Parziale, et al. 2006).

Client-side dynamic content

The Client-side dynamic content is usually in the form of applets and script. When a Java program is started from inside an HTML (Web) page, it is called a Java applet. Applets are downloaded by the Web browser from a server and, by definition, are somewhat limited in the way they can use resources of the local system. JavaScript is an HTML extension and programming language, developed by Netscape, which is a simple object-based language compatible with Java. JavaScript programs are embedded as a source directly in an HTML document. They can control the behavior of forms, buttons, and text elements. It is used to create dynamic behavior in elements of the Web page (Parziale, et al. 2006).

Server-side dynamic content

The complement of client-side dynamic content generation is server-side content generation. The Common Gateway Interface (CGI) is a means of allowing a Web server to execute a program that is provided by the Web server administrator, rather than retrieving a file.

12.5 The Common Gateway Interface (CGI)


Server-side dynamic content is usually in the form of CGI, Server-specific APIs, Servlets, Server-side includes (SSI), Java Server Pages (JSP), Objects, and /or JavaBeans.

CGI programs allow a Web server to generate a dynamic response, usually based on the client's input. A number of popular Web servers support the CGI, and a variety of programming languages can be used to develop programs that interface with CGI. However, CGI programs are not easily portable across platforms, unless using Perl (Parziale, et al. 2006).

Perl

Perl is a general-purpose programming language originally developed for text manipulation and now used for a wide range of tasks including system administration, web development, network programming, GUI development, and more (Robert ,version 16.0).

Its major features are (Robert ,version 16.0):
it's easy to use,
supports both procedural and object-oriented programming,
has powerful built-in support for text processing, and
Has one of the world's most impressive collections of third-party modules.

12.5 The Common Gateway Interface (CGI)


Perl basic syntax

To run a Perl program from the UNIX command line, you may use: Perl progname.pl

In order to make Perl more robust it is recommended to start every program with the following lines:

#!/usr/bin/perl
use strict;
use warnings;

The two additional lines request from Perl to catch various common problems in your code. They check different things so you need both (Robert ,version 16.0).

A Perl script or program consists of one or more statements. These statements are simply written in the script in a straightforward fashion (Robert ,version 16.0).

Perl statements end in a semi-colon: print "Hello, world";

Comments start with a hash symbol and run to the end of the line: # This is a comment Whitespace is irrelevant:


12.5 The Common Gateway Interface (CGI)


print
   "Hello, world"
   ;
except inside quoted strings:
   # this would print with a linebreak in the middle
print "Hello
   world";

Perl has three main variable types: scalars, arrays, and hashes. Scalar values can be strings, integers or floating point numbers, and Perl will automatically convert between them as required. There is no need to pre-declare your variable types, but you have to declare them using the my keyword the first time you use them. A scalar represents a single value:

my $animal = "camel"; or my $answer = 42;
   An array represents a list of values, and is zero-indexed:
   my @animals = ("camel", "llama", "owl");
   print $animals[0]; # prints "camel"
A hash represents a set of key/value pairs:
   my %fruit_color = ("apple", "red", "banana", "yellow");
To get at hash elements:
   $fruit_color{"apple"}; # gives "red"

12.5 The Common Gateway Interface (CGI)


Hashes have no particular internal order, though you can sort the keys and loop through them.

More complex data types can be constructed using references, which allow you to build lists and hashes within lists and hashes. A reference is a scalar value and can refer to any other Perl data type. So by storing a reference as the value of an array or hash element, you can easily create lists and hashes within lists and hashes (Robert ,version 16.0).