Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML...

18
Chapter Nine Chapter Nine Perl and CGI Perl and CGI Programming Programming

Transcript of Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML...

Page 1: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

Chapter NineChapter Nine

Perl and CGI ProgrammingPerl and CGI Programming

Page 2: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

22

ObjectivesObjectives

Basic features of Perl languageBasic features of Perl language

Set up an HTML Web pageSet up an HTML Web page

Use Perl and CGI scripts to make your Use Perl and CGI scripts to make your web pages interactiveweb pages interactive

Page 3: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

33

Perl is a script language

– Elements of csh, sh, awk

Example:Example:

#! /usr/bin/perl#! /usr/bin/perl

# example of perl script# example of perl script

print(“hello world\n”);print(“hello world\n”);

Introduction to PerlIntroduction to Perl

Page 4: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

44

VariablesVariables

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");

Page 5: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

55

VariablesVariables

Hash%animals = ("cat",10,"dog",12,"fish",23);

print("1: $animals{'cat'}\n");

print("2: $animals{'dog'}\n");

print("3: $animals{'fish'}\n");

Page 6: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

66

Command line argumentsCommand 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");

}

Page 7: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

77

Reading InputReading Input

Standard inputStandard input

print("enter a number: ");print("enter a number: ");

$number = <STDIN>;$number = <STDIN>;

print("here it is: $number");print("here it is: $number");

Page 8: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

88

Reading from a FileReading from a File

if (!open(FILE, "$ARGV[0]"))if (!open(FILE, "$ARGV[0]")){{ print("cannot open: $ARGV[0]\print("cannot open: $ARGV[0]\n");n");

}}while ($line = <FILE>)while ($line = <FILE>){{ print("$line");print("$line");}}

Page 9: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

99

Setting Up a Web PageSetting Up a Web Page

Web pages can be created using HTML Web pages can be created using HTML (Hypertext markup Language)(Hypertext markup Language)

HTML is a format for creating documents with HTML is a format for creating documents with embedded codes known as tags and when the embedded codes known as tags and when the document is viewed in a Web browser, the tags document is viewed in a Web browser, the tags give the document special propertiesgive the document special properties

After using HTML to create a Web page, the After using HTML to create a Web page, the page is published on a Web server, this allows page is published on a Web server, this allows others to access the document via the Internetothers to access the document via the Internet

Page 10: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1010

Creating a Web PageCreating a Web Page

HTML documents are created by typing its HTML documents are created by typing its text and the desired embedded tagstext and the desired embedded tags

There are two parts to the HTML codeThere are two parts to the HTML code– The head contains the title, which appears on The head contains the title, which appears on

the top bar of the browser windowthe top bar of the browser window– The body defines what appears within the The body defines what appears within the

browser windowbrowser window

Page 11: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1111

CGI OverviewCGI Overview

CGI (Common Gateway Interface) is a protocol, CGI (Common Gateway Interface) is a protocol, or set of rules, governing how browsers and or set of rules, governing how browsers and servers communicateservers communicate

Scripts that send or receive information from a Scripts that send or receive information from a server need to follow the CGI protocolserver need to follow the CGI protocol

Perl is the most commonly used language for Perl is the most commonly used language for CGI programmingCGI programming

Perl scripts are written to get, process, and Perl scripts are written to get, process, and return information through Web pagesreturn information through Web pages

Page 12: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1212

Perl LibrariesPerl Libraries

Libraries of functions:Libraries of functions:

use CGI qw(:standard);use CGI qw(:standard);

print(header);print(header);

print(start_html("Hello World"));print(start_html("Hello World"));

print h2("Test Website");print h2("Test Website");

print(end_html);print(end_html);

Page 13: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1313

Setting up a Web PageSetting up a Web Page

Create subdirctoryCreate subdirctorypublic_htmlpublic_html

add files:add files:index.htmlindex.html

ten.cgiten.cgi

Page 14: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1414

Interactive Web PageInteractive Web Page<html><head><html><head><title>COP 3344</title></head><title>COP 3344</title></head><body><body><h1>User Inquiry</h1><h1>User Inquiry</h1><form method=get action=eleven.cgi><form method=get action=eleven.cgi>Enter your name: Enter your name: <input type="text" size=20 <input type="text" size=20 name="userid"><br>name="userid"><br>

<input type="submit" value="Submit"><input type="submit" value="Submit"></form></form></body></html></body></html>

Page 15: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1515

Interactive Web PageInteractive Web Page

Page 16: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1616

Interactive Web PageInteractive Web Page#! /usr/bin/perl#! /usr/bin/perl# example perl script# example perl scriptuse CGI qw(:standard);use CGI qw(:standard);print(header);print(header);print(start_html("Anser Page"));print(start_html("Anser Page"));print h2("Answer Page");print h2("Answer Page");print("Welcome: ", param('userid'));print("Welcome: ", param('userid'));print(end_html);print(end_html);

Page 17: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1717

Interactive Web PageInteractive Web Page

Page 18: Chapter Nine Perl and CGI Programming. 2 Objectives Basic features of Perl language Set up an HTML Web page Use Perl and CGI scripts to make your web.

1818

Chapter SummaryChapter Summary

Perl is a powerful scripting languagePerl is a powerful scripting language

Perl scripts are used to create web pagesPerl scripts are used to create web pages

An HTML document contains two parts: a An HTML document contains two parts: a head and a bodyhead and a body

CGI is a protocol or set of rules CGI is a protocol or set of rules governing how browsers and servers governing how browsers and servers communicatecommunicate