ch08.htm
来自「Web_Programming_with_Perl5,一个不错的Perl语言教程」· HTM 代码 · 共 1,982 行 · 第 1/4 页
HTM
1,982 行
35: <TITLE>MCES's Audiovisual Reference Room - Full Listing</TITLE>
36: <LINK REV="MADE" HREF="mailto:avlibrar\@ces.msstate.edu">
37: <META NAME="MarkUp" CONTENT="Tom White">
38: </HEAD>
39: <BODY>
40: <h1><img src="/pics/ces-b1mt.gif" width=499 height=53
41: alt="Cooperative Extension Service -
42: Mississippi State University">
43: </h1>
44: <h2>
45: Audiovisual Reference Room
46: </h2>
47: <h3>
48: Full Catalog Listing
49: </h3>
50: <ol>
51: EOD
52: }
53: sub footer {
54: my @loctime;
55: my $lastupdate;
56: @loctime = localtime;
57: $lastupdate = sprintf("%02d-%02d-%02d", ($loctime[4]+1),
58: ($loctime[3]), ($loctime[5]));
59: print NEWF <<EOD;
60: </ol>
61: <HR>
62: [<a href="./">Audiovisual Reference Room</a>]
63: <br>
64: [<a href="/ces.html">Cooperative Extension</a>]
65: [<a href="http://www.msstate.edu/">Mississippi State</a>]
66: [<a href="http://www.msstate.edu/web/search.htm">Search MSU's Web</a>]
67: <br>
68: <FONT SIZE="-1">
69: For information about this page, contact
70: <a href="mailto:avlibrar\@ces.msstate.edu">avlibrar\@ces.msstate.edu</A>.
71: <br>
72: <b>Last modified:</b> $lastupdate
73: <br>
74: <A HREF="http://www.msstate.edu/web/disclaim.htm">Mississippi State University is
75: an equal opportunity institution.</A>
76: </FONT>
77: </BODY>
78: </HTML>
79: EOD
80: }
</FONT></PRE>
<P><A HREF="09wpp01.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/09wpp01.jpg"><TT><B>Figure 8.1.</B></TT></A> Output from
program in Listing 8.3.
<CENTER>
<H4><A NAME="Heading13"></A><FONT COLOR="#000077">Build HTML from Data on a Single
Item in the Database</FONT></H4>
</CENTER>
<P>The program in Listing 8.4 uses DBI to query the database about a specific item
and to extract the various information provided in the raw data we input earlier
into the database. The data is formatted into HTML output in a similar fashion as
in Listing 8.3. Figure 8.2 shows the HTML output generated by this program. Notice
CGI.pm is used here to parse the URI encoded data containing the call_num of the
item to look up. There are four main steps in the program in Listing 8.4.
<OL>
<LI>line 20 The driver (DBD) for mSQL is installed.
<P>
<LI>line 21 DBI is instructed to connect to the database named AV_LIBRARY.
<P>
<LI>line 25 The prepare statement is invoked.
<P>
<LI>line 30 The execute statement is invoked.
</OL>
<CENTER>
<H3><A NAME="Heading14"></A><FONT COLOR="#000077">Listing 8.4. Program that writes
HTML from data</FONT></H3>
</CENTER>
<CENTER>
<H3><A NAME="Heading15"></A><FONT COLOR="#000077">about a single item in the database.</FONT></H3>
</CENTER>
<PRE><FONT COLOR="#0066FF">1: #!/usr/local/bin/perl
2: #
3: # Display an item from the catalog table in the AV_LIBRARY database
4: #
5: # by Tom White
6: #
7: # $Id: display.cgi,v 1.4 1996/08/28 13:42:06 avlibrar Exp avlibrar $
8: open (STDERR, ">&STDOUT");
9: select STDERR; $| = 1;
10: select STDOUT; $| = 1;
11: use CGI;
12: $ENV{`PATH'} = `/usr/local/bin:/usr/ucb:/usr/bin:/usr/ccs/bin:/usr/sbin';
13: $ENV{`MANPATH'} =`/usr/local/man:/usr/man';
14: $ENV{`PAGER'} = `/usr/bin/cat';
15: $ENV{`SHELL'} = `/bin/sh';
16: $ENV{`IFS'} = ``;
17: $query = new CGI;
18: &pheader;
19: use DBI;
20: $drh = DBI->install_driver( `mSQL' ) || die "Could not install: $DBI::errstr";
21: $dbh = $drh->connect( ``, `AV_LIBRARY' )
22: || die "Could not connect: $DBI::errstr";
23: $call_num = uc $query->param(`cn');
24: $sstring = "call_num = \'$call_num\'";
25: $sth = $dbh->prepare( "SELECT title, subtitle, type, description,
26: publisher, narrative, kinder, primer,
27: element, junior, senior, college, adult
28: FROM catalog WHERE $sstring" )
29: || die "Could not start select: $DBI::errstr";
30: $sth->execute || die "Could not select: $DBI::errstr";
31: print "<dt><b>Call Number</b> <dd>$call_num\n";
32: if ( ($title, $subtitle, $type, $description, $publisher,
33: $narrative, $kinder, $primer, $element, $junior,
34: $senior, $college, $adult) = $sth->fetchrow ) {
35: print "<dt><b>Title</b> <dd>$title\n";
36: if ( $subtitle ) {
37: print "<dt><b>Subtitle</b> <dd>$subtitle\n";
38: }
39: print "<dt><b>Medium</b> <dd>$type";
40: if ( $description ) {
41: print " ($description)";
42: }
43: print "\n";
44: print "<dt><b>Publisher</b> <dd>$publisher\n";
45: print "<dt><b>Narrative</b> <dd>$narrative\n";
46: print "<dt><b>Audience</b>";
47: print " <dd>Kindergarten (ages 3-5)" if ( $kinder );
48: print " <dd>Primary (grades 1-3)" if ( $primer );
49: print " <dd>Elementary (grades 4-6)" if ( $element );
50: print " <dd>Junior High (grades 7-9)" if ( $junior );
51: print " <dd>Senior High (grades 10-12)" if ( $senior );
52: print " <dd>College" if ( $college );
53: print " <dd>Adult" if ( $adult );
54: } else {
55: print "<br><b>is NOT on file.</b>";
56: }
57: print "\n";
58: $sth->finish;
59: &pfooter;
60: exit;
61: sub pheader {
62: print $query->header;
63: print <<EOD;
64: <HTML>
65: <HEAD>
66: <TITLE>MCES's Audiovisual Reference Room - Item Listing</TITLE>
67: <LINK REV="MADE" HREF="mailto:avlibrar\@ces.msstate.edu">
68: <META NAME="MarkUp" CONTENT="Tom White">
69: </HEAD>
70: <BODY>
71: <h1><img src="/pics/ces-b1mt.gif" width=499 height=53
72: alt="Cooperative Extension Service -
73: Mississippi State University">
74: </h1>
75: <h2>
76: Audiovisual Reference Room
77: </h2>
78: <h3>
79: Item Listing
80: </h3>
81: <dl>
82: EOD
83: }
84: sub pfooter {
85: print <<EOD;
86: </dl>
87: <HR>
88: [<a href="./">Audiovisual Reference Room</a>]
89: <br>
90: [<a href="/ces.html">Cooperative Extension</a>]
91: [<a href="http://www.msstate.edu/">Mississippi State</a>]
92: [<a href="http://www.msstate.edu/web/search.htm">Search MSU's Web</a>]
93: <br>
94: <FONT SIZE="-1">
95: For information about this page, contact
96: <a href="mailto:avlibrar\@ces.msstate.edu">avlibrar\@ces.msstate.edu</A>.
97: <br>
98: <b>Last modified:</b> 08-10-96
99: <br>
100: <A HREF="http://www.msstate.edu/web/disclaim.htm">Mississippi State
University is an equal opportunity institution.</A>
101: </FONT>
102: </BODY>
103: </HTML>
104: EOD
105: }
</FONT></PRE>
<P><A HREF="09wpp02.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/09wpp02.jpg"><TT><B>Figure 8.2.</B></TT></A> Output from
Listing 8.4.
<CENTER>
<H4><A NAME="Heading16"></A><FONT COLOR="#000077">Searching the Database for Keywords
and Outputting Results</FONT></H4>
</CENTER>
<P>Listing 8.5 uses DBI to query the database about an item based on parameters specified
in an HTML form. The data returned is formatted into HTML output in a similar fashion
as in Listing 8.3. Figures 8.3 and 8.4 show the HTML output generated by this program.
If no input is sent to the CGI, only the search form is displayed; however, if URI
encoded data is detected by CGI.pm in the URI, that data is operated on, the database
is queried, and search results are displayed. Notice extensive use of CGI.pm in this
example.
<CENTER>
<H3><A NAME="Heading17"></A><FONT COLOR="#000077">Listing 8.5. Searching the database
for keywords</FONT></H3>
</CENTER>
<PRE><FONT COLOR="#0066FF">1: #!/usr/local/bin/perl
2: #
3: # Search the AV Reference Room
4: #
5: # by Tom White
6: #
7: # $Id: search.cgi,v 1.6 1996/08/28 13:42:13 avlibrar Exp avlibrar $
8: #
9: open (STDERR, ">&STDOUT");
10: select STDERR; $| = 1;
11: select STDOUT; $| = 1;
12: use CGI;
13: $ENV{`PATH'} = `/usr/local/bin:/usr/ucb:/usr/bin:/usr/ccs/bin:/usr/sbin';
14: $ENV{`MANPATH'} =`/usr/local/man:/usr/man';
15: $ENV{`PAGER'} = `/usr/bin/cat';
16: $ENV{`SHELL'} = `/bin/sh';
17: $ENV{`IFS'} = ``;
18: $query = new CGI;
19: # build a WHERE clause for mSQL
20: # note: mSQL evaluates compound conditions strictly left-to-right
21: $where_string = "";
22: $conjunc = "WHERE ";
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?