180 likes | 347 Views
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. Introduction to Perl. Perl is a script language Elements of csh, sh, awk Example: #! /usr/bin/perl
E N D
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
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”);
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");
Variables • Hash %animals = ("cat",10,"dog",12,"fish",23); print("1: $animals{'cat'}\n"); print("2: $animals{'dog'}\n"); print("3: $animals{'fish'}\n");
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"); }
Reading Input • Standard input print("enter a number: "); $number = <STDIN>; print("here it is: $number");
Reading from a File if (!open(FILE, "$ARGV[0]")) { print("cannot open: $ARGV[0]\n"); } while ($line = <FILE>) { print("$line"); }
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
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
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
Perl Libraries • Libraries of functions: use CGI qw(:standard); print(header); print(start_html("Hello World")); print h2("Test Website"); print(end_html);
Setting up a Web Page • Create subdirctory public_html • add files: index.html ten.cgi
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>
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);
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