Transcript ch_09

Chapter Nine
Perl and CGI Programming
Objectives
Basic features of Perl language
Set up an HTML Web page
Use Perl and CGI scripts to make your
web pages interactive
2
Introduction to Perl
Perl is a script language
– Elements of csh, sh, awk
Example:
#! /usr/bin/perl
# example of perl script
print(“hello world\n”);
3
Variables
Scalar
$word = “test”
print(“here it is: $word \n”);
Array
@list = ("one", "two", "three");
print("here it is: $list[0]\n");
print("here it is: $list[1]\n");
print("here it is: $list[2]\n");
4
Variables
Hash
%animals =
("cat",10,"dog",12,"fish",23);
print("1: $animals{'cat'}\n");
print("2: $animals{'dog'}\n");
print("3: $animals{'fish'}\n");
5
Command line arguments
$ARGV[0], $ARGV[1], …
print("$ARGV[0], $ARGV[1]\n");
With a loop:
$size = @ARGV;
for ($i = 0; $i < $size; $i++)
{
print("$ARGV[$i]\n");
}
6
Reading Input
Standard input
print("enter a number: ");
$number = <STDIN>;
print("here it is: $number");
7
Reading from a File
if (!open(FILE, "$ARGV[0]"))
{
print("cannot open:
$ARGV[0]\n");
}
while ($line = <FILE>)
{
print("$line");
}
8
Setting Up a Web Page
Web pages can be created using HTML
(Hypertext markup Language)
HTML is a format for creating documents with
embedded codes known as tags and when the
document is viewed in a Web browser, the tags
give the document special properties
After using HTML to create a Web page, the
page is published on a Web server, this allows
others to access the document via the Internet
9
Creating a Web Page
HTML documents are created by typing its
text and the desired embedded tags
There are two parts to the HTML code
– The head contains the title, which appears on
the top bar of the browser window
– The body defines what appears within the
browser window
10
CGI Overview
CGI (Common Gateway Interface) is a protocol,
or set of rules, governing how browsers and
servers communicate
Scripts that send or receive information from a
server need to follow the CGI protocol
Perl is the most commonly used language for
CGI programming
Perl scripts are written to get, process, and
return information through Web pages
11
Perl Libraries
Libraries of functions:
use CGI qw(:standard);
print(header);
print(start_html("Hello World"));
print h2("Test Website");
print(end_html);
12
Setting up a Web Page
Create subdirctory
public_html
add files:
index.html
ten.cgi
13
Interactive Web Page
<html><head>
<title>COP 3344</title></head>
<body>
<h1>User Inquiry</h1>
<form method=get action=eleven.cgi>
Enter your name:
<input type="text" size=20
name="userid"><br>
<input type="submit" value="Submit">
</form>
</body></html>
14
Interactive Web Page
15
Interactive Web Page
#! /usr/bin/perl
# example perl script
use CGI qw(:standard);
print(header);
print(start_html("Anser Page"));
print h2("Answer Page");
print("Welcome: ", param('userid'));
print(end_html);
16
Interactive Web Page
17
Chapter Summary
Perl is a powerful scripting language
Perl scripts are used to create web pages
An HTML document contains two parts: a
head and a body
CGI is a protocol or set of rules
governing how browsers and servers
communicate
18