You are previewing CGI Programming with Perl, 2nd Edition.
O'Reilly logo
CGI Programming with Perl, 2nd Edition

Book Description

Programming on the Web today can involve any of several technologies, but the Common Gateway Interface (CGI) has held its ground as the most mature method--and one of the most powerful ones--of providing dynamic web content. CGI is a generic interface for calling external programs to crunch numbers, query databases, generate customized graphics, or perform any other server-side task. There was a time when CGI was the only game in town for server-side programming; today, although we have ASP, PHP, Java servlets, and ColdFusion (among others), CGI continues to be the most ubiquitous server-side technology on the Web. CGI programs can be written in any programming language, but Perl is by far the most popular language for CGI. Initially developed over a decade ago for text processing, Perl has evolved into a powerful object-oriented language, while retaining its simplicity of use. CGI programmers appreciate Perl's text manipulation features and its CGI.pm module, which gives a well-integrated object-oriented interface to practically all CGI-related tasks. While other languages might be more elegant or more efficient, Perl is still considered the primary language for CGI. CGI Programming with Perl, Second Edition, offers a comprehensive explanation of using CGI to serve dynamic web content. Based on the best-selling CGI Programming on the World Wide Web, this edition has been completely rewritten to demonstrate current techniques available with the CGI.pm module and the latest versions of Perl. The book starts at the beginning, by explaining how CGI works, and then moves swiftly into the subtle details of developing CGI programs. Topics include:

  • Incorporating JavaScript for form validation

  • Controlling browser caching

  • Making CGI scripts secure in Perl

  • Working with databases

  • Creating simple search engines

  • Maintaining state between multiple sessions

  • Generating graphics dynamically

  • Improving performance of your CGI scripts

