⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>The CGI.pm Module (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch10_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h1 class="chapter">Chapter 10. The CGI.pm Module</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4>  <p> <a href="#perlnut2-CHP-10-SECT-1">HTML Tag Generation</a><br /><a href="ch10_02.htm">Importing Method Groups</a><br /><a href="ch10_03.htm">Maintaining State</a><br /><a href="ch10_04.htm">Named Parameters</a><br /><a href="ch10_05.htm">Using JavaScript Features</a><br /><a href="ch10_06.htm">Debugging</a><br /><a href="ch10_07.htm">CGI.pm Reference</a><br /></p></div><p>CGI.pm is a Perl module for creating and parsing CGI forms.<a name="INDEX-1642" /></a>Ithas been distributed with the Perl source kit since 5.004, but youcan upgrade your existing CGI.pm installation from CPAN. As of thewriting of this edition, the current version of CGI.pm is 2.81.</p><p>CGI is an object-oriented module. Don't let theobject-oriented nature scare you off, though; CGI.pm is very easy touse, as evidenced by its overwhelming popularity among all levels ofPerl programmers. To give you an idea of how easy it is to useCGI.pm, let's take a scenario in which a user fillsout and submits a form containing her birthday. Without CGI.pm, thescript would have to translate the URL-encoded input by hand(probably using a series of regular expressions) and assign it to avariable. For example, you might try an antiquated parsing methodlike this:</p><blockquote><pre class="code">#!/usr/local/bin/perl # CGI script without CGI.pm$size_of_form_info = $ENV{'CONTENT_LENGTH'};read ($STDIN, $form_info, $size_of_form_info);# Split up each pair of key/value pairsforeach $pair (split (/&amp;/, $form_info)) {   # For each pair, split into $key and $value variables   ($key, $value) = split (/=/, $pair);   # Get rid of the pesky %xx encodings   $key =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;   $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg;   # Use $key as index for $parameters hash, $value as value   $parameters{$key} = $value;}# Print out the obligatory content type lineprint "Content-type: text/plain\n\n";# Tell the user what they saidprint "Your birthday is on " . $parameters{birthday} . ".\n";</pre></blockquote><p>Regardless of whether this code actually works, you must admitit's ugly, and completely ignores important detailssuch as dealing with tainted input. With CGI.pm, the script could bewritten as:</p><blockquote><pre class="code">#!/usr/local/bin/perl -w# CGI script with CGI.pmuse CGI;my $query = CGI-&gt;new( );my $bday = $query-&gt;param("birthday");print $query-&gt;header(-type =&gt; 'text/plain');print $query-&gt;p("Your birthday is $bday.");</pre></blockquote><p>Even for this tiny program, you can see that CGI.pm can alleviatemany of the headaches associated with CGI programming.</p><p>As with any Perl module, the first thing you do is call the modulewith <tt class="literal">use</tt>. You then call the constructor(<tt class="literal">new( )</tt>), creating a new CGI object called<tt class="literal">$query</tt>. Next, get the value of the<tt class="literal">birthday</tt> parameter <a name="INDEX-1643" /></a>from theCGI program using the <tt class="literal">param</tt> method. Note thatCGI.pm does all the work of determining whether the CGI program iscalled by the GET or POST methods, and it does all the URL decodingfor you. To generate output, use the <tt class="literal">header</tt> methodto return the content type header and the <tt class="literal">p</tt> methodto generate a paragraph marker <tt class="literal">&lt;P&gt;</tt> tag.</p><p>However, this is only the tip of the iceberg as far as what CGI.pmcan do for you. There are three basic categories of CGI.pm methods:CGI handling, creating forms, and retrieving environment variables.(A fourth category is creating HTML tags, but wedon't cover those in detail.) <a href="ch10_01.htm#perlnut2-CHP-10-TABLE-1">Table 10-1</a> lists most of these methods. They are alsocovered in more detail later in this chapter.</p><a name="perlnut2-CHP-10-TABLE-1" /></a><h4 class="objtitle">Table 10-1. CGI.pm methods </h4><table border="1" cellpadding="3"><tr><th><p>Method</p></th><th><p>Description</p></th></tr><tr><td><p><b class="emphasis-bold">CGI handling</b></p></td></tr><tr><td><p><tt class="literal">keywords</tt></p></td><td><p>Gets keywords from an <tt class="literal">&lt;ISINDEX&gt;</tt> search</p></td></tr><tr><td><p><tt class="literal">param</tt></p></td><td><p>Gets (or sets) the value of parameters</p></td></tr><tr><td><p><tt class="literal">append</tt></p></td><td><p>Appends to a parameter</p></td></tr><tr><td><p><tt class="literal">import_names</tt></p></td><td><p>Imports variables into a namespace</p></td></tr><tr><td><p><tt class="literal">delete</tt></p></td><td><p>Deletes a parameter</p></td></tr><tr><td><p><tt class="literal">delete_all</tt></p></td><td><p>Deletes all parameters</p></td></tr><tr><td><p><tt class="literal">save</tt></p></td><td><p>Saves all parameters to a file</p></td></tr><tr><td><p><tt class="literal">self_url</tt></p></td><td><p>Creates self-referencing URL</p></td></tr><tr><td><p><tt class="literal">url</tt></p></td><td><p>Gets URL of current script without query information</p></td></tr><tr><td><p><tt class="literal">header</tt></p></td><td><p>Creates HTTP header</p></td></tr><tr><td><p><tt class="literal">redirect</tt></p></td><td><p>Creates redirection header</p></td></tr><tr><td><p><tt class="literal">cookie</tt></p></td><td><p>Gets (or sets) a cookie</p></td></tr><tr><td><p><tt class="literal">nph</tt></p></td><td><p>Declares this to be a NPH script</p></td></tr><tr><td><p><tt class="literal">dump</tt></p></td><td><p>Prints all name/value pairs</p></td></tr><tr><td><p><b class="emphasis-bold">Form generation</b></p></td></tr><tr><td><p><tt class="literal">start_html</tt></p></td><td><p>Generates an <tt class="literal">&lt;HTML&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">end_html</tt></p></td><td><p>Generates an <tt class="literal">&lt;/HTML&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">autoEscape</tt></p></td><td><p>Sets whether to use automatic escaping</p></td></tr><tr><td><p><tt class="literal">isindex</tt></p></td><td><p>Generates an <tt class="literal">&lt;ISINDEX&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">startform</tt></p></td><td><p>Generates a <tt class="literal">&lt;FORM&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">start_multipart_form</tt></p></td><td><p>Generates a <tt class="literal">&lt;FORM&gt;</tt> tag for<tt class="literal">multipart/form-data</tt> encoding</p></td></tr><tr><td><p><tt class="literal">textfield</tt></p></td><td><p>Generates an <tt class="literal">&lt;INPUT TYPE=TEXT&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">textarea</tt></p></td><td><p>Generates an <tt class="literal">&lt;TEXTAREA&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">password_field</tt></p></td><td><p>Generates an <tt class="literal">&lt;INPUT TYPE=PASSWORD&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">filefield</tt></p></td><td><p>Generates an <tt class="literal">&lt;INPUT TYPE=FILE&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">popup_menu</tt></p></td><td><p>Generates a pop-up menu via <tt class="literal">&lt;SELECT SIZE=1&gt;</tt>and <tt class="literal">&lt;OPTION&gt;</tt> tags</p></td></tr><tr><td><p><tt class="literal">scrolling_list</tt></p></td><td><p>Generates a scrolling list via <tt class="literal">&lt;SELECT&gt;</tt> and<tt class="literal">&lt;OPTION&gt;</tt> tags</p></td></tr><tr><td><p><tt class="literal">checkbox_group</tt></p></td><td><p>Generates a group of checkboxes via multiple <tt class="literal">&lt;INPUTTYPE=CHECKBOX&gt;</tt> tags</p></td></tr><tr><td><p><tt class="literal">checkbox</tt></p></td><td><p>Generates a single checkbox via an <tt class="literal">&lt;INPUTTYPE=CHECKBOX&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">radio_group</tt></p></td><td><p>Generates a group of radio buttons via <tt class="literal">&lt;INPUTTYPE=RADIO&gt;</tt> tags</p></td></tr><tr><td><p><tt class="literal">submit</tt></p></td><td><p>Generates a <tt class="literal">&lt;SUBMIT&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">reset</tt></p></td><td><p>Generates a <tt class="literal">&lt;RESET&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">defaults</tt></p></td><td><p>Generates a <tt class="literal">&lt;DEFAULTS&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">hidden</tt></p></td><td><p>Generates an <tt class="literal">&lt;INPUT TYPE=HIDDEN&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">image_button</tt></p></td><td><p>Generates a clickable image button via a<tt class="literal">&lt;SELECT&gt;</tt> tag</p></td></tr><tr><td><p><tt class="literal">button</tt></p></td><td><p>Generates a JavaScript button</p></td></tr><tr><td><p><b class="emphasis-bold">Handling environment variables</b></p></td></tr><tr><td><p><tt class="literal">accept</tt></p></td><td><p>Gets accept types from ACCEPT header</p></td></tr><tr><td><p><tt class="literal">user_agent</tt></p></td><td><p>Gets value of USER_AGENT header</p></td></tr><tr><td><p><tt class="literal">path_info</tt></p></td><td><p>Gets value of EXTRA_PATH_INFO header</p></td></tr><tr><td><p><tt class="literal">path_translated</tt></p></td><td><p>Gets value of PATH_TRANSLATED header</p></td></tr><tr><td><p><tt class="literal">remote_host</tt></p></td><td><p>Gets value of REMOTE_HOST header</p></td></tr><tr><td><p><tt class="literal">raw_cookie</tt></p></td><td><p>Gets value of HTTP_COOKIE header</p></td></tr><tr><td><p><tt class="literal">script_name</tt></p></td><td><p>Gets value of SCRIPT_NAME header</p></td></tr><tr><td><p><tt class="literal">referer</tt></p></td><td><p>Gets value of REFERER header</p></td></tr><tr><td><p><tt class="literal">auth_type</tt></p></td><td><p>Gets value of AUTH_TYPE header</p></td></tr><tr><td><p><tt class="literal">remote_user</tt></p></td><td><p>Gets value of REMOTE_USER header</p></td></tr><tr><td><p><tt class="literal">user_name</tt></p></td><td><p>Gets user name (not via headers)</p></td></tr><tr><td><p><tt class="literal">request_method</tt></p></td><td><p>Gets value of REQUEST_METHOD header</p></td></tr></table><p><div class="sect1"><a name="perlnut2-CHP-10-SECT-1" /></a><h2 class="sect1">10.1. HTML Tag Generation</h2><p>In addition to the form-generation methods, CGI.pm<a name="INDEX-1644" /></a><a name="INDEX-1645" /></a>includes a group of methods for creatingHTML tags. The names of the HTML tag methods generally follow theHTML tag name (e.g., <tt class="literal">p</tt> for<tt class="literal">&lt;P&gt;</tt>) and take named parameters that areassumed to be valid attributes for the tag (e.g.,<tt class="literal">img(src=&gt;'camel.gif')</tt> becomes <tt class="literal">&lt;IMGSRC="camel.gif"&gt;</tt>). We do not list all tags in this book;see the CGI.pm manpage for more information, or the book<em class="emphasis">Official Guide to Programming with CGI.pm</em> byLincoln Stein (John Wiley &amp; Sons, 1998).</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch10_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">9.4. CGI Environment Variables</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">10.2. Importing Method Groups</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm">      </map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -