1st Perl CGI Presentation - School of Engineering

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

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

100 εμφανίσεις

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.)


or



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
Server
-
side Database to save time

And improve security

CGI


Common Gateway Interface


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


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

PERL


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
Script


Other methods (PHP) exist, but
Perl

is on
our Webserver

Web Resources


www.perl.com


www.perl.org


www.cookwood.com/perl


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 “
table
” 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 “
zipped
file for Windows
” in the right
-
hand frame.
These are the actual scripts.

Caution


The scripts were set up for running on a
UNIX
-
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
hash


A string constant should be enclosed in
quotes


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
sh
arp, and ! Is
a
bang
)


The shebang line is always the same:




#!/usr/local/bin/perl<enter>


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,
each

comment
line being preceded by <#>

Example Script (p. 58 in Castro):


To create output in a browser


#!/usr/local/bin/perl


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


print “Content
-
type: text/html
\
n
\
n”;


print “Stuff”;


Store your script, giving it somename.cgi


Use FTP to upload script to the server,
or

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
stored


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
executed

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
prompt:




perl somename.cgi



The material you wanted to print should
appear


Before exiting Telnet, be sure to change
permissions

The Ultimate Test


Go to your homepage using Netscape


Enter a URL that points to your CGI file:


www.seas.smu.edu/~youracct/somename.cgi



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