📄 ch05_02.htm
字号:
<?label 5.2. Handling Input with CGI.pm?><html><head><title>Handling Input with CGI.pm (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch05_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm">CGI Programming with Perl</a></td><td width="172" valign="top" align="right"><a href="ch05_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><hr align="left" width="515" /><h2 class="sect1">5.2. Handling Input with CGI.pm</h2><p><a name="INDEX-973" /> <a name="INDEX-974" /> <a name="INDEX-975" />CGI.pm primarily handlestwo separate tasks: it reads and parses input from the user, and itprovides a convenient way to return HTML output. Let's firstlook at how it collects input.</p><a name="ch05-2-fm2xml" /><div class="sect2"><h3 class="sect2">5.2.1. Environment Information</h3><p>CGI.pm provides many<a name="INDEX-976" /> <a name="INDEX-977" />methods to get information about yourenvironment. Of course, when you use CGI.pm, all of your standard CGIenvironment variables are still available in Perl's<tt class="literal">%ENV</tt> hash, but CGI.pm also makes most of theseavailable via method calls. It also provides some unique methods.<a href="ch05_02.htm#ch05-65059">Table 5-1</a> shows how CGI.pm's functionscorrespond to the standard CGI environment variables.</p><a name="ch05-65059" /><h4 class="objtitle">Table 5-1. CGI.pm Environment Methods and CGI Environment Variables </h4><table border="1"><tr><th><p>CGI.pm Method</p></th><th><p>CGI Environment Variable</p></th></tr><tr><td><p><tt class="function">auth_type</tt></p></td><td><p>AUTH_TYPE</p></td></tr><tr><td><p>Not available</p></td><td><p>CONTENT_LENGTH</p></td></tr><tr><td><p><tt class="function">content_type</tt></p></td><td><p>CONTENT_TYPE</p></td></tr><tr><td><p>Not available</p></td><td><p>DOCUMENT_ROOT</p></td></tr><tr><td><p>Not available</p></td><td><p>GATEWAY_INTERFACE</p></td></tr><tr><td><p><tt class="function">path_info</tt></p></td><td><p>PATH_INFO</p></td></tr><tr><td><p><tt class="function">path_translated</tt></p></td><td><p>PATH_TRANSLATED</p></td></tr><tr><td><p><tt class="function">query_string</tt></p></td><td><p>QUERY_STRING</p></td></tr><tr><td><p><tt class="function">remote_addr</tt></p></td><td><p>REMOTE_ADDR</p></td></tr><tr><td><p><tt class="function">remote_host</tt></p></td><td><p>REMOTE_HOST</p></td></tr><tr><td><p><tt class="function">remote_ident</tt></p></td><td><p>REMOTE_IDENT</p></td></tr><tr><td><p><tt class="function">remote_user</tt></p></td><td><p>REMOTE_USER</p></td></tr><tr><td><p><tt class="function">request_method</tt></p></td><td><p>REQUEST_METHOD</p></td></tr><tr><td><p><tt class="function">script_name</tt></p></td><td><p>SCRIPT_NAME</p></td></tr><tr><td><p><tt class="function">self_url</tt></p></td><td><p>Not available</p></td></tr><tr><td><p><tt class="function">server_name</tt></p></td><td><p>SERVER_NAME</p></td></tr><tr><td><p><tt class="function">server_port</tt></p></td><td><p>SERVER_PORT</p></td></tr><tr><td><p><tt class="function">server_protocol</tt></p></td><td><p>SERVER_PROTOCOL</p></td></tr><tr><td><p><tt class="function">server_software</tt></p></td><td><p>SERVER_SOFTWARE</p></td></tr><tr><td><p><tt class="function">url</tt></p></td><td><p>Not available</p></td></tr><tr><td><p><tt class="function">Accept</tt></p></td><td><p>HTTP_ACCEPT</p></td></tr><tr><td><p><tt class="function">http("Accept-charset")</tt></p></td><td><p>HTTP_ACCEPT_CHARSET</p></td></tr><tr><td><p><tt class="function">http("Accept-encoding")</tt></p></td><td><p>HTTP_ACCEPT_ENCODING</p></td></tr><tr><td><p><tt class="function">http("Accept-language")</tt></p></td><td><p>HTTP_ACCEPT_LANGUAGE</p></td></tr><tr><td><p><tt class="function">raw_cookie</tt></p></td><td><p>HTTP_COOKIE</p></td></tr><tr><td><p><tt class="function">http("From")</tt></p></td><td><p>HTTP_FROM</p></td></tr><tr><td><p><tt class="function">virtual_host</tt></p></td><td><p>HTTP_HOST</p></td></tr><tr><td><p><tt class="function">referer</tt></p></td><td><p>HTTP_REFERER</p></td></tr><tr><td><p><tt class="function">user_agent</tt></p></td><td><p>HTTP_USER_AGENT</p></td></tr><tr><td><p><tt class="function">https</tt></p></td><td><p>HTTPS</p></td></tr><tr><td><p><tt class="function">https("Cipher")</tt></p></td><td><p>HTTPS_CIPHER</p></td></tr><tr><td><p><tt class="function">https("Keysize")</tt></p></td><td><p>HTTPS_KEYSIZE</p></td></tr><tr><td><p><tt class="function">https("SecretKeySize")</tt></p></td><td><p>HTTPS_SECRETKEYSIZE</p></td></tr></table><p>Most of these CGI.pm methods take no arguments and return that samevalue as the corresponding environment variable. For example, to getthe additional path information passed to your CGI script, you canuse the following method:</p><blockquote><pre class="code">my $path = $q->path_info;</pre></blockquote><p>This is the same information that you could also get this way:</p><blockquote><pre class="code">my $path = $ENV{PATH_INFO};</pre></blockquote><p>However, a few methods differ or have features worth noting.Let's take a look at these.</p><a name="ch05-3-fm2xml" /><div class="sect3"><h3 class="sect3">5.2.1.1. Accept</h3><p>As a <a name="INDEX-978" />general rule, if a CGI.pm method hasthe same name as a built-in Perl function or keyword (e.g.,<tt class="function">accept</tt> or <tt class="function">tr</tt>), then theCGI.pm method is<a name="INDEX-979" />capitalized. Although there would be nocollision if CGI.pm were available only via an object-orientedsyntax, the collision creates problem for people who use it via thestandard syntax. <tt class="function">accept</tt> was originallylowercase, but it was renamed to <tt class="function">Accept</tt> inversion 2.44 of CGI.pm, and the new name affects both syntaxes.</p><p>Unlike the other methods that take no arguments and simply return avalue, <tt class="function">Accept</tt> can also be given a<a name="INDEX-980" />content type and it willevaluate to true or false depending on whether that content type isacceptable according to the<em class="emphasis">HTTP-Accept</em><a name="INDEX-981" /> header:</p><blockquote><pre class="code">if ( $q->Accept( "image/png" ) ) { . . .</pre></blockquote><p>Keep in mind that most browsers today send<tt class="literal">*/*</tt><a name="INDEX-982" /> in their <em class="emphasis">Accept</em>header. This matches anything, so using the<tt class="function">Accept</tt> method in this manner is not especiallyuseful. For new file formats like <tt class="literal">image/png</tt>, it isbest to get the values for the HTTP header and perform the checkyourself, ignoring wildcard matches (this is unfortunate, since itdefeats the purpose of wildcards):</p><blockquote><pre class="code">my @accept = $q->Accept;if ( grep $_ eq "image/png", @accept ) { . . .</pre></blockquote></div><a name="ch05-4-fm2xml" /><div class="sect3"><h3 class="sect3">5.2.1.2. http</h3><p>If the <tt class="function">http</tt><a name="INDEX-983" /> <a name="INDEX-984" /> method is called without arguments, itreturns the name of the environment variables available that containan HTTP_ prefix. If you call <tt class="function">http</tt> with anargument, then it will return the value of the corresponding HTTP_environment variable. When passing an argument to<tt class="function">http</tt>, the HTTP_ prefix is optional,capitalization does not matter, and hyphens and underscores areinterpreted the same. In other words, you can pass the actual HTTPheader field name or the environment variable name or even somehybrid of the two, and <tt class="function">http</tt> will generallyfigure it out. Here is how you can display all the HTTP_ environmentvariables your CGI script receives:</p><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use CGI;my $q = new CGI;print $q->header( "text/plain" );print "These are the HTTP environment variables I received:\n\n";foreach ( $q->http ) { print "$_:\n"; print " ", $q->http( $_ ), "\n";}</pre></blockquote></div><a name="ch05-5-fm2xml" /><div class="sect3"><h3 class="sect3">5.2.1.3. https</h3><p>The<tt class="function">https</tt><a name="INDEX-985" /> method functionssimilarly to the <tt class="function">http</tt> method when it is passed aparameter. It returns the corresponding
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -