Lecture 7: Introduction to CGI and Perl - PowerPoint PPT Presentation

1 / 53
About This Presentation
Title:

Lecture 7: Introduction to CGI and Perl

Description:

fruits = qw( apple orange banana ); # qw means quote word ... returns list in alphabetical order. Assigns current key to $key and performs indicated actions ... – PowerPoint PPT presentation

Number of Views:1286
Avg rating:3.0/5.0
Slides: 54
Provided by: felixha
Category:

less

Transcript and Presenter's Notes

Title: Lecture 7: Introduction to CGI and Perl


1
Lecture 7 Introduction to CGI and Perl
  • Web Architecture Web server
  • CGI Common Gateway Interface
  • examples of server-side programming
  • Perl
  • Data types
  • String processing
  • Client-server environment variables
  • Form Processing Business Logic
  • SSI

2
1. Web Architecture
  • Three-tier architecture
  • Presentation clients contains both the
    presentation and application logic components.
  • Content web server provides interactive view of
    information from a data store.
  • Data and service level provides data for the web
    server.

3
1. Web Architecture
  • Multi-tier architecture
  • Application-level or middleware has an
    application server, which is used to find
    requested data and services, makes them available
    for viewing, and carries out transactions.
  • Data and service level has a variety of data and
    services accessible by the application server.

4
1. Web Server
  • A web server is a network server that manages
    access to files, folders and other resources over
    Internet or local Intranet via HTTP
  • Handle permissions
  • Execute programs
  • Keep track of directories and files
  • Communicate with client computers
  • A web server consists of a computer with
  • a suitable operating system
  • a suitable file system
  • a network connection
  • web server software which accepts requests from
    browsers such as IE and Netscape

5
1. Examples of Web Server Software
  • Microsoft Personal Web Server
  • Microsoft Internet Information Server
  • W3C Jigsaw Web Server
  • http//jigsaw.w3.org/Distrib/
  • Apache
  • http//Apache-Server.Com/tutorials/

6
2. CGI
  • Server-side technologies such as CGI(Common
    Gateway Interface) , SSI(Server Side Include) and
    ASPs (Active Server pages)
  • CGI.
  • is a standard for interfacing external
    applications with web servers.
  • extends Web server's functionality through
    outside scripts or compiled programs.
  • Data path of a typical CGI-based application

7
2. CGI
  • Web browser
  • Take info from user
  • typically http//www.mydomain.com/cgi-bin/ name
  • Using HTTP, sends info to a Web server
  • Server-side CGI program executed
  • Standard output from server-side applications or
    scripts redirected or piped to CGI
  • Output sent from CGI over the Internet to the
    client browser for rendering
  • CGI is an interface
  • Cannot be directly programmed
  • Script or executable program must be used to
    interact with it

8
2. CGI
  • CGI programs can be written in any language, e.g.
    C/C, Fortran, Perl, TCL, any Unix shell, Visual
    Basic
  • CGI programs need to reside in a special
    directory, so that the Web server knows to
    execute the program rather than just display it
    to the browser.
  • typically resides in /cgi-bin directory.
  • Within Web server
  • Permission needs to be granted by Web master to
    allow specific programs to be executed on the
    server

9
3. Perl
  • Perl Practical Extraction and Report Language
  • High-level programming language
  • Developed by Larry Wall in 1987
  • Rich, easy-to-use text-processing capabilities
  • Alternative to the terse and strict C programming
    language
  • Powerful alternative to UNIX shell scripts
  • To check current version type perl v (in
    UNIX)
  • Logical choice for programming the server side of
    interactive Web-based applications
  • Most popular language for doing so today
  • Is continuously corrected and evolved by the
    online Perl community
  • Stays competitive with newer server-side
    technologies

10
3. Perl
  • Perl initially developed for UNIX platform
  • Always intended to be a cross-platform computer
    language
  • ActivePerl
  • Version of Perl for Windows
  • Free download at http//www.activestate.com
  • Includes the core Perl package
  • Predefined functionality expected to behave the
    same across all platforms
  • Perl Interpreter - perl.exe placed in bin
    directory
  • Loaded into memory each time Perl program invoked
  • Extension of Perl programs is .pl
  • Associated with Perl interpreter by default
  • Perl program execution
  • Type perl followed by filename of Perl source
    code at command line (DOS prompt)