Table of Contents

  1. CGI Programming with Perl, 2nd Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. What’s in the Book
    2. What You Are Expected to Know Before Reading
    3. Overview of the Book
    4. Conventions in This Book
    5. How to Contact Us
    6. Acknowledgments
    7. Acknowledgments from the First Edition
  4. 1. Getting Started
    1. History
      1. How CGI Is Used Today
    2. Introduction to CGI
      1. Sample CGI
      2. Invoking CGI Scripts
    3. Alternative Technologies
      1. Why Perl?
      2. Alternatives to CGI
    4. Web Server Configuration
      1. Configuring CGI Scripts
        1. Configuring by directory
        2. Configuring by extension
  5. 2. The Hypertext Transport Protocol
    1. URLs
      1. Elements of a URL
      2. Absolute and Relative URLs
      3. URL Encoding
    2. HTTP
      1. The Request and Response Cycle
      2. HTTP Headers
      3. HTTP 1.1 and HTTP 1.0
    3. Browser Requests
      1. The Request Line
        1. PUT and DELETE
        2. GET
        3. HEAD
        4. POST
      2. Request Header Field Lines
        1. Host
        2. Content-Length
        3. Content-Type
        4. Authorization
        5. User-Agent
        6. Accept
        7. Referer
        8. Cookies
    4. Server Responses
      1. The Status Line
      2. Server Headers
        1. Content-Base
        2. Content-Length
        3. Content-Type
        4. Date
        5. ETag
        6. Last-Modified
        7. Location
        8. Server
        9. Set-Cookie
        10. WWW-Authenticate
    5. Proxies
      1. Identifying Clients
      2. Caching
    6. Content Negotiation
      1. Media Type
      2. Internationalization
      3. Encoding
    7. Summary
  6. 3. The Common Gateway Interface
    1. The CGI Environment
      1. File Handles
        1. STDIN
        2. STDOUT
        3. STDERR
    2. Environment Variables
      1. CGI Environment Variables
      2. Examining Environment Variables
    3. CGI Output
      1. Partial Headers
        1. Outputting documents
        2. Forwarding to another URL
        3. Specifying status codes
      2. Complete (Non-Parsed) Headers
    4. Examples
      1. Check the Client Browser
      2. User Authentication and Identification
      3. Restricting Image Hijacking
  7. 4. Forms and CGI
    1. Sending Data to the Server
    2. Form Tags
      1. Quick Reference to Form Tags
      2. The <FORM> Tag
      3. The <INPUT> Tag
        1. Text fields
        2. Password fields
        3. Hidden fields
        4. Checkboxes
        5. Radio buttons
        6. Submit buttons
        7. Reset buttons
        8. Image buttons
        9. Plain buttons
      4. The <SELECT> Tag
        1. The <OPTION> tag
      5. The <TEXTAREA> Tag
    3. Decoding Form Input
  8. 5. CGI.pm
    1. Overview
      1. Denial of Service Attacks
      2. The Kitchen Sink
      3. Standard and Object-Oriented Syntax
    2. Handling Input with CGI.pm
      1. Environment Information
        1. Accept
        2. http
        3. https
        4. query_string
        5. self_url
        6. url
        7. virtual_host
      2. Accessing Parameters
        1. Modifying parameters
        2. POST and the query string
        3. Index queries
        4. Supporting image buttons as submit buttons
      3. Exporting Parameters to a Namespace
      4. File Uploads with CGI.pm
    3. Generating Output with CGI.pm
      1. Controlling HTTP Headers with CGI.pm
        1. Media type
        2. Status
        3. Caching
        4. Specifying an alternative target
        5. Redirection
        6. Other headers
      2. Starting and Ending Documents
        1. start_html
        2. end_html
      3. Standard HTML Elements
      4. Form Elements
    4. Alternatives for Generating Output
      1. Lots of print Statements
      2. Here Documents
    5. Handling Errors
      1. Dying Gracefully
        1. Trapping die
      2. CGI::Carp
      3. Error Subroutines
      4. Custom Module
  9. 6. HTML Templates
    1. Reasons for Using Templates
      1. Rolling Your Own
    2. Server Side Includes
      1. Configuration
      2. Format
      3. Environment Variables
      4. Tailoring SSI Output
      5. Including Boilerplates
      6. Executing CGI Programs
      7. Common Errors
    3. HTML::Template
      1. Syntax
        1. Loops
        2. Conditionals
        3. Including other files
      2. Summary
    4. Embperl
      1. Configuration
        1. Execute
        2. mod_perl
        3. embpcgi.pl
      2. Syntax
        1. Embperl code blocks
        2. Variable scope
        3. Meta-commands
      3. HTML Logic
        1. Tables
        2. List elements
        3. Form input elements
      4. Global Variables
      5. Example
        1. Embperl handler
      6. Summary
    5. Mason
      1. A Component Approach
  10. 7. JavaScript
    1. Background
      1. History
      2. Compatibility
    2. Forms
      1. Input Validation
        1. Validating elements
        2. Validating submits
        3. Validation example
        4. Validating twice
    3. Data Exchange
      1. WDDX
      2. Example
    4. Bookmarklets
      1. Bookmarklet Basics
        1. Compatibility
      2. Bookmarklets and CGI
  11. 8. Security
    1. The Importance of Web Security
    2. Handling User Input
      1. Calling External Applications
      2. User Input and the Shell
      3. Security Strategies
      4. fork and exec
      5. Trusting the Browser
    3. Encryption
      1. MD5
      2. SHA-1
    4. Perl’s Taint Mode
      1. How Taint Works
      2. What Is Monitored by Taint Mode
      3. How Taintedness Is Removed
        1. Allowing versus disallowing
      4. Why Use Taint Mode?
      5. Common Problems with Taint Mode
    5. Data Storage
      1. Dynamic Filenames
      2. Location of Files
      3. File Permissions
    6. Summary
  12. 9. Sending Email
    1. Security
      1. Mailers and Shells
      2. False Identities
      3. Spam
    2. Email Addresses
      1. Validating Syntax
    3. Structure of Internet Email
    4. sendmail
      1. Command-Line Options
      2. Mail Queue
    5. mailx and mail
    6. Perl Mailers
    7. procmail
      1. Autoreply from nobody
      2. Forwarding to Another User
  13. 10. Data Persistence
    1. Text Files
      1. Locking
        1. flock
        2. Manual lock files
      2. Write Permissions
      3. Temporary Files
        1. Anonymous temporary files
        2. Named temporary files
      4. Delimiters
      5. DBD::CSV
    2. DBM Files
      1. DB_File
      2. MLDBM
    3. Introduction to SQL
      1. Creating a Database
      2. Inserting Data
      3. Accessing Data
      4. Updating Data
      5. Deleting Data
    4. DBI
      1. Using DBI
        1. Connecting to DBI
        2. Database manipulation
        3. Database querying
      2. DBI Address Book
        1. Address book database creation script
        2. Address book CGI script
  14. 11. Maintaining State
    1. Query Strings and Extra Path Information
    2. Hidden Fields
    3. Client-Side Cookies
      1. Setting Cookies
      2. Testing for Cookies
  15. 12. Searching the Web Server
    1. Searching One by One
    2. Searching One by One, Take Two
      1. Regex-Based Search Engine
    3. Inverted Index Search
      1. Search Application
  16. 13. Creating Graphics on the Fly
    1. File Formats
      1. GIF
        1. The LZW patent
      2. PNG
      3. JPEG
      4. PDF
    2. Outputting Image Data
      1. An Example
        1. binmode
      2. Including Dynamic Images in HTML
        1. Redundant path information
        2. Preventing caching
    3. Generating PNGs with GD
      1. Installation
      2. Using GD
    4. Additional GD Modules
      1. GD::Text
        1. GD::Text::Align
      2. GD::Graph
      3. GD::Graph3D
    5. PerlMagick
      1. Installation
        1. Requirements
        2. Enabling LZW compression
      2. Converting PNGs to GIFs or JPEGs
      3. PDF and PostScript Support
      4. Image Processing
  17. 14. Middleware and XML
    1. Communicating with Other Servers
      1. Sockets
      2. IO::Socket
      3. LWP
    2. An Introduction to XML
    3. Document Type Definition
    4. Writing an XML Parser
    5. CGI Gateway to XML Middleware
  18. 15. Debugging CGI Applications
    1. Common Errors
      1. Application Permissions
      2. The Pound-Bang
      3. Line Endings
      4. “Malformed” Header
    2. Perl Coding Techniques
      1. Use strict
      2. Check Status of System Calls
      3. Is It Open?
      4. Trap die
      5. File Locking
      6. Unbuffer Output Stream
      7. Use binmode
    3. Debugging Tools
      1. Check Syntax
      2. Check Error Logs
      3. Running Scripts from the Command Line
      4. Dumping Variables
      5. Debuggers
        1. The Perl debugger
        2. ptkdb
        3. ActiveState Perl debugger
  19. 16. Guidelines for Better CGI Applications
    1. Architectural Guidelines
      1. Plan for Future Growth
      2. Use Directories to Organize Your Projects
      3. Use Relative URLs
      4. Separate Configuration from Your Primary Code
      5. Separating Display from Your Primary Code
      6. Separating Storage from Your Primary Code
      7. Number of Scripts per Application
        1. Using one CGI program rather than many for each major application
        2. Using multiple CGI scripts for each major application
      8. Using Submit Buttons to Control Flow
    2. Coding Guidelines
  20. 17. Efficiency and Optimization
    1. Basic Perl Tips, Top Ten
      1. Benchmark Your Code
      2. Benchmark Modules, Too
      3. Localize Variables with my
      4. Avoid Slurping
      5. undef Versus ( )
      6. SelfLoader
      7. autouse
      8. Avoid the Shell
      9. Find Existing Solutions for Your Problems
      10. Regular Expressions
    2. FastCGI
      1. Installing FastCGI
    3. mod_perl
      1. Installation and Configuration
      2. mod_perl Considerations
  21. A. Works Cited and Further Reading
    1. References
    2. Additional Reading
    3. RFCs
    4. Other Specifications
    5. Project Home Pages
    6. Newsgroups
  22. B. Perl Modules
    1. CPAN
      1. Installing Modules
    2. perldoc
  23. Index
  24. About the Authors
  25. Colophon
  26. Copyright