1st Perl CGI Presentation - School of Engineering

greenbeansneedlesΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 4 χρόνια και 6 μήνες)

125 εμφανίσεις

SMU School of Engineering

EE 2305

Introduction to
Perl CGI Script

Outline for this Session

Why do we need PERL/CGI?

What is CGI?

What is PERL?

Intro to Perl CGI Script

So What Happens when
you “Process”

Either processing is done in the PC (client),
using JavaScript within the HTML document.
(This is client
side processing.)


The form data is sent to the webserver for
processing by a CGI (Common Gateway
Interface) program and the results sent back
to the client. (This is server
side processing.)

CGI Program can interact with
side Database to save time

And improve security


Common Gateway Interface

CGI is a
for communiction between
the Client (user’s computer) and the Server

A protocol is a set of rules governing a time
sequence of events


Practical Extraction and Report Language

PERL is a programming language, part of
which is commonly used to write the scripts
for CGI communications between the Client
and Server.

It is not necessary to learn all of PERL in
order to write these scripts

The finished product is called a Perl CGI

Other methods (PHP) exist, but

is on
our Webserver

Web Resources




The latter URL is provided by Elizabeth
Castro, author of your Lab Text. It contains
numerous (

200) examples of Perl CGI
Scripts that are keyed to the Lab Text and
can be downloaded, customized and
inserted into your program.

Exploring www.cookwood.com

Go to http://www.cookwood.com

Click on “Perl and CGI”

Click on “Examples” in the left
hand frame

Click on “
” in the right
hand frame. You
will see a list of all the Perl CGI Scripts that
have been created for you, keyed to the
various chapters and pages of the book.

Go back to “Examples” and click on “
file for Windows
” in the right
hand frame.
These are the actual scripts.


The scripts were set up for running on a
based webserver.

When modifying these scripts , use the
UNIX text editor, “pico”. Do NOT use
notepad. Notepad inserts a non
UNIX code
when you hit <enter>, and the Note: the little
black rectangles in the scripts are <enter>,
and the PERL interpreter doesn’t
understand it.

PERL Building Blocks

Perl Data can be a number or string, a
constant or variable, and a scalar, array or

A string constant should be enclosed in

A variable is a memory location or
“placeholder” into which data can be stored
and retrieved.

A scalar is a single piece of data.

More Building Blocks

An array is a collection of scalars

A hash is a collection of scalars in pairs, like
a table with only two columns

A variable name always begins with

$ for a scalar

@ for an array

% for a hash

Starting a PERL Script

All Perl CGI Scripts start with the “shebang”
line (so
called because # is a
arp, and ! Is

The shebang line is always the same:


Each statement ends with a semicolon <;>
(The shebang line is not a Perl statement,
so it does not get a semicolon.)

Comments can be inserted,

line being preceded by <#>

Example Script (p. 58 in Castro):

To create output in a browser


#The following script causes “Stuff” to
#appear on the screen.

print “Content
type: text/html

print “Stuff”;

Store your script, giving it somename.cgi

Use FTP to upload script to the server,

use pico to enter the script directly into your
cyberspace, but do NOT use Notepad

Checking Syntax

Telnet to the directory where your script is

Type perl
c somename.cgi

The PERL interpreter will advise you if there
is a syntax problem in the script. The
interpreter will tell you if the script’s syntax is
OK or if there is a problem

This action does not cause the script to be

Testing your script

While you are in the directory in which your
script is stored, you can run it by typing the
following command from the % command

perl somename.cgi

The material you wanted to print should

Before exiting Telnet, be sure to change

The Ultimate Test

Go to your homepage using Netscape

Enter a URL that points to your CGI file:


You should see your message on the
browser screen

You have succeeded in causing your client
side browser to pick up and display info from
the server.

Next Session on Perl CGI

Transferring information from HTML forms
to the server, for server
side processing