11
3. Perl
  • Comment Character -
  • Goes at beginning of every line with comment
  • Function print
  • Outputs text indicated by quotation marks ()
  • Escape sequence \n
  • When inside a string, moves cursor to next line
  • Statements terminated with semicolons ()
  • Exception where braces () used to denote block
    of code

Welcome to Perl!
12
3.1. Perl Data Types
  • Perl contains set of data types
  • Represent different kinds of information
  • Each variable name has special character
    preceding it
  • - variable contains scalar value
  • Strings, integer numbers and floating-point
    numbers
  • _at_ - indexed array
  • Uses an integer (called an index) to reference
    array elements
  • An ordered list of scalar variables that can be
    accessed using integer indices
  • - hash
  • Uses keys that are strings to reference
    individual array elements
  • An unordered set of scalar variables whose values
    are accessed using unique scalar values (ie,
    strings) called keys.
  • Variables do not have to be initialized before
    being used
  • Variable names in strings
  • Serve as place-holders for values they represent
  • If have no declared value set to null (empty)
    value

13
3.1. Perl Data Types
  • Example to demonstrate scalar variables

Program to illustrate the use of scalar
variables. number 5 print( "The value of
variable \number is number\n\n" ) number
5 print( "Variable \number after adding 5 is
number\n" ) number 2 print( "Variable
\number after multiplying by 2 is " ) print(
"number\n\n\n" )
The value of variable number is 5 Variable
number after adding 5 is 10 Variable number
after multiplying by 2 is 20
14
3.1. Perl Data Types
  • Example to demonstrate uninitialized variables

using an uninitialized variable in the context
of a string print( "Using a variable before
initializing variable\n\n" ) using an
uninitialized variable in a numeric context test
undefined 5 print( "Adding uninitialized
variable \undefined " ) print( "to 5 yields
test\n" )
Using a variable before initializing Adding
uninitialized variable undefined to 5 yields 5
  • uninitialized variables have the value undef
  • Undef is evaluated to different values depending
    on variables context
  • Numeric context, undef evaluates to 0
  • String context, undef evaluates to

15
3.1. Perl Data Types
  • Example adding strings and numeric variables

using strings in numeric context string "A
string value" number 1 number
string print("Adding a string to an integer
\n") print(" \number \number \string
number \n") string2 "15charactersand1" num
ber2 number string2 print("Adding a string
with numbers in the beginning of the string\n
") print("\number2 \number \string2
number2\n\n") print("evaluating a string in
numeric context does not") print(" the value of
the string, \n \"string2\" yields
") print("string2\n")
16
3.1. Perl Data Types
  • Adding a string to an integer
  • number number string 1
  • Adding a string with numbers in the beginning of
    the string
  • number2 number string2 16
  • evaluating a string in numeric context does not
    the value of the string,
  • "15charactersand1" yields 15charactersand1

17
3.1. Perl Data Types
  • Perl can store arrays
  • Array definition
  • _at_arrayName (element1, element2, ,
    elementN)
  • Elements are referenced as scalar values with
    element number in square brackets ()
  • _at_ refers to array as a whole, refers to
    elements
  • First array element is 0
  • e.g. array2refers to the third element in
    _at_array
  • Range Operator ..
  • Used to store all values between given arguments
  • e.g. _at_array2 (A..Z)
  • Creates array _at_array2 containing all capital
    letters in alphabet (all letters between A and Z)

18
3.1. Perl Data Types
  • Example to demonstrate arrays

Program to demonstrate arrays in Perl _at_array
("Bill", "Bobby", "Sue", "Michelle") print "The
array contains\n\n" print "_at_array \n\n" print
"Third element array2\n\n" _at_array2
(A..Z) print "The range operator is used to
store all\n" print "letters from capital A to
Z\n\n" print "_at_array2 \n"
The array containsBill Bobby Sue
MichelleThird element SueThe range operator
is used to store allletters from capital A to
ZA B C D E F G H I J K L M N O P Q R S T U V W
X Y Z
19
3.1. Perl Data Types
  • array3 3 "4th"
  • print( "Array with just one element initialized
    _at_array3 \n\n" )
  • print( 'Printing literal using single quotes '
    )
  • print( '_at_array and \n', "\n" )
  • print( "Printing literal using backslashes " )
  • print( "\_at_array and \\n\n" )
  • Array with just one element initialized 4th
  • Printing literal using single quotes _at_array and
    \n
  • Printing literal using backslashes _at_array and \n
  • uninitialized elements of an array take the
    value undef
  • Eg, array30 array31 array32
    (empty string)
  • 3 spaces printed before printing 4th
  • Perl interpreter interprets strings inside
    single quotes literally, eg _at_array
  • use back slash to escape special characters, eg
    \_at_

20
3.2. Perl String Processing
  • Processing textual data easily and efficiently
  • One of Perls most powerful capabilities
  • Usually done through use of regular expressions
  • Patterns of characters used to search through
    text files and databases
  • Allows large amounts of text to be searched using
    relatively simple expressions
  • eq equality operator
  • Tests whether two strings are equivalent
  • eg. if (hello eq Good Morning)
  • Keyword my
  • Indicates designated variable only valid for
    block of code in which it is declared

21
3.2. Perl String Processing
  • Example to demonstrate string processing

my stringa "Test" my stringb
"Testing" if (stringa eq "Test") print
"stringa matches Test.\n" else print
"stringa does not match Test.\n" if
(stringb eq "Test") print "stringb matches
Test.\n" else print "stringb does not
match Test.\n"
Test matches Test.Testing does not match Test.
22
3.2 Perl String Processing
Program to demonstrate the eq, ne, lt, gt
operators. _at_fruits qw( apple orange banana )
qw means quote word foreach item ( _at_fruits )
if ( item eq "banana" ) print(
"String 'item' matches string 'banana'\n" )
if ( item ne "banana" ) print( "String
'item' does not match string 'banana'\n" )
if ( item lt "banana" ) print( "String
'item' is less than string 'banana'\n" ) if
( item gt "banana" ) print( "String
'item' is greater than string 'banana'\n" )
  • String 'apple' does not match string 'banana'
  • String 'apple' is less than string 'banana'
  • String 'orange' does not match string 'banana'
  • String 'orange' is greater than string 'banana'
  • String 'banana' matches string 'banana'

23
3.2 Perl String Processing
  • Searches using the matching operator and
    regular expressions.search "Now is is the
    time"print( "Test string is 'search'\n\n"
    )if ( search /Now/ ) print( "String
    'Now' was found.\n" )if ( search /Now/ )
    print( "String 'Now' was found at the
    beginning of the line." ) print( "\n" )if
    ( search /Now/ ) print( "String 'Now'
    was found at the end of the line.\n" )if (
    search /\b ( \w ow ) \b/x ) print(
    "Word found ending in 'ow' 1 \n" )if (
    search /\b ( \w ) \s ( \1 ) \b/x )
    print( "Repeated words found 1 2\n"
    )_at_matches ( search / \b ( t \w ) \b
    /gx )print( "Words beginning with 't' found
    _at_matches\n" )

24
3.2 Perl String Processing
  • Test string is 'Now is is the time'
  • String 'Now' was found.
  • String 'Now' was found at the beginning of the
    line.
  • Word found ending in 'ow' Now
  • Repeated words found is is
  • Words beginning with 't' found the time
  • \w ow - indicates searching for pattern ending
    with ow
  • \b \b - word boudary
  • match result(s) stored in special Perl
    variables, eg, 1, 2, 3
  • x indicates that whitespace in the regular
    expression are to be ignored
  • eg, If the expression search /\b ( \w ow
    ) \b/
  • then the script will search for
  • a word boundary,
  • 2 spaces,
  • one or more alphanumeric characters,
  • one space, the characters ow,
  • 2 spaces
  • word boundary.

25
3.3. Perl Packages
  • In addition to core Perl package
  • Add-ons called packages provide additional
    functionality
  • The packages can be including using use command
  • Packages
  • Often provide platform specific features
  • Are available free of charge at
    http//www.activestate.com/packages

26
3.3. Perl Samples Viewing Client/Server
Environment Variables
  • Knowing info about client very useful to system
    administrators
  • CGI environment variables
  • Contains info about client
  • Version of CGI server running
  • HTTP host, HTTP connection
  • Much more
  • Note In the following Perl example for
    Client/server environment variables, the purpose
    is to demonstrate how one can access the
    environment variables. The intention is not to
    study each and every of the environment
    variables.
  • Use statement
  • Allows inclusion of predefined library packages
    in programs

27
3.3 Perl Samples Viewing Client/Server
Environment Variables
  • CGI Library
  • Included to provide functionality that makes it
    easier to write HTML sent to Web browser
  • Contains keywords that represent HTML tags
  • foreach loop
  • Iterates through keys in given hashtable,
    performs indicated actions
  • foreach key (sort keys ENV)
  • Iterates through ENV hashtable
  • Built-in table in Perl that contains names and
    values of all CGI environment variables
  • sort function
  • returns list in alphabetical order
  • Assigns current key to key and performs
    indicated actions

28
3.3 Perl Samples Viewing Client/Server
Environment Variables
  • Line 2
  • use instruct Perl to include the contents of
    predefined functions
  • standard import a standard set of pre-defined
    functions
  • Line 4-7
  • Change the default document type definition from
    HTMLs DTD to the value in dtd
  • Line 9
  • Instruct Perl to print a valid HTTP header using
    the header() function

1 !/usr/local/bin/perl 2 use CGI qw
(standard) 3 4 dtd 5 "-//W3C//DTD XHTML
1.0 Transitional//EN\" 6 \"http//www.w3.org/T
R/xhtml1/DTD/xhtml1- 7 transitional.dtd" 8 9
print( header() )
29
3.3 Perl Samples Viewing Client/Server
Environment Variables
  • Line 10
  • start_html() is a CGI function that prints the
    document type definition, and the opening tags
    such as , ,
  • Line 16
  • Tr() and th() are CGI functions that place
    arguments between Table row and table header tags
    (ref, p 923, Deitel)
  • Line 19
  • td() is a CGI function that prints tags
  • hr() is a CGI function that prints a horizontal
    line and a line break above and below the
    horizontal line

10 print( start_html( dtd dtd, 11
title "Environment Variables..." ) ) 12 13
print( "2 14 font-weight bold\"" ) 15 16
print( Tr( th( "Variable Name" ), 17
th( "Value" ) ) ) 18 19 print( Tr( td( hr() ),
td( hr() ) ) )
30
3.3 Perl Samples Viewing Client/Server
Environment Variables
  • Line 20
  • ENV hash is a built-in table in Perl that
    contains the names and values of all the
    environment variables
  • each element in a hash is accessed using a unique
    string key that is associated with that elements
    value.
  • keys() is a function that returns an unordered
    array containing all keys in ENV
  • foreach iterates sequentially through the array
    returns by sort
  • Line 24
  • hash values are accessed using the syntax
    hashNamekeyName, eg, ENV variable

20 foreach variable ( sort( keys( ENV ) ) )
21 22 print( Tr( td( style
"background-color 11bbff" , 23
variable ), 24 td( style
"font-size 12pt" , 25
ENV variable ) ) ) 26 27 print( Tr( td(
hr() ), td( hr() ) ) ) 28 29 30 print(
"" ) 31 print( end_html() )
31
3.3 Perl Samples Viewing Client/Server
Environment Variables
  • Script output

32
3.4 Perl Samples Form Processing and Business
Logic
  • Two parts
  • client side HTML forms
  • server side CGI programs, e.g. Perl script
  • HTML FORMs
  • 1. Allow users to enter data
  • 2. Data sent to Web server for processing
  • 3. Program processes data
  • Allows users to interact with server
  • Vital to electronic commerce
  • FORM element
  • Indicates what action should occur when user
    submits form
  • Attribute ACTION cgi-bin/form.pl
  • Directs server to execute form.pl Perl script

33
  • Line 17
  • Open FORM
  • Define FORM attributes
  • Specify the action
  • IMG SRC specify the path to display the image
  • Line 23-30
  • Define form INPUT elements
  • TYPE text inserts a one-line text box
  • NAME provides a unique identification for INPUT
    element
  • Line 33
  • Specify correct input format

34
  • Line 55-64
  • Radio buttons
  • similar in function and usage to checkboxes
  • Only one radio button in a group can be selected

35
3.4 Perl Samples Form Processing and Business
Logic
  • Script Output

36
3.4 Perl Samples Form Processing and Business
Logic
  • CGI Scripts written in Perl
  • Retrieving data from form output
  • Assign to variables
  • Example Assign data from form INPUT OS to
    variable OS
  • os param(OS)
  • Testing for correct form input
  • Example Make sure phone number in format
    (555)555-5555
  • if ( phone / \( \d3 \) \d3 - \d4 /x)
    actions
  • dn tests for n digits
  • \ is escape character
  • Close-bracket ()) character is used in Perl
    statements, needs escape character \ to appear
    as part of search test string

37
  • Line 2
  • Use (include) standard CGI library
  • Line 3-8
  • Assign form field values to variables
  • param() is part of the Perl CGI module that
    retrieves values from a form fields value

1 !/usr/local/bin/perl 2 use CGI qw
(standard) 3 os param( "os" ) 4
firstName param( "fname" ) 5 lastName
param( "lname" ) 6 email param( "email" ) 7
phone param( "phone" ) 8 book param(
"book" ) 9 dtd 10 "-//W3C//DTD XHTML 1.0
Transitional//EN\" 11 \"http//www.w3.org/TR/xht
ml1/DTD/xhtml1-transitional.dtd" 12 print(
header() ) 13 print( start_html( dtd
dtd, 14 title "Form
Results" ) )
38
  • Line 15
  • Test for correct phone number input form using
    if structure
  • Indicate actions to be performed if test returns
    TRUE result
  • check if phone no. has the format (555)-555-5555
  • \d3 means 3 digits
  • ensure that there are no extra characters
    in front or at the end
  • Line 22
  • br() function adds a break (ie ,
    )
  • Line 24
  • span() function adds a tag

15 if ( phone / \( \d3 \) \d3 - \d4
/x ) 16 print( "Hi " ) 17 print( span(
style "color blue font-weight bold" , 18
firstName ) ) 19 print( "!"
) 20 21 print( "\nThank you for completing the
survey." ) 22 print( br(), "You have been added
to the " ) 23 24 print( span( style
"color blue font-weight bold" , 25
book ) ) 26 print( " mailing list.",
br(), br() ) 27 28 print( span( style
"font-weight bold" , 29 "The
following information has 30
been saved in our database " ), br() )
39
31 print( table( 32 Tr( th( style
"background-color ee82ee" , 33
"Name" ), 34 th( style
"background-color 9370db" , 35
"E-mail" ), 36 th( style
"background-color 4169e1" , 37
"Phone" ), 38 th( style
"background-color 40e0d0" , 39
"OS" ) ), 40 41 Tr( style
"background-color c0c0c0" , 42
td( "firstName lastName" ), 43 td(
email ), 44 td( phone ), 45
td( os ) ) ) )
  • Line 32
  • Tr() and th() are CGI functions that place
    arguments between Table row and table header tags
  • (ref, p 923, Deitel)
  • Line 42
  • td() is a CGI function that prints tags

40
46 print( br() ) 47 print( div( style
"font-size x-small" , 48 "This is
only a sample form. You have not been 49
added to a mailing list." ) ) 50 51 else
52 print( div( style "color red
font-size x-large" , 53 "INVALID
PHONE NUMBER" ), br() ) 54 print( "A valid
phone number must be in the form " ) 55 print(
span( style "font-weight bold" , 56
"(555)555-5555." ) ) 57 print( div(
style "color blue" , 58 "Click
the Back button, and enter a 59
valid phone number and resubmit." ) ) 60
print( br(), br() ) 61 print( "Thank you."
) 62 print( end_html() )
  • Line 47
  • div() is a CGI function that generates a
    tag
  • Line 51
  • Set actions to be performed if the if structure
    returns a FALSE value
  • Line 51
  • end_html() returns the closing tags for the page
    ( and )

41
3.4 Perl Samples Form Processing and Business
Logic
  • Script Output 1 If phone number is valid

42
3.4 Perl Samples Form Processing and Business
Logic
  • Script Output 2 If phone number is invalid

43
4. SSI
  • SSI Server-Side Include
  • Commands embedded in HTML documents
  • Provide for content creation
  • Allow inclusion of current time, date or even
    contents of different html document
  • Cf. Original CGI is executed by client commands.
  • http//www.mydomain.com/cgi-bin/
  • With SSI, the commands to execute CGI programs
    are embedded in HTML scripts.

44
4. SSI
  • SSI commands
  • Execute CGI scripts on a server
  • Are capable of connecting to an ODBC data source
  • Use to create customized Web pages depending for
    certain conditions
  • Document containing SSI commands has .shtml file
    extension
  • EXEC CGI command
  • Issued to execute a Perl script before document
    sent to client
  • Example
  • Executes the Perl script counter.pl, located in
    the cgi-bin directory

45
4. SSI
  • ECHO command
  • Used to display variable information
  • Is followed by the keyword VAR and variables
    constant name
  • Example
  • Returns the current local time
  • Other variables
  • DATE_GMT
  • Contains current Greenwich Mean Time
  • DOCUMENT_NAME
  • Contains name of current document
  • Many more

46
4. SSI Samples
  • Common usage
  • For tracking clients
  • Where client coming from
  • What client views on your site
  • Where client goes after your site
  • Tracking Web data important, allows Web masters
    to
  • Know which sites visited most frequently
  • Know how effective advertisements and products are

47
  • Line 14
  • Execute Perl script counter.pl using EXEC CGI
    statement
  • Line 18
  • Use ECHO VAR statements to display environmental
    variables

48
  • Continue printing environmental variables using
    ECHO VAR statements

49
4. SSI Samples
  • The Perl Script counter.pl
  • Line 5-8
  • Open counter.dat, assign to filehandle COUNTREAD
  • read 1 line from file referred by filehandler
    COUNTEREAD
  • Increment data in COUNTREAD
  • Close COUNTREAD
  • Line 9-11
  • means write mode, open counter.dat for
    writing, referred to by filehandler COUNTERWRITE
  • print indicates the filehandler COUNTERWRITE
    where data is written

1 !/usr/local/bin/perl 2 Program to track the
number of times a Web page has been accessed. 3
counter.cgi 4 use CGI qw( standard ) 5 open(
COUNTREAD, "counter.dat" ) 6 data
7 data 8 close( COUNTREAD ) 9
open( COUNTWRITE, "counter.dat" ) 10 print(
COUNTWRITE data ) 11 close( COUNTWRITE ) 12
print( header(), "center font-weight bold\"" ) 13 print( "You
are visitor number", br() ) 14 for ( count
0 count number substr( data, count, 1 ) 17 print(
img( src "number.gif" ), "\n" ) 18 19
print( "" )
50
4. SSI Samples
  • Perl script uses built-in functions
  • open() and close() function
  • to open and close a file
  • print() function
  • to redirect output to a file
  • substr( x, y, z ) function
  • Similar to JavaScripts substr function
  • First argument (x)
  • Specifies string from which to take a substring
  • Second argument (y)
  • Specifies offset in characters from beginning of
    the string
  • Third argument (z)
  • Specifies length of substring to return

51
4. SSI Samples
  • Script output

52
Further Readings
  • Note This topic is designed with the objective
    of providing an introduction to CGI, Perl and
    SSI.
  • Advanced features of CGI, Perl and SSI are beyond
    the scope of this course and will NOT be taught
    or discussed. Students who wish to invest more
    time on studying advanced features and topics of
    CGI, Perl and SSI are referred to the following
    resources
  • Deitel Chapter 27
  • http//hoohoo.ncsa.uiuc.edu/cgi/overview.html -
    CGI info
  • http//hoohoo.ncsa.uiuc.edu/docs/tutorials/include
    s.html - SSI info

53
Further Readings
  • Students who wish to invest more time on studying
    Web servers are referred to the following
    resources
  • Deitel Chapter 24
  • Apache
  • http//Apache-Server.Com/tutorials/
Write a Comment
User Comments (0)
About PowerShow.com