CS 299 – Web Programming and Design Perl/CGI. CS 299 – Web Programming and Design 2 What We Have...

92
CS 299 – Web Programming and Design Perl/CGI

Transcript of CS 299 – Web Programming and Design Perl/CGI. CS 299 – Web Programming and Design 2 What We Have...

CS 299 – Web Programming and Design

Perl/CGI

2CS 299 – Web Programming and Design

What We Have Learned and Will Learn

• HTML/XHTML: contents• CSS: display, layout• JavaScript: client side programmability

– Ajax: based on JavaScript, XML, HTML, CSS

• Server side programmability– CGI: Command Gateway Interface– PHP: Open source, strong database support– ASP: Microsoft product– …

3CS 299 – Web Programming and Design

Command Gateway Interface (CGI)

• CGI provides a way by which a web server can obtain data from (or send data to) a database, and other programs, and present that data to viewers via the web

• A CGI program can be written in any programming language, but Perl is one of the most popular

4CS 299 – Web Programming and Design

Specifics About CGI

• A CGI program can be written in any language that allows to be executed on the system:– C/C++– Fortran– Perl– Python– VB– …

• CGI programs are executable– Basically equivalent of letting the world run a

program on your system– Security precautions: CGI programs need to reside in

a special directory

5CS 299 – Web Programming and Design

Perl is A Common Choice

• Simple, powerful string manipulation• Flexible pattern matching and substitution• Multi-platform deployment • Abstracted database access• Web server integration• Safe garbage collection • Simple integration with C/C++ for speed

6CS 299 – Web Programming and Design

Our First CGI Program – hello.cgi

#!/usr/local/bin/perl –w

print “Content-type: text/html\r\n\r\n”;print “<html>\n”;print “<head><title>Hello World!</title></head>”;print “<body style=\”text-align:center\”>\n”;print “<h1>Hello World!</h1>\n”;print “</body>”;print “</html>\n”;

exit(0);

http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/hello.cgi

http://www.csupomona.edu/~ftang/www/cgi-bin/hello.cgi

7CS 299 – Web Programming and Design

Anatomy of the Program

• First line “#!/usr/local/bin/perl –w” is known as the shebang, which informs the OS of the program that can understand this code (perl, in this case)

• The MIME type “Content-type: text/html\r\n\r\n” tells the client’s browser how to interpret the information that follows the declaration

• HTML document: the meat– Many possibilities

• Return Value– exit (0): normal exit– exit (1): something bad happened

8CS 299 – Web Programming and Design

What can Perl Do?

• What can Perl do?– print out HTML code – read a template and substitute portion of the template

document with some dynamic code– connect to a database and dynamically generate HTML

from query results– read in form variables and values from an HTTP request

and compute the next HTML page using some process– Have Perl initiate an HTTP request to some other web

server, mutate the results and pass them back to the client– …– Basically, if you can code it with Perl, then you can make it

web-enabled with not much more effort

9CS 299 – Web Programming and Design

What is Perl?

• Practical Extraction Report Language

• Type “which perl” after we login, this will show us the path of the perl command– In our system, it is under “/usr/local/bin/perl”

• You can write perl program and name it “filename.pl”

• To run this perl program from terminal– try “perl filename.pl”

10CS 299 – Web Programming and Design

Basic Perl

• Numeric and String Literals• Variables• Operators• Statements• Control Statements• Functions

11CS 299 – Web Programming and Design

Literal

• A literal is a value that is hard-coded in your source code

• Perl uses four types of literals:– Numbers: the most basic data type– Strings: a string is a series of characters that are

handled as one unit– Arrays: an array is a series of numbers and strings

handled as a unit; a list– Associative arrays: a list in which every value has an

associated lookup item

12CS 299 – Web Programming and Design

Numeric Literals and String Literals

• Numeric literals– integers such as 4, 043(octal format), 0x23(hex)– float such as .000034 or 3.4E-5 (scientific notation)

• String literals (unlike C/C++, no ending NULL character)– Single-quoted ‘hello’

• Can add a line break by adding line break to source code

– Double-quoted “hello”• Have special escape sequences \n, \%

– Back-quoted `ls –l`• To execute system commands

13CS 299 – Web Programming and Design

Literals Continue…

• Array literals– () an empty array

– (“This”, “is”, “an”, “array”, “of”, “strings”)

– Nesting arrays:• ((“Good”, “Morning”), (“Good”, “Afternoon”))

– Use a range of values• print (1..15) will print out numbers from 1 to 15• print (“A”, “B”, “F”..”H”, “Z”) will print out A, B, F, G, H, Z

• Will talk about associative array later

14CS 299 – Web Programming and Design

Variables (Three Types)

• In Perl, you never have to declare, define, or allocate these data types

• Scalar: holds one number or one string at a time– Always begin with a $– $quantity = 5; $price = 10.3; $name = “blah”

• Array: holds a list of values– Always begin with a @– @emptyArr = ();@numArr=(1..10); @alphabet =

(“A”..”Z”);– print @numArr; print $alphabet[25];– using negative subscripts will print each array element in a

reverse order– $numItems = @numArr; # assigns the number of elements

in numArr to $numItems

15CS 299 – Web Programming and Design

Variable Continue…

• Array continue:– How to grab a slice (a part) of an Array

• @array = (“1”, “2”, “3”, “4”);• ($first, $third) = @array[0, 2];• @half = @array[2, 3];

• Associative array: uses any value as an index into an array (like a hashtable)– Always begin with a %– %birthdays = (“Jack”, “Dec 2”, “Joe”, “June 2”,

“Jane”, “Feb 13”);– print $birthdays{“Jack”};– undefined key, return null or blank string

16CS 299 – Web Programming and Design

Some Array Functions

• push() and pop():– add or remove an item from the right hand side of an

array– push(@mystack, $newvalue);– $off_val = pop(@mystack);

• shift() and unshift()– like push() and pop(), but on the left hand side

• reverse()– reverse the ordering of list– @a = (1, 2, 3}; @b = reverse(@a);

17CS 299 – Web Programming and Design

Some Array Functions

• sort()– @x = sort (“small”, “medium”, “large”);– @y = sort(1, 32, 16, 4, 2); # gets (1, 16, 2, 32, 4)

sorting is done on the string values of each number (alphabetical)

• chop()– removes the last element from an array– chop(“small”, “medium”, “large”);

18CS 299 – Web Programming and Design

Associative Array Operators

• keys(%arrayname)– lists all the key names in a specified associative

array– @names = keys (%birthdays);

• values(%arrayname);– returns the values of the specified associative array– @dates = values(%birthdays);

• delete– deletes an associated key and value by key

reference– delete $birthdays(“Joe”);

19CS 299 – Web Programming and Design

Operators

• Binary arithmetic operators– +, -, *, /, %, **

• Unary arithmetic operators– +op1, -op1, ++op1, --op1, op1++, op1--

• Logical operators– && (and), || (or), ! (not)

• Bitwise operators– & (and), | (or), ^ (exclusive-or), ~ (complement), >> (shift

right), << (shift left)• Numeric relational operators

– ==, !=, <, <=, >, >=– op1 <=> op2

• returns 1: if op1 is greater than op2• returns 0: if op1 equals op2• return –1: if op1 is less than op2

20CS 299 – Web Programming and Design

Operators, Continue…

• String relational operators– op1 eq op2: returns true if equivalent– op1 ne op2: returns true if not equivalent– op1 lt op2: returns true if op1 is less than op2– op1 le op2: returns true is op1 is less than or equal to op2– op1 gt op2: returns true if op1 is greater than op2– op1 ge op2: returns true if op1 is greater than or equal ot

op2– op1 cmp op2

• returns 1 if op1 is greater than op2• returns 0 if op1 equals op2• returns –1 if op1 is less than op2

• Ternary operator– condition-part ? true-part : false-part

21CS 299 – Web Programming and Design

Operators, Continue

• Range operator ..– @array = (1..10); @array=(“aa”..“af”);– @array = (“ay”..“bf”);

• String operators– Concatenation .

• $first = “tom”; $second = “jerry”; • $name = $first . “ and ” . $second

– Repetition x• $str = $str x 2; # print out the string twice

• Assignment operators– var += op1;– …

22CS 299 – Web Programming and Design

Operators, Continue

• Conversion between numbers and Strings– Scalar variables are converted automatically– $x = “40”; $y = “50”;– $z = $x + $y; # answer 90– $s = $x.$y; # answer “4050”

• chop()– takes a single argument and removes the last

character from the string– chop(‘sandy’) would give ‘sand’– most string inputs in Perl end with a \n. chop() can

easily remove it for future processing

23CS 299 – Web Programming and Design

Operators, Continue

• Assigning an array to scalar variables– @array = (“Tom Jones”, “123 Harley LN”,

“Birmingham”, “AR”);– ($name, $street, $town, $state) = @array;

• For operators, the order of precedence is always important– parentheses are recommended to explicitly tell Perl

how and in which order to evaluate operators

24CS 299 – Web Programming and Design

So Far, We Have Learned …

• Literals• Variables• Operators

• Statement/Statement Block• Function• Reference

25CS 299 – Web Programming and Design

Statement and Statement Block

• Statements are a complete unit of instruction for the computer to process

• A statement block is a group of statements surrounded by curly braces

• You can use the my() function to create variables whose scope is limited to the statement block$firstvar = 10;{ my($firstvar) = “A”; print $firstvar x 5 . “\n”;}print (“firstvar = $firstvar\n”);

26CS 299 – Web Programming and Design

if/unless statement

if (expression){ true_statement_1; …. true_statement_n;}

if () {…} else {…}

if () {…} elsif () {…} else {} unless ($age < 18) {print “Go and Vote\n”;}

curly braces are required even if there’s only one statement

27CS 299 – Web Programming and Design

Loops

• For statement– for (initial_expr; test_expr; increment_expr) {…}

• while/until statement– while (expr) {…} # while the expr is true– until (expr) {…} # until the expr is false

• foreach statement– foreach $i (@some_list) {…}

@a = (1..5);foreach $i (reverse @a) { print $i;}

28CS 299 – Web Programming and Design

Jump Keywords

• Four keywords– last: jumps out of the current statement block– next: skips the rest of the statement block and

continues with the next iteration of the loop– redo: restarts the statement block– goto: jumps to a specified label

{print(“What’s your name? ”);$name = <STDIN>;chop($name); # also try chomp() hereif (!length($name)) {

print(“Msg: Zero length input. Please try again\n”);redo;

}print(“Thank you, ” . uc($name) . “\n”); # uc() uppercass

}

29CS 299 – Web Programming and Design

Function

• A function definition in Perl is as follows:sub functionName {}

• Using parameters– All parameters to a function are stored in an array

called @_.– Perl parameters are called by reference. Changing

their values in the function also changes their values in the main program.

30CS 299 – Web Programming and Design

A Function Example

$areaOfFirstCircle = areaOfCircle(5); print("$areaOfFirstCircle\n"); sub areaOfCircle {

$radius = $_[0]; return(3.1415 * ($radius ** 2));

}

31CS 299 – Web Programming and Design

Using the Parameter Array (@_)

• Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.firstSub(1, 2, 3, 4, 5, 6); firstSub(1..3); firstSub("A".."Z"); sub firstSub {

$numParameters = @_ ; print("The number of parameters is $numParameters\n");

}

areaOfRectangle(2, 3); areaOfRectangle(5, 6); sub areaOfRectangle {

($height, $width) = @_ ; $area = $height * $width; print("The height is $height. The width is $width. The area is $area.\n\n");

}

32CS 299 – Web Programming and Design

Passing Parameters by Reference

@array = (0..5); print("Before func call, array =

@array\n"); firstSub(@array); print("After func call, array =

@array\n");

sub firstSub { $_[0] = "A"; $_[1] = "B";

}

@array = (0..5); print("Before func call, array =

@array\n"); firstSub(@array); print("After func call, array = @array\

n");

sub firstSub{ ($firstVar, $secondVar) = @_ ;

$firstVar = "A"; $secondVar = "B";

}

• Perl parameters are called by reference, changing their values in the function also changes their values in the main program.

33CS 299 – Web Programming and Design

Scope of Variables

• Scope refers to the visibility of variables.• Normally, every variable has a global scope.• Two Perl’s built-in functions can create

variables of limited scope.– my() : visible to the current function only– local() : visible to the current function and any

functions that are called by the current function– using my() enforces good programming practices

and reduces headaches

34CS 299 – Web Programming and Design

Using a List as a Function Parameter

• You can pass many scalar values as you want to a function, but only on an array. If you try to pass more than one array, the array elements become joined together and passed as one array to the function.

• To pass both scalar and array, put scalar type first, and then array to distinguish them

• Nested functions

35CS 299 – Web Programming and Design

String Functions

• chomp (string or array): remove the trailing newline if it exists from a string or each element of an array

• chop(string or array): removes the last character from a string or from every element in an array. The last character chopped is returned

• index(string, substring, position): returns the position of the first occurrence of substring in string at or after position

• rindex(string, substring, position) : returns the position of the last occurrence of substring in string at or after position

• join(string, array): returns a string that consists of all the elements of array joined together by string

36CS 299 – Web Programming and Design

More String Functions

• split(pattern, string, limit)– breaks up a string using pattern as the delimiter.– The limit parameter indicates how many parts to create from

string• substr(string, offset, length)

– returns a portion of a string as determined by the offset and length parameters.

• lc(string), uc(string): lowercase; uppercase• lcfirst(string), ucfirst(string)• length(string)

• Note as a general rule, if Perl sees a number where it expects a string, the number is automatically converted to a string

• Note that some of the string functions use the special variable $_ as the default string to work with

37CS 299 – Web Programming and Design

Array Functions

• defined(variable)– returns true if the variable has a real value and false if the

variable has not yet been assigned a value• delete(key)

– removes the key-value pair from the given associative array, e.g., delete($arr{“orange”})

• exists(key)• each(assoc_arr)

– returns a list that contains key-value pairs from the associative array, so that it’s easy to iterate through

• keys(assoc_arr)– returns a list of keys

• values(assoc_arr)– returns a list of values

38CS 299 – Web Programming and Design

More Array Functions

• pop, push, reverse, sort• shift/unshift:

– shift(array): returns the first value of an array and reduces the size by one

– unshift(arr1, arr2): adds the elements of arr2 to the front of arr1

39CS 299 – Web Programming and Design

Reference

• A reference is a scalar value that points to a memory location that holds some type of data

• All of your variables and functions are located at some memory location

• References are used to hold the memory addresses

• When a reference is dereferenced, you can retrieve the information referred to by the reference

40CS 299 – Web Programming and Design

Common Reference Types

• $refScalar = \$scalar;• ${$refScalar} is a scalar value

• $refArray = \@array;• @{$refArray} is an array value

• $refHash = \%hash;• %{$refHash} is a hash value

• $refRef = \$refScalar;• ${${$refRef}} is a scalar value

• $refglob = \*file;

• $refFunction = \&function;• &{$refFunction} is a function location

41CS 299 – Web Programming and Design

Passing Parameters to Functions

@array1 = (1..5); @array2 = ("A".."E"); firstSub( \@array1, \@array2); sub firstSub {

my($ref_firstArray, $ref_secondArray) = @_; print("The first array is @{$ref_firstArray}.\n"); print("The second array is @{$ref_secondArray}.\n");

} This program displays:

The first array is 1 2 3 4 5.

The second array is A B C D E.

42CS 299 – Web Programming and Design

The ref() Function

• Using references to pass arrays into a function is easy• However, what if you pass a scalar reference to a

function instead of an array reference?

firstSub( 10, ("A".."E") ); sub firstSub {

my($ref_firstArray, $ref_secondArray) = @_ ; print("The first array is @{$ref_firstArray}.\n"); print("The second array is @{$ref_secondArray}.\n");

}

This program displays: Not an ARRAY reference at 08lst01.pl line 9.

43CS 299 – Web Programming and Design

The ref() Function (Con’t.)

• ref() function can help you check the reference type before dereferencing a reference

– ref(10); # return value is undefined– ref(\10); # return value is “SCALAR”– ref(\(1..10)); # return value is “ARRAY”– ref(\{1 => “Joe”}); # return value is “HASH”– ref(\&firstSub); # return value is “CODE”– ref(\\10); # return value is “REF”

firstSub( 10, ("A".."E") ); sub firstSub {

my($ref_firstArray, $ref_secondArray) = @_ ; print("The first array is @{$ref_firstArray}.\n") if (ref($ref_firstArray) eq "ARRAY");print("The second array is @{$ref_secondArray}.\n" if (ref($ref_secondArray) eq "ARRAY");

}

44CS 299 – Web Programming and Design

Creating a Data Record

• Associate arrays (hashes) are extremely useful to store information that facilitates easy retrieval

%recordOne = ( "Name" => "Jane Hathaway", "Address" => "123 Anylane Rd.", "Town" => "AnyTown", "State" => "AnyState", "Zip" => "12345-1234“

); %recordTwo = ( "Name" => "Kevin Hughes",

"Address" => "123 Allways Dr.", "Town" => "AnyTown", "State" => "AnyState", "Zip" => "12345-1234"

); @database = ( \%recordOne, \%recordTwo );print( %{$database[0]}->{"Address"} . "\n");

45CS 299 – Web Programming and Design

More on Perl Programming

• Files (Input/Output)• Regular Expression• Special Variables

46CS 299 – Web Programming and Design

Using Files

• Four basic operations: open, read, write, close• Three standard file handles:

– STDIN: reads program input from the computer’s keyboard

– STDOUT: displays program output to the computer’s monitor

– STDERR: displays program errors, equivalent to STDOUT most of the time

47CS 299 – Web Programming and Design

Read From STDIN

• Read a line of input from the standard input, STDIN• This will continue until you press Ctrl+D on Unix

systems

while (<STDIN>) { # diamond operators, default $_ print();}

# the following program acts the same as the abovewhile ($tmpline = <STDIN>) {

print ($tmpline);}

48CS 299 – Web Programming and Design

Using the Diamond Operators

• If no file handle is used with the <>, Perl will examine the @ARGV special variable. If @ARGV has no elements, then the diamond operator will read from STDINwhile (<>) {

print();}

>perl file2.pl str1.pl str2.pl

Perl will create the @ARGV array from the command line. Each file name on the command line – after the programwill be added to the @ARGV array as an element.Normally, using <> to iterate over a list of filenames is handy.

49CS 299 – Web Programming and Design

File Functions

• Open a file to read:$INPUT_FILE = "fixed.dat"; open(INPUT_FILE); @array = <INPUT_FILE>; close(INPUT_FILE); foreach (@array) {

print(); }

A file can be opened in many different ways: to read, write, append, etc.

• Or:open(FILE, “<fixed.dat”); @array = <FILE>; close(FILE); foreach (@array) {

print(); } • Or open (FILE, “fixed.dat”)

|| die “cannot open file”;

while (<FILE>) {

print $_;

}

close (FILE);

50CS 299 – Web Programming and Design

Examples

• Call the open() function to open “message.log” file for writing with LOGFILE as the file handle

if (open(LOGFILE, ">message.log")) {

print LOGFILE ("This is message number 1.\n"); print LOGFILE ("This is message number 2.\n"); close(LOGFILE);

} • Call the open() function to append to “message.log”if (open(LOGFILE, ">>message.log")) {

print LOGFILE ("This is message number 3.\n"); close(LOGFILE);

}

51CS 299 – Web Programming and Design

Reading into a Hash

• Create a hash table using the record number special variable ($.) as the key and the line of input ($_) as the value

open(FILE, "<fixed.dat"); while (<FILE>) {

$hash{$.} = $_; }close(FILE); foreach (keys %hash) {

print("$_: $hash{$_}"); }

CS 299 – Web Programming and Design

Regular Expressions

53CS 299 – Web Programming and Design

What are Regular Expressions?

• A regular expression is a pattern to be matched against a string: /regular_expression/

• Three main uses for regular expressions in Perl:– matching: m/Pattern/ operator, which returns true if

Pattern is found in $_– substitution: s/Pattern/Replacement/ operator, which

replaces the sub-string matched by Pattern with Replacement

– translation: tr/Characters/Replacements/ operator, which replaces characters specified by Characters with the characters in Replacements

• All three operators work with $_ as the string to search• You can use the binding operators to search a variable

other than $_

54CS 299 – Web Programming and Design

Special Pattern Matching Character Operators

• Meaning of the meta-characters:– \ Quote the next meta-character– ^ Match the beginning of the line– . Match any character (except newline)– $ Match the end of the line (or before newline at the

end)– | Alternation– () Grouping– [] Character class

55CS 299 – Web Programming and Design

Control Characters and Quantifiers

• Control characters:– \d: digit– \s: space– \w: word– \D, \S, \W are the negations of the above– \b: Match a word boundary– \B: Match a non-word boundary

• Quantifiers:– * Match 0 or more times– + Match 1 or more times– ? Match 0 or 1 times– {n} Match exactly n times– {n, } Match at least n times– {n, m} Match at least n but not more than m times

56CS 299 – Web Programming and Design

Parenthesis

• Parenthesis can be used to delimit special matches– (abc)* matches “”, abc, abcabc, abcabcabc, …– (a|b)(c|d) matches ac, ad, bc, bd

– dave(.)marshall\1 matches daveXmarshallX, but not daveXmarshallY

– a(.)b(.)c\2d\1 matches axbycydx– a(.*)b\1c matches aFREDbFREDc but not aXXbXc

57CS 299 – Web Programming and Design

Pattern Examples

• /b.t/ # match to bat, bit, but, bbt, bct …• /b[aiu]t/ # can only match to bat, bit, or but• [0123456789] # any single digit• [0-9] # also any single digit• [a-z] # any single lower case letter• [a-zA-Z] # any single letter• [0-9\-] # 0 to 9 plus minus character

• [^0-9] # any single non-digit• [^aeiouAEIOU] # any single non-vowel

58CS 299 – Web Programming and Design

More Pattern Examples

• Self-matching characters: any character will match itself unless it is a meta-character or one of $, @, %, &– m/a/; will match “a” m/\&/; will mach “&”

• Character sequences– m/abc/; will match “abc” but not “cba” or “bca”

• Alternation: match more than one string– m/(dog|cat)/; will match “dog” or “cat”

• Character classes: []– m/[0123456789]/; will match any decimal digit

• Anchors: – m/^one/; will match if the search string starts with a

character sequence “one”– m/(last|end)$/; will match if the search string ends with a

character sequence “last” or “end”– m/(Jack$|^Tom)/;

59CS 299 – Web Programming and Design

More Pattern Examples, Continue

• Quantifiers– m/a{5}/;

• Word boundaries: \b meta-sequence– m/\bfoo/; will match “foobar” but not “barfoo”– m/foo\b/; will match “foo” but not “foobar”

60CS 299 – Web Programming and Design

The Matching Operator (m//)

• m// is used to find patterns in strings, but it only searches the $_ variable

• Example:$_ = “AAA bbb AAA”;print “Found bbb\n” if m/bbb/;

print “Found bbb\n” if /bbb/; # you can even leave off “m”

• Another example:$target = “Tom”;open(INPUT, “<file.dat”);while (<INPUT>) {

if (/$target/) {print “Found $target on line $.\n”;

}}close (INPUT);

61CS 299 – Web Programming and Design

The Matching Options

• g: global (finds all occurrences of the pattern in the string)

• i: ignores the case of characters • …

• Example$_ = “AAA BBB CCC”;print “Found bbb\n” if m/bbb/i;

62CS 299 – Web Programming and Design

Substitution Operations

• s/Pattern/Replacement/

• Example:– $CGI_in_val =~ s/\+/ /ge– replace the + character from CGI input with a space– g: global– i: ignore case– e: evaluate the replacement pattern as an

expression

63CS 299 – Web Programming and Design

Translation Operations

• tr/Characters/Replacements/

• Example:– tr/a/z/: translates all occurrences of “a” into “z”– tr/ab/z/: translates all “a” and “b” characters in to

“z”– tr/WWW/ABC/: translates all “W” characters into “A”,

the rest of the replacement characters are ignored

64CS 299 – Web Programming and Design

Binding Operators (=~ and !~)

• The binding operators let you match against a specified string (rather than $_)

• In CGI, we frequently need to match against input name/values

$infile = <>; # whatever

if ($infile =~ /\.gif$/){ # file is gif format file, which ends in a .gif …}

65CS 299 – Web Programming and Design

Split & Join

• Split function is used to split a string into smaller sections

• Join function is the opposite• Examples:

– Split a string into words: • s/^\s+//;• @array = split;

– Or:• $line =~ s/^\s+//;• @array = split(/\W/, $line);

– Split a string into characters:• @array = split(//);

– Split a string based on a delimiter sequence of characters:• @array = split(/:/);

CS 299 – Web Programming and Design

Perl/CGI

67CS 299 – Web Programming and Design

CGI Programming in Perl

• CGI: the standard programming interface between Web servers and external programs– Common: interacts with many different OSs– Gateway: provides users with a way to gain access to different

programs– Interface: uses a well-defined method to interact with a Web

server• CGI standard lets Web browsers pass information to programs

written in any language

68CS 299 – Web Programming and Design

Writing and Running CGI Scripts

• CGI scripts can be compiled programs or batch files or any executable entity

• Typically CGI scripts are written in:– Perl scripts– C/C++ programs– Unix Scripts

• The 2 most common ways of running a CGI script are:– From an HTML form – the “action” attribute of the form

specifies the CGI script to be run– Direct URL reference – A CGI script can be run directly by

giving the URL explicitly in HTML

69CS 299 – Web Programming and Design

Why Use Perl for CGI?

• Perl is the standard for CGI programming:– Socket support – create programs that interface

seamlessly with Internet protocols– Pattern matching – ideal for handling form data and

searching text– Flexible text handling – no details to worry– Advantage of an interpreted language – simplicity in

development, debugging, and revision

70CS 299 – Web Programming and Design

How Does CGI Work?

• CGI programs are always placed on a disk that the Web server has access to

• Web servers are generally configured so that all CGI applications are placed into a cgi-bin directory

71CS 299 – Web Programming and Design

Invoke CGI Programs

• There are many ways to invoke CGI programs besides using a web browser to visit the URL

• We can start CGI programs from:– a hypertext link

• <a href=“cgi-user/ftang/www/cgi-bin/hello.cgi”>Click here to run the hello CGI program</a>

– a button on a HTML form– a server-side include

• We can use a question mark to pass information to a CGI program. – For example, passing keywords that will be used in a search

• <a href=“cgi-user/ftang/www/cgi-bin/search.pl?Wine+1993”> Search for 1993 Wine</a>

• The information follows the question mark will be available to your CGI program through the QUERY_STRING environment variables.

• Generally speaking, visitors to your web site should never have to type in the URL for a CGI program.

• A hypertext link should always be provided to start the program.

72CS 299 – Web Programming and Design

The Hello World Example

• When the web server executes your CGI program, it automatically opens the STDIN, STDOUT, STDERR file handles for you.– STDIN: the standard input of your CGI program might

contain information that was generated by an HTML form. Otherwise, you shouldn’t use STDIN.

– STDOUT: the standard output of your CGI program is linked to the STDIN of the web browser.

– STDERR: The standard output of your CGI program is linked to the web server’s log file.

73CS 299 – Web Programming and Design

CGI Script Output

• A CGI script must send information back in the following format:– The output header– A blank line– The output data

• For examples:– print(“Content Type: text/html\n\n”);

• Note: Between the Header and Data there MUST be a blank line

74CS 299 – Web Programming and Design

Output Header: Content-Type

• The output header for a CGI script must specify an output type

• 3 forms of Header Type:– Content-Type:

• text/html, text/plain, image/gif, image/jpeg, application/, postscript, video/mpeg

– Location– Status

• Content-Type is the most popular type

• See our hello world example:– http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/hell

o.pl

75CS 299 – Web Programming and Design

Output Header: Location

• The Location header is used to redirect the client Web browser to another page

• For example:– print "Location: http://www.google.com/\n\n";

76CS 299 – Web Programming and Design

CGI Script Input

• A CGI script will often require some form of input in order to operate

• We’ll study:– What form of input a CGI can receive– How a CGI receives input– How to process the input in a CGI Perl script– How a useful Perl library makes this easy

77CS 299 – Web Programming and Design

Accepting Input from the Browser

• A CGI script can receive data in many ways:– Environment variables– Standard input

• Data can be passed as standard input through the POST method of an HTML form

– Arguments of CGI script• If you call a CGI script directly or use the GET method

of a form, arguments are following the “?” after the script URL and multiple arguments are separated by &

78CS 299 – Web Programming and Design

Receiving/Processing Information in CGI

• Two basic ways:– Do it yourself: write Perl code to process the input– Use pre-written Perl libraries

• http://stein.cshl.org/WWW/CGI/ a Perl5 CGI Library

79CS 299 – Web Programming and Design

Forms and CGI: What Can They Do?

• A CGI program can be used to:– accept the data which the user inputs – do something with it

• email the data to someone• add an entry to a database• write out a text file• create a customized display• anything that you can program

– send a reply back to user

80CS 299 – Web Programming and Design

Form Processing

• The two most important options with the <form> tag:– method: specifies the manner in which form

information is passed to the CGI scripts:• POST: contacts the CGI program and once connection

established sends data• GET: Contacts the CGI program and sends data in a

single transaction

– action: specifies the URL of the CGI script that will be invoked when the submit button is clicked

• <from method = “post” action=“/cgi-bin/hello.pl”>

81CS 299 – Web Programming and Design

Handling Form Information

• The GET method– <form method=“get” action=“/cgi-bin/guestbook.pl”>

– The GET method appends all of the form data to the end of the URL used to invoke the CGI script.

– A question mark is used to separate the original URL and the form information.

• http://somserver/cgi-bin/foo.pl?fname=Craig&lname=Kelley

– The GET method can’t be used for larger forms

82CS 299 – Web Programming and Design

Handling Form Information

• The POST method– <form method=“post” action=“cgi-bin/guestbook.pl”>– The POST method sends all the form information to

the CGI program using the STDIN file handle.

• Which to use?– It is good practice to use the GET method whenever

you are able to because the POST method is more difficult for a user to manage, and it doesn’t function well with a browser’s back or history button.

– It is good to use the POST method when something secure or private is being sent such as password or a credit card information.

83CS 299 – Web Programming and Design

CGI.pm: a Perl 5 CGI Library

• This Perl 5 library uses objects to do many things. It– Uses objects to create Web fill-out forms on the fly and to

parse their contents– Provides a simple interface for parsing and interpreting

query strings passed to CGI scripts– Offers a rich set of functions for creating fill-out forms

• Everything is done through a “CGI” object• The most basic use of CGI.pm is to get the query

parameters submitted to your script. To do so, put the following at the top of your Perl/CGI programs:– Use CGI;– $query = new CGI;

– Or Use CGI qw(:standard);

http://stein.cshl.org/WWW/CGI/

84CS 299 – Web Programming and Design

Function Oriented vs. Object Oriented

#!/usr/local/bin/perl # imports standard set of

functionsuse CGI qw/:standard/;

print header(), start_html(-

title=>'Wow!'), h1('Wow!'), 'Look Ma, no hands!', end_html();

#!/usr/local/bin/perl

use CGI; $q = new CGI; print $q->header(), $q->start_html(-

title=>'Wow!'), $q->h1('Wow!'), 'Look Ma, no hands!', $q->end_html();

http://stein.cshl.org/WWW/CGI/#functionvsoo

85CS 299 – Web Programming and Design

What Can You Do with the Query Object?

• Fetch the names of all the parameters passed to your script– @names = $query->param;

• Fetch the value(s) of a named parameter– @values = $query->param(‘foo’); # or– $value = $query->param(‘foo’);

• And many other methods …

• Example:– http://www.csupomona.edu/~ftang/www/courses/CS299-S09/examples/simple.html

86CS 299 – Web Programming and Design

Printing HTML

• Instead of printing the quote marks and write in new line characters

• An easier way is to use a special print command in Perl:

• print <<AnyWord• …• AnyWord

#!/usr/bin/perlprint "Content-type: text/html\n\n";print <<ENDHTML;<html><head><title>CGI Test</title></head><body><a href="http://what.com">Click Here</a></body></html>

ENDHTML;

87CS 299 – Web Programming and Design

Save the Current State of a Form

• Save the state to a file– $query->save(FILEHANDLE);– The contents are written out as TAG=VALUE pairs

• Save the state in a self-referencing URL– $my_url = $query->self_url;– Useful when you want to jump around within a script-

generated document using internal anchors, but don’t want to disrupt the current contents.

88CS 299 – Web Programming and Design

Three Different Methods

• Print out the information after form submission– www.csupomona.edu/~ftang/www/cgi-bin/procform1.cgi

• Submit the information to an email address– www.csupomona.edu/~ftang/www/cgi-bin/procform2.cgi

• Save the information to a file (will not use this one)– www.csupomona.edu/~ftang/www/cgi-bin/procform3.cgi

• To run the above programs:– http://www.csupomona.edu/~ftang/www/courses/CS299-S0

9/examples/runcgi.html

89CS 299 – Web Programming and Design

Creating the HTTP Header

• Creating the standard header– print $query->header()

• default to ‘text/html’

• Creating the header for a redirection request– print $query->redirect(‘http://www.google.com’);

90CS 299 – Web Programming and Design

HTML Shortcuts

• Create an HTML header– print $query->start_html(many options);

• End an HTML document– print $query->end_html;

• Shortcut methods for many HTML tags• Importing CGI methods

– Single query object– import CGI module methods– use CGI qw(:standard);– $dinner = param(‘entrée’);

91CS 299 – Web Programming and Design

Other Supports

• Support for CSS– http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/style.pl – http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/bio.pl

• Support for JavaScript– Similar as CSS– check http://stein.cshl.org/WWW/CGI/ for details

– http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/contact.pl

92CS 299 – Web Programming and Design

Many Perl/CGI Examples

• http://stein.cshl.org/WWW/CGI/examples/