1 / 18

Chapter Nine

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

zona
Download Presentation

Chapter Nine

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 pages interactive

  3. 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”);

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

  5. Variables • Hash %animals = ("cat",10,"dog",12,"fish",23); print("1: $animals{'cat'}\n"); print("2: $animals{'dog'}\n"); print("3: $animals{'fish'}\n");

  6. 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"); }

  7. Reading Input • Standard input print("enter a number: "); $number = <STDIN>; print("here it is: $number");

  8. Reading from a File if (!open(FILE, "$ARGV[0]")) { print("cannot open: $ARGV[0]\n"); } while ($line = <FILE>) { print("$line"); }

  9. 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

  10. 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

  11. 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

  12. Perl Libraries • Libraries of functions: use CGI qw(:standard); print(header); print(start_html("Hello World")); print h2("Test Website"); print(end_html);

  13. Setting up a Web Page • Create subdirctory public_html • add files: index.html ten.cgi

  14. 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>

  15. Interactive Web Page

  16. 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);

  17. Interactive Web Page

  18. 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

More Related