370 likes | 391 Views
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.
E N D
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 • Create simple Perl scripts with variables and logic structures • Create simple Perl scripts to read and sort data files
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
Introduction to Perl Perl can be directed to read its input from the keyboard
Identifying Data Types • Data may be represented in Perl in a variety of ways: • Variables and constants • Scalars • Numbers • Strings • Arrays • Hashes
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 ($)
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 (_)
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 (“)
Identifying Data Types The use of special codes determined the output of this Perl script
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
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
Perl versus the Awk Program The Awk program uses fewer lines of code to resolve pattern-matching extractions than does Perl
How Perl Accesses Disk Files Perl uses filehandles to reference files, as shown in this figure
Using Perl to Sort You can sort words in a Perl program into alphabetical order using the sort function
Using Perl to Sort Perl can access a file by passing the filename on the command line
Using Perl to Sort You can sort numeric fields in a Perl program by using a sort subroutine
Lesson B Creating an Interactive Web Page
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
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
Creating Web Pages An HTML document viewed in Netscape Navigator
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
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
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