1 / 37

Chapter Nine

Chapter Nine. Perl and CGI Programming. Lesson A. Learning to Use Perl. Objectives. Understand the basics of the Perl language Identify and use data types in Perl scripts Understand the difference between the Awk program and Perl programming. Objectives.

ppauline
Télécharger la présentation

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. Lesson A Learning to Use Perl

  3. Objectives • Understand the basics of the Perl language • Identify and use data types in Perl scripts • Understand the difference between the Awk program and Perl programming

  4. Objectives • Create simple Perl scripts with variables and logic structures • Create simple Perl scripts to read and sort data files

  5. Introduction to Perl Perl contains a features found in other languages – it is very similar to the C and also contains features found in Awk and shell programs

  6. Introduction to Perl Perl can be directed to read its input from the keyboard

  7. Introduction to Perl

  8. Introduction to Perl

  9. Identifying Data Types • Data may be represented in Perl in a variety of ways: • Variables and constants • Scalars • Numbers • Strings • Arrays • Hashes

  10. Identifying Data Types • Variables are symbolic names that represent values stored in memory • The value of a variable can change while the program runs; constants do not change • Scalars are simple variables that hold a number or a string • Scalar variable names begin with a dollar sign ($)

  11. Identifying Data Types • Numbers are stored as either signed integers, or as double-precision floating-point values • Numeric literals can be either integers or floating-point values • Perl uses added convention with numeric literals to improve legibility: the underscore character (_)

  12. Identifying Data Types • Strings are sequences of any types of characters • Strings are often used for logical analysis, sorts, or searches • String literals are usually delimited by either single (‘) or double quotes (“)

  13. Identifying Data Types

  14. Identifying Data Types

  15. Identifying Data Types The use of special codes determined the output of this Perl script

  16. Identifying Data Types • Arrays are variables that store an ordered list of scalar values that are accessed with numeric subscripts • An at sign (@) precedes the name of an array when assigning it values • Use the dollar sign ($) when processing the individual elements of an array

  17. Identifying Data Types • Arrays are variables that represent a set of key/value pairs • A percent sign (%) precedes the name of a hash variable when assigning it a value • Use the dollar sign ($) to refer to a single element of a hash

  18. Perl versus the Awk Program The Awk program uses fewer lines of code to resolve pattern-matching extractions than does Perl

  19. How Perl Accesses Disk Files Perl uses filehandles to reference files, as shown in this figure

  20. Using Perl to Sort You can sort words in a Perl program into alphabetical order using the sort function

  21. Using Perl to Sort Perl can access a file by passing the filename on the command line

  22. Using Perl to Sort You can sort numeric fields in a Perl program by using a sort subroutine

  23. Lesson B Creating an Interactive Web Page

  24. Objectives • Set up an HTML Web page • Use Perl and CGI scripts to make your web pages interactive • Use X Window and Netscape to retrieve Web pages

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

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

  27. Creating Web Pages An HTML document viewed in Netscape Navigator

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

  29. Chapter Summary • Perl is used as a powerful text-manipulation tool similar to the Awk program • Perl is written in scripts that are translated and executed by the Perl program • The Perl programmer has to write process-handling instructions for data items to prevent misidentification of data types and subsequent processing errors • Perl has three basic data types: scalars, arrays, and hashes

  30. Chapter Summary • A list is an ordered group of simple variables or literals, separated by commas • Anything besides a textual sort must be handled with a sort subroutine • 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 • To run your Web pages, you need to be in X Window and have access to a Web browser

More Related