Perl Tutorials - Herong's Tutorial Examples
∟CGI.pm Module for Building Web Pages
∟What Is CGI.pm?
This section describes what is CGI.pm - a Perl module that helps you to process HTTP requests and prepare HTTP responses through the CGI (Common Gateway Interface) standard.
What Is CGI.pm?
CGI.pm, developed by Lincoln D. Stein, is a Perl module that helps you
to process HTTP requests and prepare HTTP responses through the CGI (Common Gateway Interface) standard.
The current version of CGI.pm included in Perl 5.18 support a large number of properties and methods,
which can be divided into these categories:
1. Constructing Query Objects - Methods to construct query (CGI) object to represent HTTP requests.
- $q = CGI->new() - Create a query (CGI) object that represents the HTTP request provided by the CGI interface.
- $q = CGI->new(INPUTFILE) - Create a query (CGI) object that represents a HTTP request described by the specified input file.
This constructor is usually used for debugging purpose.
2. Managing Parameters from Request - Methods to manage parameters provided in the HTTP request.
- @l = $q->param() - Return parameter names as an array from the current query object.
- $v = $q->param{$n} - Return the parameter value of the specified parameter name from the current query object.
- $q->param{$n, $v} - Add a new parameter to the current query object.
- $q->delete{$n} - Delete a parameter from the current query object.
3. Retrieving Headers from Request - Methods to retrieve header line information from the HTTP request.
- @l = $q->http() - Return header names as an array from the HTTP request
- $v = $q->http($n) - Return the header value of the given header name from the HTTP request.
- $v = $q->accept() - Return the "Accept:" header value from the HTTP request.
- $v = $q->user_agent() - Return the "User-Agent:" header value from the HTTP request.
- $v = $q->content_type() - Return the "Content-Type:" header value from the HTTP request.
- $v = $q->request_method() - Return the method of the HTTP request: GET, POST, ...
- $v = $q->raw_cookie() - Return cookies from the HTTP request in raw format.
- $v = $q->referer() - Return the "Referer:" header value from the HTTP request.
- ...
4. Retrieving Server information - Methods to retrieve environment information from the Web server.
- $v = $q->path_info() - Return the path name from the URI that invokes this CGI script.
- $v = $q->path_translated() - Return the physical path name of this CGI script file.
- $v = $q->script_name() - Return the file name of this CGI script file.
- $v = $q->remote_host() - Return the remote host name.
- $v = $q->remote_addr() - Return the remote host IP address.
- $v = $q->server_name() - Return the host name of the Web server.
- $v = $q->server_port() - Return the port number used by the Web server.
- $v = $q->url() - Return the current URI without query string.
- $v = $q->url(-query=>1) - Return the current URI with query string.
- ...
5. Setting Headers to Response - Methods to create header lines ready to be printed to the HTTP response.
- print $q->header() - Print the default header line "Content-Type: text/html; charset=ISO-8859-1".
Note that header() should only be called once.
- print $q->header('image/gif') - Print the default header line with "image/gif" as the content type.
- print $q->header(-status=>'402 Payment required', -type=>'image/gif', -charset=>'utf8', ...) -
Print header lines with give header names and values.
- print $q->redirect($uri); - Print a redirect header to the HTTP response with the default redirect status "302 Found",
meaning moved temporarily.
- print $q->redirect(-uri=>$uri, -status=>$s); - Print a redirect header to the HTTP response.
6. Building Response Content - Methods to build HTML elements ready to be printed to the HTTP response.
- print $q->start_html() - Print default <html>, <head> and <body> elements.
- print $q->start_html(-title=>'...', -meta=>'...', -style=>'...', ...) - Print <html>, <head> and <body> elements
with sub elements like <title>, <meta>, <style>, ...
- print $q->end_html() - Print default ending tags: </html> and </html> .
- print $q->h1('...') - Print a <h1> element.
- print $q->p('...') - Print a <p> element.
- print $q->pre('...') - Print a <pre> element.
- print $q->hr() - Print a <hr> element.
- print $q->a(-href=>'...', '...') - Print a <a> element.
- print $q->img(-src=>'...') - Print a <img> element.
- ...
Table of Contents
About This Book
Perl on Linux Systems
ActivePerl on Windows Systems
Data Types: Values and Variables
Expressions, Operations and Simple Statements
User Defined Subroutines
Perl Built-in Debugger
Name Spaces and Perl Module Files
Symbolic (or Soft) References
Hard References - Addresses of Memory Objects
Objects (or References) and Classes (or Packages)
Typeglob and Importing Identifiers from Other Packages
String Built-in Functions and Performance
File Handles and Data Input/Output
Open Files in Binary Mode
Open Directories and Read File Names
File System Functions and Operations
Image and Picture Processing
Using DBM Database Files
Using MySQL Database Server
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
XML Communication Model
SOAP::Lite - SOAP Server-Client Communication Module
Perl Programs as IIS Server CGI Scripts
CGI (Common Gateway Interface)
XML-RPC - Remote Procedure Call with XML and HTTP
RPC::XML - Perl Implementation of XML-RPC
Integrating Perl with Apache Web Server
►CGI.pm Module for Building Web Pages
►What Is CGI.pm?
Generating HTML Document with CGI.pm
Retrieving Query Parameters and Headers
Retrieving Environment and Script Information
Redirecting Browser to a URI
"Refresh" http-equiv Meta Tag
What Is CGI::Cookie?
Retrieving and Setting HTTP Cookies
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary
Managing Perl Engine and Modules on macOS
Archived Tutorials
References
Full Version in PDF/EPUB