📄 ch19_08.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 & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & 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><OL><LI>red</LI> <LI>blue</LI> <LI>green</LI></OL></I></CODE></B></CODE>@names = qw(Larry Moe Curly);print ul( li({ -TYPE => "disc" }, \@names) );<CODECLASS="userinput"><B><CODECLASS="replaceable"><I><UL><LI TYPE="disc">Larry</LI> <LI TYPE="disc">Moe</LI> </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <LI TYPE="disc">Curly</LI></UL></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("alpha");<CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <LI>alpha</LI></I></CODE></B></CODE>print li( [ "alpha", "omega"] );<CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <LI>alpha</LI> <LI>omega</LI></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"><TR></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 = ( "Wisconsin" => [ "Superior", "Lake Geneva", "Madison" ], "Colorado" => [ "Denver", "Fort Collins", "Boulder" ], "Texas" => [ "Plano", "Austin", "Fort Stockton" ], "California" => [ "Sebastopol", "Santa Rosa", "Berkeley" ],);$\ = "\n";print "<TABLE> <CAPTION>Cities I Have Known</CAPTION>";print Tr(th [qw(State Cities)]);for $k (sort keys %hash) { print Tr(th($k), td( [ sort @{$hash{$k}} ] ));}print "</TABLE>";</PRE><PCLASS="para">That generates text that looks like this:</P><PRECLASS="programlisting"><CODECLASS="userinput"><B><CODECLASS="replaceable"><I><TABLE> <CAPTION>Cities I Have Known</CAPTION></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TR><TH>State</TH> <TH>Cities</TH></TR></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TR><TH>California</TH> <TD>Berkeley</TD> <TD>Santa Rosa</TD> </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TD>Sebastopol</TD> </TR></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TR><TH>Colorado</TH> <TD>Boulder</TD> <TD>Denver</TD> </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TD>Fort Collins</TD> </TR></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TR><TH>Texas</TH> <TD>Austin</TD> <TD>Fort Stockton</TD> </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TD>Plano</TD></TR></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TR><TH>Wisconsin</TH> <TD>Lake Geneva</TD> <TD>Madison</TD> </I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I> <TD>Superior</TD></TR></I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I></TABLE></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("LIMIT");print header(), start_html("Salary Query"), h1("Search"), <CODECLASS="literal">start_form()</CODE>, p("Enter minimum salary", textfield("LIMIT")), <CODECLASS="literal">submit(),</CODE><CODECLASS="literal"> end_form();</CODE>if (defined $limit) { $dbh = <ACLASS="indexterm"NAME="ch19-idx-1000007372-0"></A>DBI->connect("dbi:mysql:somedb:server.host.dom:3306", "username", "password") or die "Connecting: $DBI::errstr"; $sth = $dbh->prepare("SELECT name,salary FROM employees WHERE salary > $limit") or die "Preparing: ", $dbh->errstr; $sth->execute or die "Executing: ", $sth->errstr; print h1("Results"), "<TABLE BORDER=1>"; while (@row = $sth-><CODECLASS="literal">fetchrow_array())</CODE> { print Tr( td( \@row ) ); } print "</TABLE>\n"; $sth->finish; $dbh->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 © 2002</a> O'Reilly & 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 + -