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

📄 ch19_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 19.5. Making CGI Scripts Efficient (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:34Z"><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_05.htm"TITLE="19.4. Writing a Safe CGI Program"><LINKREL="next"HREF="ch19_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes"></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_05.htm"TITLE="19.4. Writing a Safe CGI Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.4. Writing a Safe CGI Program"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_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.6. Executing Commands Without Shell Escapes"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-40762">19.5. Making CGI Scripts Efficient</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-442">Problem<ACLASS="indexterm"NAME="ch19-idx-1000005441-0"></A></A></H3><PCLASS="para">Your CGI script is called often, and the web server is suffering as a result. You'd like to lessen the load your CGI script causes.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-448">Solution</A></H3><PCLASS="para">Use <CODECLASS="literal">mod_perl</CODE><ACLASS="indexterm"NAME="ch19-idx-1000005442-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005442-1"></A> in the Apache web server along with the following section in your <EMCLASS="emphasis">httpd.conf</EM> file:</P><PRECLASS="programlisting">Alias /perl/ /real/path/to/perl/scripts/&lt;Location /perl&gt;SetHandler  perl-scriptPerlHandler Apache::RegistryOptions ExecCGI&lt;/Location&gt;PerlModule Apache::RegistryPerlModule CGIPerlSendHeader On</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-476">Discussion</A></H3><PCLASS="para">Using the <CODECLASS="literal">mod_perl</CODE> Apache web server module, you can write Perl code that will step in at any part of a request's processing. You can write your own logging and authentication routines, define virtual hosts and their configuration, and write your own handlers for certain types of request.</P><PCLASS="para">The snippet above says that requests with URLs starting in <EMCLASS="emphasis">/perl/</EM> are actually in <EMCLASS="emphasis">/real/path/to/perl/scripts/</EM> and that they should be handled by Apache::Registry. This runs them in a CGI environment. <CODECLASS="literal">PerlModule</CODE> <CODECLASS="literal">CGI</CODE> preloads the CGI module, and <CODECLASS="literal">PerlSendHeader</CODE> <CODECLASS="literal">On</CODE> makes most of your CGI scripts work out of the box with <CODECLASS="literal">mod_perl</CODE>.</P><PCLASS="para"><EMCLASS="emphasis">/perl/</EM> works analogously to <EMCLASS="emphasis">/cgi-bin/</EM>. To make the suffix <EMCLASS="emphasis">.perl</EM> indicate <CODECLASS="literal">mod_perl</CODE> CGI scripts just as the suffix <ICLASS="filename">.cgi</I> indicates regular CGI scripts, use the following in your Apache configuration file:</P><PRECLASS="programlisting">&lt;Files *.perl&gt;SetHandler  perl-scriptPerlHandler Apache::RegistryOptions ExecCGI&lt;/Files&gt;</PRE><PCLASS="para">Because the Perl interpreter that runs your CGI script doesn't shut down when your script is done (as normally happens when the web server runs your script as a separate program), you cannot rely on your global variables being undefined when the program starts. <BCLASS="emphasis.bold">-w</B> and <CODECLASS="literal">use</CODE> <CODECLASS="literal">strict</CODE> check for many bad habits in these kinds of scripts. There are other gotchas, too &nbsp;-  see the <EMCLASS="emphasis">mod_perl_traps</EM> manpage.</P><PCLASS="para">Don't worry about how big your web server processes appear to grow from pre-loading all these scripts. They need to find their way into memory eventually, and it's better to happen before Apache forks off kids. That way each script has to be in memory only once, because forked children have shared memory pages (under all modern operating systems). In other words, it only appears to take up more memory this way. It actually takes less!</P><PCLASS="para">An interface to Netscape's server is also available at <EMCLASS="emphasis">http://www.perl.com/CPAN-local/modules/by-module/Netscape/nsapi_perl-0.24.tar.gz</EM> that effects a similar performance gain by avoiding forking. <ACLASS="indexterm"NAME="ch19-idx-1000005444-0"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-502">See Also</A></H3><PCLASS="para">The documentation for Bundle::Apache, Apache, Apache::Registry, from CPAN; <ACLASS="systemitem.url"HREF="http://perl.apache.org">http://perl.apache.org</A>/, mod_perl FAQ at <ACLASS="systemitem.url"HREF="http://perl.apache.org/faqa/">http://perl.apache.org/faqa/</A>, the <ICLASS="filename">mod_perl </I>(3) and<ICLASS="filename"> cgi_to_mod_perl </I>(1) manpages (if you have them)</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_05.htm"TITLE="19.4. Writing a Safe CGI Program"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.4. Writing a Safe CGI Program"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_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.6. Executing Commands Without Shell Escapes"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.4. Writing a Safe CGI Program</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.6. Executing Commands Without Shell Escapes</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 + -