chapter 9: perl and cgi programming guide to unix using linux third edition
TRANSCRIPT
![Page 1: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/1.jpg)
Chapter 9:
Perl and CGI Programming
Guide To UNIX Using Linux Third Edition
![Page 2: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/2.jpg)
Guide to UNIX Using Linux, Third Edition 2
Objectives
• Understand the basics of the Perl language
• Identify and use data types in Perl scripts
• Understand differences between the Awk program and Perl programming
![Page 3: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/3.jpg)
Guide to UNIX Using Linux, Third Edition 3
Objectives (continued)
• Access disk files in Perl
• Use Perl to sort information
• Set up a simple HTML Web page
• Understand how Perl and CGI are used for creating Web pages
![Page 4: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/4.jpg)
Guide to UNIX Using Linux, Third Edition 4
Introduction to Perl
Perl contains features found in other languages – it is very similar to C and also contains features found in Awk and shell programs
![Page 5: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/5.jpg)
Guide to UNIX Using Linux, Third Edition 5
Introduction to Perl (continued)
Perl can be directed to read its input from the keyboard
![Page 6: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/6.jpg)
Guide to UNIX Using Linux, Third Edition 6
Introduction to Perl (continued)
Perl uses decision structures such as if statements to control program flow
![Page 7: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/7.jpg)
Guide to UNIX Using Linux, Third Edition 7
Introduction to Perl (continued)
![Page 8: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/8.jpg)
Guide to UNIX Using Linux, Third Edition 8
Introduction to Perl (continued)
![Page 9: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/9.jpg)
Guide to UNIX Using Linux, Third Edition 9
Identifying Data Types
• Data may be represented in Perl in a variety of ways:– Variables and constants– Scalars– Numbers– Strings– Arrays– Hashes
![Page 10: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/10.jpg)
Guide to UNIX Using Linux, Third Edition 10
Variables and Constants
• Variables and constants are symbolic names that represent values stored in memory
• The value of a variable can change while the program runs
• The value of a constant does not change while the program runs
![Page 11: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/11.jpg)
Guide to UNIX Using Linux, Third Edition 11
Scalars
• Scalars are simple variables that hold a number or a string
• A string is any nonnumeric sequence of characters (including numbers treated as characters)
• Scalar variable names begin with a dollar sign ($)
![Page 12: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/12.jpg)
Guide to UNIX Using Linux, Third Edition 12
Numbers
• 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 an added convention with numeric literals to improve legibility: the underscore character (_)
![Page 13: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/13.jpg)
Guide to UNIX Using Linux, Third Edition 13
Strings
• Sequences of any types of characters
– Often used for logical analysis, sorts, or searches
– String literals are usually delimited by either single (‘) or double quotes (“)
– To put control and escape characters into strings, need to use \ notation, e.g., \n is a newline character
![Page 14: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/14.jpg)
Guide to UNIX Using Linux, Third Edition 14
Strings (continued)
![Page 15: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/15.jpg)
Guide to UNIX Using Linux, Third Edition 15
Strings (continued)
The use of special codes determined the output of this Perl script
![Page 16: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/16.jpg)
Guide to UNIX Using Linux, Third Edition 16
Arrays
• Variables that store an ordered list of scalar values 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
– Subscripts are zero-based
![Page 17: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/17.jpg)
Guide to UNIX Using Linux, Third Edition 17
Hashes
• Variables that represent 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
![Page 18: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/18.jpg)
Guide to UNIX Using Linux, Third Edition 18
Perl versus the Awk Program
This Awk program counts comment lines in a file. Awk doesn’t use while-type statements for looping.
![Page 19: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/19.jpg)
Guide to UNIX Using Linux, Third Edition 19
How Perl Accesses Disk Files
• Perl uses filehandles to reference files
• A filehandle is the name for an I/O connection between Perl and the OS
• Used to open, read, write, and close a file
• 3 standard filehandles: STDIN, STDOUT, STDERR
![Page 20: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/20.jpg)
Guide to UNIX Using Linux, Third Edition 20
How Perl Accesses Disk Files (continued)
• Can open a file
– With an explicit open statement
– By providing the file name at the command line (storing it in ARGV[0])
• Should always check for failure or EOF when opening a file
![Page 21: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/21.jpg)
Guide to UNIX Using Linux, Third Edition 21
How Perl Accesses Disk Files (continued)
Perl can access a file by passing the filename on the command line
![Page 22: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/22.jpg)
Guide to UNIX Using Linux, Third Edition 22
Using Perl to Sort
• Perl has a powerful sort operator
• Can sort strings or numbers
• Can sort in ascending or descending order
• Advanced sorting operations allow you to define your own sorting routine
![Page 23: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/23.jpg)
Guide to UNIX Using Linux, Third Edition 23
Using Perl to Sort Alphanumeric Fields
You can sort words in a Perl program into alphabetical order using the sort function
![Page 24: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/24.jpg)
Guide to UNIX Using Linux, Third Edition 24
Using Perl to Sort Numeric Fields
You can sort numeric fields in a Perl program by using a sort subroutine
![Page 25: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/25.jpg)
Guide to UNIX Using Linux, Third Edition 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 tags
• Tags give the document special properties when it is viewed in a Web browser
![Page 26: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/26.jpg)
Guide to UNIX Using Linux, Third Edition 26
Setting Up a Web Page (continued)
• Hyperlinks load another document into the browser when clicked
• Web pages are published on a web server
– Apache is a common Web server software
• Linux has a loopback networking feature
– Lets you access your own system as if it were an external network
– Useful for testing Web pages before publishing
![Page 27: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/27.jpg)
Guide to UNIX Using Linux, Third Edition 27
Creating a Simple Web Page
• Two ways to create HTML documents:
– Typing the text and desired embedded tags
– Using a visual HTML editor
• Two main parts to HTML code
– Head contains the title, which appears on the top bar of the browser window
– Body defines what appears in the browser window
![Page 28: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/28.jpg)
Guide to UNIX Using Linux, Third Edition 28
Creating a Simple Web Page (continued)
An HTML document viewed in Mozilla
![Page 29: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/29.jpg)
Guide to UNIX Using Linux, Third Edition 29
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
![Page 30: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/30.jpg)
Guide to UNIX Using Linux, Third Edition 30
CGI Overview (continued)
• Perl scripts are written to get, process, and return information through Web pages (dynamic pages)
• Main objects in dynamic Web pages are forms that allow you to collect input data from a Web page and send it to a server
![Page 31: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/31.jpg)
Guide to UNIX Using Linux, Third Edition 31
CGI Overview (continued)
This Web page contains a form that collects information from a user to submit to a server via CGI
![Page 32: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/32.jpg)
Guide to UNIX Using Linux, Third Edition 32
Chapter Summary• Perl is a interpreted scripting language that can be
combined with CGI to create interactive Web pages
• Perl blends features found in C, Awk, and shell programs
• Perl includes
– An if-else statement as a decision structure
– Numeric and string relational operators
– Arithmetic operators
![Page 33: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/33.jpg)
Guide to UNIX Using Linux, Third Edition 33
Chapter Summary
• Perl’s data types include numbers, strings, arrays, and hashes
• Perl and Awk are both good for applications requiring pattern matching
• Unlike Awk, Perl includes an explicit while looping structure
• Perl includes a powerful sort feature
![Page 34: Chapter 9: Perl and CGI Programming Guide To UNIX Using Linux Third Edition](https://reader035.vdocuments.site/reader035/viewer/2022081421/56649d6e5503460f94a4f64f/html5/thumbnails/34.jpg)
Guide to UNIX Using Linux, Third Edition 34
Chapter Summary
• Web pages are created using Hypertext Markup Language (HTML)
• An HTML document contains embedded tags that specify document properties and links to other pages
• CGI is a protocol or set of rules governing how browsers and servers communicate