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

📄 ch05_05.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
this feature in one of three ways:</p><ol><li><p>You can pass the<a name="INDEX-1215" /><em class="emphasis">-unique_headers</em> flagwhen you load CGI.pm:</p><blockquote><pre class="code">use CGI qw( -unique_headers );</pre></blockquote></li><li><p>You can set the<tt class="literal">$CGI::HEADERS_ONCE</tt><a name="INDEX-1216" /> variable to a true value after you useCGI.pm, but before you create an object:</p><blockquote><pre class="code">use CGI;$CGI::HEADERS_ONCE = 1;my $q = new CGI;</pre></blockquote></li><li><p>Finally, if you know that you always want this feature, you canenable it globally for all of your scripts by setting<tt class="literal">$HEADERS_ONCE</tt><a name="INDEX-1217" /> to a true value within your copy ofCGI.pm. You can do this just like <tt class="literal">$POST_MAX</tt> and<tt class="literal">$DISABLE_UPLOADS</tt> variables we discussed at thebeginning of the chapter. You will find<tt class="literal">$HEADERS_ONCE</tt> is in the same configurable sectionof CGI.pm:</p><blockquote><pre class="code"># Change this to 1 to suppress redundant HTTP headers$HEADERS_ONCE = 0;</pre></blockquote></li></ol><p>Although adding subroutines to each of your CGI scripts is certainlyan acceptable way to catch errors, it's still not a verygeneral solution. You will probably want to create your own errorpages that are customized for your site. Once you start includingcomplex HTML in your subroutines, it will quickly become toodifficult to maintain them. If you build error subroutines thatoutput error pages according to your site's template, and thenlater someone decides they want to change the site's look, youmust go back and update all of your subroutines. Clearly, a muchbetter option is to create a general error handler that all of yourCGI scripts can access.</p></div><a name="ch05-31-fm2xml" /><div class="sect2"><h3 class="sect2">5.5.4. Custom Module</h3><p>It is a good idea to create your own <a name="INDEX-1218" /><a name="INDEX-1219" />Perl module that's specific toyour site. If you host different sites, or have differentapplications within your site with different looks and feels, you maywish to create a module for each. Within this module, you can placesubroutines that you find yourself using across many CGI scripts.These subroutines will vary depending on your site, but one shouldhandle errors.</p><p>If you have not created your own Perl module before, don'tworry, it's quite simple. <a href="ch05_05.htm#ch05-67677">Example 5-3</a> shows avery minimal module.</p><a name="ch05-67677" /><div class="example"><h4 class="objtitle">Example 5-3. CGIBook::Error.pm </h4><a name="INDEX-1220" /><blockquote><pre class="code">#!/usr/bin/perl -wTpackage CGIBook::Error;# Export the error subroutineuse Exporter;@ISA = "Exporter";@EXPORT = qw( error );$VERSION = "0.01";use strict;use CGI;use CGI::Carp qw( fatalsToBrowser );BEGIN {    sub carp_error {        my $error_message = shift;        my $q = new CGI;        my $discard_this = $q-&gt;header( "text/html" );        error( $q, $error_message );    }    CGI::Carp::set_message( \&amp;carp_error );}sub error {    my( $q, $error_message ) = @_;        print $q-&gt;header( "text/html" ),          $q-&gt;start_html( "Error" ),          $q-&gt;h1( "Error" ),          $q-&gt;p( "Sorry, the following error has occurred: " ),          $q-&gt;p( $q-&gt;i( $error_message ) ),          $q-&gt;end_html;    exit;}1;</pre></blockquote></div><p>The only difference between a <a name="INDEX-1221" /> <a name="INDEX-1,222" />Perl module and a standard Perl script isthat you should save your file with a<em class="filename">.pm</em><a name="INDEX-1223" /><a name="INDEX-1224" /> extension, declarethe name of module's package with the<tt class="function">package</tt><a name="INDEX-1225" />function (this should match the file's name except without the<em class="filename">.pm </em>extension and substituting<tt class="literal">::</tt> for <tt class="literal">/</tt>),<a href="#FOOTNOTE-7">[7]</a> and make surethat it returns a true value when evaluated (the reason for the<tt class="literal">1;</tt> at the bottom).</p><blockquote><a name="FOOTNOTE-7" /><p>[7]Whendetermining the package name, the file's name should berelative to a library path in <tt class="literal">@INC</tt>. In ourexample, we store the file at<em class="filename">/usr/lib/perl5/site_perl/5.005/CGIBook/Error.pm</em>.<em class="filename">/usr/lib/perl5/site_perl/5.005 </em>is a librarydirectory. Thus, the path to the module relative to the librarydirectory is <em class="filename">CGIBook/Error.pm </em>so the package is<em class="emphasis">CGIBook::Error</em>.</p></blockquote><p>It is standard practice to store the<a name="INDEX-1226" />version of the module in<tt class="literal">$VERSION</tt>. For the sake of convenience, we also usethe <a name="INDEX-1227" /><a name="INDEX-1228" />Exporter module to exportthe <tt class="function">error</tt> subroutine. This allows us to refer toit in our scripts<tt class="function"> </tt>as<tt class="function"> error</tt>instead of <tt class="function">CGIBook::Exporter::error</tt>. Refer tothe Exporter manpage or a primary Perl text, such as<em class="citetitle">Programming Perl</em>, for details on usingExporter.</p><p>You have a couple options for saving this file. The simplest solutionis to save it within the<em class="filename">site_perl</em><a name="INDEX-1229" /><a name="INDEX-1230" /> directoryof your <a name="INDEX-1231" />Perl libraries, such as<em class="filename">/usr/lib/perl5/site_perl/5.005/CGIBook/Error.pm</em>.The<em class="emphasis"> site_perl</em> directory includes modules thatare site-specific (i.e., not included in Perl's standarddistribution). The paths of your Perl libraries may differ; you canlocate them on your system with the following command:</p><blockquote><pre class="code">$ perl -e 'print map "$_\n", @INC'</pre></blockquote><p>You probably want to create a subdirectory that is unique to yourorganization, as we did with <em class="filename">CGIBook</em>, to holdall the Perl modules you create.</p><p>You can use the module as follows:</p><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use CGI;use CGIBook::Error;my $q = new CGI;unless ( check_something_important(  ) ) {    error( $q, "Something bad happened." );}</pre></blockquote><p>If you do not have the permission to install the module in your Perllibrary directory, and if you cannot get your system administrator todo it, then you can place the module in another location, forexample,<em class="filename">/usr/local/apache/perl-lib/CGIBook/Error.pm</em>.Then you must remember to include this directory in the list thatPerl searches for modules. The simplest way to <a name="INDEX-1232" />do this is<a name="INDEX-1233" /><a name="INDEX-1234" />with the<tt class="function">lib</tt> <a name="INDEX-1235" /> <a name="INDEX-1,236" />pragma:</p><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use lib "/usr/local/apache/perl-lib";use CGI;use CGIBook::Error;...</pre></blockquote></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_04.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="ch06_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">5.4. Alternatives for Generating Output</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">6. HTML Templates</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 &copy; 2001</a> O'Reilly &amp; 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 + -