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, &quot;&gt;&amp;STDOUT&quot;);



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:  &amp;pheader;







19:  use DBI;







20:  $drh = DBI-&gt;install_driver( `mSQL' ) || die &quot;Could not install: $DBI::errstr&quot;;



21:  $dbh = $drh-&gt;connect( ``, `AV_LIBRARY' )



22:            || die &quot;Could not connect: $DBI::errstr&quot;;







23:  $call_num = uc $query-&gt;param(`cn');



24:  $sstring = &quot;call_num = \'$call_num\'&quot;;



25:  $sth = $dbh-&gt;prepare( &quot;SELECT title, subtitle, type, description,



26:                                publisher, narrative, kinder, primer,



27:                                element, junior, senior, college, adult



28:                         FROM catalog WHERE $sstring&quot; )



29:           || die &quot;Could not start select: $DBI::errstr&quot;;



30:  $sth-&gt;execute || die &quot;Could not select: $DBI::errstr&quot;;



31:  print &quot;&lt;dt&gt;&lt;b&gt;Call Number&lt;/b&gt; &lt;dd&gt;$call_num\n&quot;;



32:  if ( ($title, $subtitle, $type, $description, $publisher,



33:        $narrative, $kinder, $primer, $element, $junior,



34:        $senior, $college, $adult) = $sth-&gt;fetchrow ) {



35:      print &quot;&lt;dt&gt;&lt;b&gt;Title&lt;/b&gt; &lt;dd&gt;$title\n&quot;;



36:      if ( $subtitle ) {



37:        print &quot;&lt;dt&gt;&lt;b&gt;Subtitle&lt;/b&gt; &lt;dd&gt;$subtitle\n&quot;;



38:      }



39:      print &quot;&lt;dt&gt;&lt;b&gt;Medium&lt;/b&gt; &lt;dd&gt;$type&quot;;



40:      if ( $description ) {



41:        print &quot; ($description)&quot;;



42:      }



43:      print &quot;\n&quot;;



44:      print &quot;&lt;dt&gt;&lt;b&gt;Publisher&lt;/b&gt; &lt;dd&gt;$publisher\n&quot;;



45:      print &quot;&lt;dt&gt;&lt;b&gt;Narrative&lt;/b&gt; &lt;dd&gt;$narrative\n&quot;;



46:      print &quot;&lt;dt&gt;&lt;b&gt;Audience&lt;/b&gt;&quot;;



47:      print &quot; &lt;dd&gt;Kindergarten (ages 3-5)&quot; if ( $kinder );



48:      print &quot; &lt;dd&gt;Primary (grades 1-3)&quot; if ( $primer );



49:      print &quot; &lt;dd&gt;Elementary (grades 4-6)&quot; if ( $element );



50:      print &quot; &lt;dd&gt;Junior High (grades 7-9)&quot; if ( $junior );



51:      print &quot; &lt;dd&gt;Senior High (grades 10-12)&quot; if ( $senior );



52:      print &quot; &lt;dd&gt;College&quot; if ( $college );



53:      print &quot; &lt;dd&gt;Adult&quot; if ( $adult );



54:  } else {



55:      print &quot;&lt;br&gt;&lt;b&gt;is NOT on file.&lt;/b&gt;&quot;;



56:  }



57:  print &quot;\n&quot;;



58:  $sth-&gt;finish;







59:  &amp;pfooter;







60:  exit;







61:  sub pheader {



62:    print $query-&gt;header;



63:    print &lt;&lt;EOD;



64:  &lt;HTML&gt;







65:  &lt;HEAD&gt;



66:    &lt;TITLE&gt;MCES's Audiovisual Reference Room - Item Listing&lt;/TITLE&gt;



67:    &lt;LINK REV=&quot;MADE&quot; HREF=&quot;mailto:avlibrar\@ces.msstate.edu&quot;&gt;



68:    &lt;META NAME=&quot;MarkUp&quot; CONTENT=&quot;Tom White&quot;&gt;



69:  &lt;/HEAD&gt;







70:  &lt;BODY&gt;







71:  &lt;h1&gt;&lt;img src=&quot;/pics/ces-b1mt.gif&quot; width=499 height=53



72:           alt=&quot;Cooperative Extension Service -



73:                Mississippi State University&quot;&gt;



74:  &lt;/h1&gt;



75:  &lt;h2&gt;



76:    Audiovisual Reference Room



77:  &lt;/h2&gt;



78:  &lt;h3&gt;



79:    Item Listing



80:  &lt;/h3&gt;



81:  &lt;dl&gt;



82:  EOD



83:  }







84:  sub pfooter {



85:    print &lt;&lt;EOD;



86:  &lt;/dl&gt;



87:  &lt;HR&gt;



88:    [&lt;a href=&quot;./&quot;&gt;Audiovisual Reference Room&lt;/a&gt;]



89:    &lt;br&gt;



90:    [&lt;a href=&quot;/ces.html&quot;&gt;Cooperative Extension&lt;/a&gt;]



91:    [&lt;a href=&quot;http://www.msstate.edu/&quot;&gt;Mississippi State&lt;/a&gt;]



92:    [&lt;a href=&quot;http://www.msstate.edu/web/search.htm&quot;&gt;Search MSU's Web&lt;/a&gt;]



93:    &lt;br&gt;



94:    &lt;FONT SIZE=&quot;-1&quot;&gt;



95:    For information about this page, contact



96:       &lt;a href=&quot;mailto:avlibrar\@ces.msstate.edu&quot;&gt;avlibrar\@ces.msstate.edu&lt;/A&gt;.



97:    &lt;br&gt;



98:    &lt;b&gt;Last modified:&lt;/b&gt; 08-10-96



99:    &lt;br&gt;



100:   &lt;A HREF=&quot;http://www.msstate.edu/web/disclaim.htm&quot;&gt;Mississippi State 



       University is an equal opportunity institution.&lt;/A&gt;



101:   &lt;/FONT&gt;



102:  &lt;/BODY&gt;



103:  &lt;/HTML&gt;



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, &quot;&gt;&amp;STDOUT&quot;);



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 = &quot;&quot;;



22:  $conjunc = &quot;WHERE &quot;;



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?