📄 ch05_03.htm
字号:
<p>This usage makes it easy to nest elements:</p><blockquote><pre class="code">print $q->p( "The server name is:", $q->em( $q->server_name ) );</pre></blockquote><p>This prints the text:</p><blockquote><pre class="code"><P>The server name is: <EM>localhost</EM></P></pre></blockquote><p>Note that a space is automatically added between each<a name="INDEX-1148" /><a name="INDEX-1149" />list element. It appears after the colonin these examples. If you wish to print multiple items in a listwithout intervening<a name="INDEX-1150" />spaces, then youmust set Perl's list separator variable, <tt class="literal">$"</tt>,to an empty string:</p><blockquote><pre class="code">{ local $" = ""; print $q->p( "Server=", $q->server_name );}</pre></blockquote><p>This prints the text:</p><blockquote><pre class="code"><P>Server=Apache/1.3.9</P></pre></blockquote><p>Note that whenever you change global variables like<tt class="literal">$"</tt>, you should localize them by enclosing them inblocks and using <a name="INDEX-1151" /> <a name="INDEX-1,152" />Perl's<tt class="function">local</tt> function.</p></li><li><p>If the first argument is a reference to a hash, then the<a name="INDEX-1153" />hash elementsare interpreted as<a name="INDEX-1154" />attributes forthe HTML element:</p><blockquote><pre class="code">print $q->a( { -href => "/downloads" }, "Download Area" );</pre></blockquote><p>This prints the text:</p><blockquote><pre class="code"><A HREF="/downloads" >Download Area</A></pre></blockquote><p>You can specify as many attributes as you want. The leading<a name="INDEX-1155" />hyphen as part of the attribute name isnot required, but it is the standard convention.</p><p>Someattributes do not take arguments and simply appear as a word. Forthese, pass <tt class="literal">undef</tt> as the value of the attribute.Prior to version 2.41 of CGI.pm, passing an empty string wouldaccomplish the same thing, but that was changed so that people couldexplicitly request an attribute set to an empty string (e.g., <IMGHREF="spacer.gif" ALT="">).</p></li><li><p>Ifyou provide a reference to an<a name="INDEX-1156" />array as an argument, the tag isdistributed across each item in the array:</p><blockquote><pre class="code">print $q->ol( $q->li( [ "First", "Second", "Third" ] ) );</pre></blockquote><p>This corresponds to:</p><blockquote><pre class="code"><OL> <LI>First</LI> <LI>Second</LI> <LI>Third</LI></OL></pre></blockquote><p>This still works fine when the first argument is a reference toa hash arguments. Here is a table:</p><blockquote><pre class="code">print $q->table( { -border => 1, -width => "100%" }, $q->Tr( [ $q->th( { -bgcolor => "#cccccc" }, [ "Name", "Age" ] ), $q->td( [ "Mary", 29 ] ), $q->td( [ "Bill", 27 ] ), $q->td( [ "Sue", 26 ] ) ] ) );</pre></blockquote><p>This corresponds to:</p><blockquote><pre class="code"><TABLE BORDER="1" WIDTH="100%"> <TR> <TH BGCOLOR="#cccccc">Name</TH> <TH BGCOLOR="#cccccc">Age</TH> </TR> <TR> <TD>Mary</TD> <TD>29</TD> </TR> <TR> <TD>Bill</TD> <TD>27</TD> </TR> <TR> <TD>Sue</TD> <TD>26</TD> </TR></TABLE></pre></blockquote></li><li><p>Aside from the <a name="INDEX-1157" />spaces we mentioned above that areintroduced between array elements, CGI.pm does not insert anywhitespace between HTML elements. It creates no indentation andinserts no new lines. Although this makes it harder for a human toread, it also makes the output smaller and downloads faster. If youwish to generate neatly formatted HTML code, you can use the<a name="INDEX-1158" /><a name="INDEX-1159" />CGI::Pretty module distributed withCGI.pm. It provides all of the features of CGI.pm (because it is anobject-oriented module that extends CGI.pm), but the HTML it producesis <a name="INDEX-1160" />neatlyindented.</p></li></ul></div><a name="ch05-24-fm2xml" /><div class="sect2"><h3 class="sect2">5.3.4. Form Elements</h3><p>The syntax for generating<a name="INDEX-1161" /><a name="INDEX-1162" />form elements differs from otherelements. These methods only take name-value pairs that correspond tothe attributes. See <a href="ch05_03.htm#ch05-22489">Table 5-2</a>.</p><a name="ch05-22489" /><h4 class="objtitle">Table 5-2. CGI.pm Methods for HTML Form Elements </h4><table border="1"><tr><th><p>CGI.pm Method</p></th><th><p>HTML Tag</p></th></tr><tr><td><p><em class="emphasis">start_form</em></p></td><td><p><FORM></p></td></tr><tr><td><p><em class="emphasis">end_form</em></p></td><td><p></FORM></p></td></tr><tr><td><p><em class="emphasis">textfield</em></p></td><td><p><INPUT TYPE="TEXT" ></p></td></tr><tr><td><p><em class="emphasis">password_field</em></p></td><td><p><INPUT TYPE="PASSWORD" ></p></td></tr><tr><td><p><em class="emphasis">filefield</em></p></td><td><p><INPUT TYPE="FILE" ></p></td></tr><tr><td><p><em class="emphasis">button</em></p></td><td><p><INPUT TYPE="BUTTON" ></p></td></tr><tr><td><p><em class="emphasis">submit</em></p></td><td><p><INPUT TYPE="SUBMIT" ></p></td></tr><tr><td><p><em class="emphasis">reset</em></p></td><td><p><INPUT TYPE="RESET" ></p></td></tr><tr><td><p><em class="emphasis">checkbox, checkbox_group</em></p></td><td><p><INPUT TYPE="CHECKBOX" ></p></td></tr><tr><td><p><em class="emphasis">radio_group</em></p></td><td><p><INPUT TYPE="RADIO" ></p></td></tr><tr><td><p><em class="emphasis">popup_menu</em></p></td><td><p><SELECT SIZE="1" ></p></td></tr><tr><td><p><em class="emphasis">scrolling_list</em></p></td><td><p><SELECT SIZE="n" > where n > 1</p></td></tr><tr><td><p><em class="emphasis">textarea</em></p></td><td><p><TEXTAREA></p></td></tr><tr><td><p><em class="emphasis">hidden</em></p></td><td><p><INPUT TYPE="HIDDEN" ></p></td></tr></table><p>The <tt class="function">start_form</tt> and <tt class="function">end_form</tt>elements generate the opening and closing form tags.<tt class="function">start_form</tt> takes arguments for each of itsattributes:</p><blockquote><pre class="code">print $q->start_form( method => "get", action => "/cgi/myscript.cgi" );</pre></blockquote><p>Note that unlike a typical form tag, CGI.pm sets the request methodto POST instead of GET by default (the reverse of the default forHTML forms). If you want to allow file uploads, use the<tt class="function">start_multipart_form</tt> method instead of<tt class="function">start_form</tt>, which sets <em class="emphasis">enctype</em> to"multipart/form-data".</p><p>All of the remaining methods create form elements. They all take the<tt class="literal">-name</tt> and <tt class="literal">-default</tt> arguments.The <tt class="literal">-default</tt> value for an element is replaced bythe corresponding value from <tt class="function">param</tt> if that valueexists. You can disable this and force the default to override auser's parameters by passing the <tt class="literal">-override</tt>argument with a true value.</p><p>The <tt class="literal">-default</tt> option specifies the default<em class="emphasis">value</em> of the element for elements with singlevalues:</p><blockquote><pre class="code">print $q->textfield( -name => "username", -default => "Anonymous" );</pre></blockquote><p>This yields:</p><blockquote><pre class="code"><INPUT TYPE="text" NAME="username" VALUE="Anonymous"></pre></blockquote><p>By supplying an array with the <tt class="literal">-values</tt> argument,the <tt class="function">checkbox_group</tt> and<tt class="function">radio_group</tt> methods generate multiple checkboxesthat share the same name. Likewise, passing an array reference withthe <tt class="literal">-values</tt> argument to the<tt class="function">scrolling_list</tt> and<tt class="function">popup_menu</tt> functions generates both the<SELECT> and <OPTION> elements. For these elements,<tt class="literal">-default</tt> indicates the values that are checked orselected; you can pass <tt class="literal">-default</tt> a reference to anarray for <tt class="function">checkbox_group</tt> and<tt class="function">scrolling_list</tt> for multiple defaults.</p><p>Each method accepts a <tt class="literal">-labels</tt> argument that takesa reference to a hash; this hash associates the value of each elementto the label the browser displays to the user.</p><p>Here is how you can generate a group of radio buttons:</p><blockquote><pre class="code">print $q->radio_group( -name => "curtain", -values => [ "A", "B", "C" ], -default => "B", -labels => { A => "Curtain A", B => "Curtain B", C => "Curtain C" } );</pre></blockquote><p>This yields:</p><blockquote><pre class="code"><INPUT TYPE="radio" NAME="look_behind" VALUE="A">Curtain A<INPUT TYPE="radio" NAME="look_behind" VALUE="B" CHECKED>Curtain B<INPUT TYPE="radio" NAME="look_behind" VALUE="C">Curtain C</pre></blockquote><p>For specifying any other attributes for form elements, like SIZE=4,pass them as additional <a name="INDEX-1164" /> <a name="INDEX-1,165" />arguments (e.g., <tt class="literal">size =>4</tt>).</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch05_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch05_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">5.2. Handling Input with CGI.pm</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">5.4. Alternatives for Generating Output</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -