Transcript perl-cgi

Perl/CGI
CS 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
…
CS 299 – Web Programming and Design
2
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
CS 299 – Web Programming and Design
3
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
CS 299 – Web Programming and Design
4
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
CS 299 – Web Programming and Design
5
Our First CGI Program – hello.cgi
#!/usr/local/bin/perl –w
print
print
print
print
print
print
print
“Content-type: text/html\r\n\r\n”;
“<html>\n”;
“<head><title>Hello World!</title></head>”;
“<body style=\”text-align:center\”>\n”;
“<h1>Hello World!</h1>\n”;
“</body>”;
“</html>\n”;
exit(0);
http://www.csupomona.edu/~ftang/www/cgi-bin/hello.cgi
http://www.csupomona.edu/cgi-user/ftang/www/cgi-bin/hello.cgi
CS 299 – Web Programming and Design
6
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
CS 299 – Web Programming and Design
7
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
CS 299 – Web Programming and Design
8
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”
CS 299 – Web Programming and Design
9
Basic Perl
•
•
•
•
•
•
Numeric and String Literals
Variables
Operators
Statements
Control Statements
Functions
CS 299 – Web Programming and Design
10
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
CS 299 – Web Programming and Design
11
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
CS 299 – Web Programming and Design
12
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
CS 299 – Web Programming and Design
13
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
CS 299 – Web Programming and Design
14
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
CS 299 – Web Programming and Design
15
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);
CS 299 – Web Programming and Design
16
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”);
CS 299 – Web Programming and Design
17
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”);
CS 299 – Web Programming and Design
18
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
CS 299 – Web Programming and Design
19
Operators, Continue…
• String relational operators
–
–
–
–
–
–
op1
op1
op1
op1
op1
op1
op2
– op1
eq op2: returns true if equivalent
ne op2: returns true if not equivalent
lt op2: returns true if op1 is less than op2
le op2: returns true is op1 is less than or equal to op2
gt op2: returns true if op1 is greater than op2
ge op2: returns true if op1 is greater than or equal ot
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
CS 299 – Web Programming and Design
20
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;
– …
CS 299 – Web Programming and Design
21
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
CS 299 – Web Programming and Design
22
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
CS 299 – Web Programming and Design
23
So Far, We Have Learned …
• Literals
• Variables
• Operators
• Statement/Statement Block
• Function
• Reference
CS 299 – Web Programming and Design
24
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”);
CS 299 – Web Programming and Design
25
if/unless statement
if (expression)
{
true_statement_1;
….
true_statement_n;
}
curly braces are required
even if there’s only one
statement
if () {…} else {…}
if () {…} elsif () {…} else {}
unless ($age < 18) {print “Go and Vote\n”;}
CS 299 – Web Programming and Design
26
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;
}
CS 299 – Web Programming and Design
27
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() here
if (!length($name)) {
print(“Msg: Zero length input. Please try again\n”);
redo;
}
print(“Thank you, ” . uc($name) . “\n”); # uc() uppercass
}
CS 299 – Web Programming and Design
28
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.
CS 299 – Web Programming and Design
29
A Function Example
$areaOfFirstCircle = areaOfCircle(5);
print("$areaOfFirstCircle\n");
sub areaOfCircle
{
$radius = $_[0];
return(3.1415 * ($radius ** 2));
}
CS 299 – Web Programming and Design
30
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");
}
CS 299 – Web Programming and Design
31
Passing Parameters by Reference
• Perl parameters are called by reference, changing their values
in the function also changes their values in the main program.
@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";
}
CS 299 – Web Programming and Design
@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";
}
32
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
CS 299 – Web Programming and Design
33
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
CS 299 – Web Programming and Design
34
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
CS 299 – Web Programming and Design
35
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
CS 299 – Web Programming and Design
36
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
CS 299 – Web Programming and Design
37
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
CS 299 – Web Programming and Design
38
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
CS 299 – Web Programming and Design
39
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
CS 299 – Web Programming and Design
40
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.
CS 299 – Web Programming and Design
41
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.
CS 299 – Web Programming and Design
42
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");
}
CS 299 – Web Programming and Design
43
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");
CS 299 – Web Programming and Design
44
More on Perl Programming
• Files (Input/Output)
• Regular Expression
• Special Variables
CS 299 – Web Programming and Design
45
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
CS 299 – Web Programming and Design
46
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 above
while ($tmpline = <STDIN>) {
print ($tmpline);
}
CS 299 – Web Programming and Design
47
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 STDIN
while (<>) {
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 program
will be added to the @ARGV array as an element.
Normally, using <> to iterate over a list of filenames is handy.
CS 299 – Web Programming and Design
48
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.
CS 299 – Web Programming and Design
• 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);
49
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);
}
CS 299 – Web Programming and Design
50
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
51
Regular Expressions
CS 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 $_
CS 299 – Web Programming and Design
53
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
CS 299 – Web Programming and Design
54
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
CS 299 – Web Programming and Design
55
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
CS 299 – Web Programming and Design
56
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
CS 299 – Web Programming and Design
57
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)/;
CS 299 – Web Programming and Design
58
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”
CS 299 – Web Programming and Design
59
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);
CS 299 – Web Programming and Design
60
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;
CS 299 – Web Programming and Design
61
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
CS 299 – Web Programming and Design
62
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
CS 299 – Web Programming and Design
63
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
…
}
CS 299 – Web Programming and Design
64
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
65
Perl/CGI
CS 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
CS 299 – Web Programming and Design
67
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
CS 299 – Web Programming and Design
68
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
CS 299 – Web Programming and Design
69
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
CS 299 – Web Programming and Design
70
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.
CS 299 – Web Programming and Design
71
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.
CS 299 – Web Programming and Design
72
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
CS 299 – Web Programming and Design
73
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/cgibin/hello.pl
CS 299 – Web Programming and Design
74
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";
CS 299 – Web Programming and Design
75
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
CS 299 – Web Programming and Design
76
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 &
CS 299 – Web Programming and Design
77
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
CS 299 – Web Programming and Design
78
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
CS 299 – Web Programming and Design
79
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”>
CS 299 – Web Programming and Design
80
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
CS 299 – Web Programming and Design
81
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.
CS 299 – Web Programming and Design
82
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;
http://stein.cshl.org/WWW/CGI/
– Or Use CGI qw(:standard);
CS 299 – Web Programming and Design
83
Function Oriented vs. Object Oriented
#!/usr/local/bin/perl
# imports standard set of
functions
use 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
CS 299 – Web Programming and Design
84
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
CS 299 – Web Programming and Design
85
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/perl
print "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
;
CS 299 – Web Programming and Design
86
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.
CS 299 – Web Programming and Design
87
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/CS299S09/examples/runcgi.html
CS 299 – Web Programming and Design
88
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’);
CS 299 – Web Programming and Design
89
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’);
CS 299 – Web Programming and Design
90
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
CS 299 – Web Programming and Design
91
Many Perl/CGI Examples
• http://stein.cshl.org/WWW/CGI/examples/
CS 299 – Web Programming and Design
92