Perl Tutorials - Herong's Tutorial Examples - v6.01, by Dr. Herong Yang
Retrieving Query Parameters and Headers
This section provides a tutorial example on how to retrieve input parameters from query string and request content, and retrieve request headers.
Next, let's play with those methods that retrieve input parameters from query string including request content, and request headers. Here is a simple example Perl CGI script: CGI-pm-Request-Info.pl:
#!c:/local/perl/bin/perl.exe #- CGI-pm-Request-Info.pl #- Copyright (c) HerongYang.com. All Rights Reserved. use CGI; $query = CGI->new(); $text = ""; #- Getting the request method $text .= "Request method = ".$query->request_method()."\n"; #- Getting input data from the query string and from the data content $text .= "Names and values from param():\n"; @names = $query->param(); foreach $name (@names) { $text .= " $name = ".$query->param($name)."\n"; } #- Getting request headers $text .= "Names and values from http():\n"; @names = $query->http(); foreach $name (@names) { $text .= " $name = ".$query->http($name)."\n"; } print $query->header(); print $query->start_html(-title=>'CGI-pm-Request-Info.pl'); print $query->pre($text); print $query->end_html();
Copy CGI-pm-Request-Info.pl to C:\local\apache\htdocs and browse to: http://localhost/CGI-pm-Request-Info.pl?name=joe&age=21&student
You should see the following result in the browser. The result looks good.
Request method = GET Names and values from param(): name = joe age = 21 student = Names and values from https(): HTTP_ACCEPT_ENCODING = gzip, deflate HTTP_CONNECTION = keep-alive HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q... HTTP_HOST = localhost HTTP_DNT = 1 HTTP_ACCEPT_LANGUAGE = en-US,en;q=0.5 HTTP_USER_AGENT = Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/2...
Table of Contents
Data Types: Values and Variables
Expressions, Operations and Simple Statements
Name Spaces and Perl Module Files
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 Directories and Read File Names
File System Functions and Operations
Socket Communication Over the Internet
XML::Simple Module - XML Parser and Generator
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
Generating HTML Document with CGI.pm
►Retrieving Query Parameters and Headers
Retrieving Environment and Script Information
Retrieving and Setting HTTP Cookies
LWP::UserAgent and Web Site Testing
Converting Perl Script to Executable Binary