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

📄 ch19_08.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 19.7. Formatting Lists and Tables with HTML Shortcuts (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:36Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"><LINKREL="prev"HREF="ch19_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes"><LINKREL="next"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.6. Executing Commands Without Shell Escapes"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch19_01.htm"TITLE="19. CGI Programming"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.8. Redirecting to a Different Location"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-12318">19.7. Formatting Lists and Tables with HTML Shortcuts</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-642">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005454-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005454-1"></A></A></H3><PCLASS="para">You have several lists and tables to generate and would like helper functions to make these easier to output.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-648">Solution</A></H3><PCLASS="para">The CGI module provides HTML helper functions which, when passed array references, apply themselves to each element of the referenced array:</P><PRECLASS="programlisting">print ol( li([ qw(red blue green)]) );<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;OL&gt;&lt;LI&gt;red&lt;/LI&gt; &lt;LI&gt;blue&lt;/LI&gt; &lt;LI&gt;green&lt;/LI&gt;&lt;/OL&gt;</I></CODE></B></CODE>@names = qw(Larry Moe Curly);print ul( li({ -TYPE =&gt; &quot;disc&quot; }, \@names) );<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;UL&gt;&lt;LI TYPE=&quot;disc&quot;&gt;Larry&lt;/LI&gt; &lt;LI TYPE=&quot;disc&quot;&gt;Moe&lt;/LI&gt; </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;LI TYPE=&quot;disc&quot;&gt;Curly&lt;/LI&gt;&lt;/UL&gt;</I></CODE></B></CODE></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-664">Discussion</A></H3><PCLASS="para">The distributive behavior of the HTML generating functions in CGI.pm can significantly simplify generation of lists and tables. Passed a simple string, they just produce HTML for that string. But passed an array reference, they work on all those strings.</P><PRECLASS="programlisting">print li(&quot;alpha&quot;);<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;LI&gt;alpha&lt;/LI&gt;</I></CODE></B></CODE>print li( [ &quot;alpha&quot;, &quot;omega&quot;] );<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;LI&gt;alpha&lt;/LI&gt; &lt;LI&gt;omega&lt;/LI&gt;</I></CODE></B></CODE></PRE><PCLASS="para">The shortcut functions for lists will be loaded when you use the <CODECLASS="literal">:standard</CODE> import tag, but you need to ask for <CODECLASS="literal">:html3</CODE> explicitly to get helper functions for working with tables. There's also a conflict between the <CODECLASS="literal">&lt;TR&gt;</CODE> tag, which would normally make a <CODECLASS="literal">tr()</CODE> function, and Perl's built-in <CODECLASS="literal">tr///</CODE> operator. Therefore, to make a table row, use the <CODECLASS="literal">Tr()</CODE> function.</P><PCLASS="para">This example generates an HTML table starting with a hash of arrays. The keys will be the row headers, and the array of values will be the columns.</P><PRECLASS="programlisting">use CGI qw(:standard :html3);%hash = (    &quot;Wisconsin&quot;  =&gt; [ &quot;Superior&quot;, &quot;Lake Geneva&quot;, &quot;Madison&quot; ],    &quot;Colorado&quot;   =&gt; [ &quot;Denver&quot;, &quot;Fort Collins&quot;, &quot;Boulder&quot; ],    &quot;Texas&quot;      =&gt; [ &quot;Plano&quot;, &quot;Austin&quot;, &quot;Fort Stockton&quot; ],    &quot;California&quot; =&gt; [ &quot;Sebastopol&quot;, &quot;Santa Rosa&quot;, &quot;Berkeley&quot; ],);$\ = &quot;\n&quot;;print &quot;&lt;TABLE&gt; &lt;CAPTION&gt;Cities I Have Known&lt;/CAPTION&gt;&quot;;print Tr(th [qw(State Cities)]);for $k (sort keys %hash) {    print Tr(th($k), td( [ sort @{$hash{$k}} ] ));}print &quot;&lt;/TABLE&gt;&quot;;</PRE><PCLASS="para">That generates text that looks like this:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;TABLE&gt; &lt;CAPTION&gt;Cities I Have Known&lt;/CAPTION&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;TR&gt;&lt;TH&gt;State&lt;/TH&gt; &lt;TH&gt;Cities&lt;/TH&gt;&lt;/TR&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;TR&gt;&lt;TH&gt;California&lt;/TH&gt; &lt;TD&gt;Berkeley&lt;/TD&gt; &lt;TD&gt;Santa Rosa&lt;/TD&gt; </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>        &lt;TD&gt;Sebastopol&lt;/TD&gt; &lt;/TR&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;TR&gt;&lt;TH&gt;Colorado&lt;/TH&gt; &lt;TD&gt;Boulder&lt;/TD&gt; &lt;TD&gt;Denver&lt;/TD&gt; </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>        &lt;TD&gt;Fort Collins&lt;/TD&gt; &lt;/TR&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;TR&gt;&lt;TH&gt;Texas&lt;/TH&gt; &lt;TD&gt;Austin&lt;/TD&gt; &lt;TD&gt;Fort Stockton&lt;/TD&gt; </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>        &lt;TD&gt;Plano&lt;/TD&gt;&lt;/TR&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>    &lt;TR&gt;&lt;TH&gt;Wisconsin&lt;/TH&gt; &lt;TD&gt;Lake Geneva&lt;/TD&gt; &lt;TD&gt;Madison&lt;/TD&gt; </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>        &lt;TD&gt;Superior&lt;/TD&gt;&lt;/TR&gt;</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>&lt;/TABLE&gt;</I></CODE></B></CODE></PRE><PCLASS="para">You can produce the same output using one print statement, although it is slightly trickier, because you have to use a <CODECLASS="literal">map</CODE> to create the implicit loop. This print statement produces output identical to that displayed above:</P><PRECLASS="programlisting">print table        caption('Cities I have Known'),        Tr(th [qw(State Cities)]),        map { Tr(th($_), td( [ sort @{$hash{$_}} ] )) } sort keys %hash;</PRE><PCLASS="para">This is particularly useful for formatting the results of a <ACLASS="indexterm"NAME="ch19-idx-1000005592-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005592-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005592-2"></A>database query, as in <ACLASS="xref"HREF="ch19_08.htm#ch19-34510"TITLE="salcheck">Example 19.3</A> (see <ACLASS="xref"HREF="ch14_01.htm"TITLE="Database Access">Chapter 14, <CITECLASS="chapter">Database Access</CITE></A>).</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch19-34510">Example 19.3: salcheck</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# salcheck - check for salariesuse DBI;use CGI qw(:standard :html3);$limit = param(&quot;LIMIT&quot;);print header(), start_html(&quot;Salary Query&quot;),      h1(&quot;Search&quot;),      <CODECLASS="literal">start_form()</CODE>,      p(&quot;Enter minimum salary&quot;, textfield(&quot;LIMIT&quot;)),      <CODECLASS="literal">submit(),</CODE><CODECLASS="literal">      end_form();</CODE>if (defined $limit) {    $dbh = <ACLASS="indexterm"NAME="ch19-idx-1000007372-0"></A>DBI-&gt;connect(&quot;dbi:mysql:somedb:server.host.dom:3306&quot;,         &quot;username&quot;, &quot;password&quot;)         or die &quot;Connecting: $DBI::errstr&quot;;    $sth = $dbh-&gt;prepare(&quot;SELECT name,salary FROM employees         WHERE salary &gt; $limit&quot;)        or die &quot;Preparing: &quot;, $dbh-&gt;errstr;    $sth-&gt;execute        or die &quot;Executing: &quot;, $sth-&gt;errstr;    print h1(&quot;Results&quot;), &quot;&lt;TABLE BORDER=1&gt;&quot;;    while (@row = $sth-&gt;<CODECLASS="literal">fetchrow_array())</CODE> {           print Tr( td( \@row ) );    }    print &quot;&lt;/TABLE&gt;\n&quot;;    $sth-&gt;finish;    $dbh-&gt;disconnect;}print <CODECLASS="literal">end_html();</CODE><ACLASS="indexterm"NAME="ch19-idx-1000005812-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005812-1"></A></PRE></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-808">See Also</A></H3><PCLASS="para">The documentation for the standard CGI module; <ACLASS="xref"HREF="ch14_11.htm"TITLE="Executing an SQL Command Using DBI and DBD">Recipe 14.10</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.6. Executing Commands Without Shell Escapes"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.8. Redirecting to a Different Location"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.6. Executing Commands Without Shell Escapes</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.8. Redirecting to a Different Location</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <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 + -