📄 ch17_03.htm
字号:
<p>If we do not load a module at startup, but use it in ourapplications, then that module will have to be loaded once for eachchild process. The same applies for our CGI applications runningunder Apache::Registry. Each child process compiles and caches theCGI application once, so the first request that is handled by thatchild will be relatively slow, but all subsequent requests <a name="INDEX-3295" /> <a name="INDEX-3296" />will be much faster.</p></div><a name="ch17-13-fm2xml" /><div class="sect2"><h3 class="sect2">17.3.2. mod_perl Considerations</h3><p>In general, Apache::Registry, does provide <a name="INDEX-3297" /> <a name="INDEX-3298" />a good emulation of a standard CGIenvironment. However, there are some differences you need to keep inmind:</p><ul><li><p>The same precautions that apply to FastCGI apply to<em class="emphasis">mod_perl</em>, namely, always use<a name="INDEX-3299" /><a name="INDEX-3300" />strict mode and it helps to enablewarnings. You should also always initialize your<a name="INDEX-3301" />variables and not assume they are emptywhen your script starts; the warning flag will tell you when you areusing undefined values. Your environment is not cleaned up with youwhen your script ends, so variables that do not go out of scope andglobal variables remain defined the next time your script is called.</p></li><li><p>Due to the fact that your code is only compiled once and then<a name="INDEX-3302" />cached, <a name="INDEX-3303" />lexical variables in the body of yourscripts that you access within your subroutines create closures. Forexample, it is possible to do this in a standard CGI script:<blockquote><pre class="code">my $q = new CGI;check_input( );..sub check_input { unless ( $q->param( "email" ) ) { error( $q, "You didn't supply an email address." ); } . .</pre></blockquote></p><p>Note that we do not pass our CGI object to<tt class="function">check_input</tt><a name="INDEX-3304" />. However, thevariable is still visible to us from within that subroutine. Thisworks fine in CGI. It will create very subtle, confusing errors in<em class="emphasis">mod_perl</em>. The problem is that the first time thescript is run on a particular Apache child process, the value of theCGI object becomes trapped in the cached copy of<tt class="function">check_input</tt>. All future calls to that sameApache child process will reuse the original value of the CGI objectwithin <tt class="function">check_input</tt>. The solution is to pass<tt class="literal">$q</tt> to <tt class="function">check_input</tt> as aparameter or else change <tt class="literal">$q</tt> from a lexical to aglobal <tt class="function">local</tt> variable.</p><p>If you arenot familiar with closures (they are not commonly used in Perl),refer to the <em class="citetitle">perlsub</em> manpage or<em class="citetitle">Programming Perl</em>.</p></li><li><p>The<em class="emphasis">constant</em><a name="INDEX-3305" /><a name="INDEX-3306" /><a name="INDEX-3307" /> module createsconstants by defining them internally as subroutines. SinceApache::Registry creates a persistent environment, using constants inthis manner can produce the following warnings in the error log whenthese scripts are recompiled:<blockquote><pre class="code">Constant subroutine FILENAME redefined at ...</pre></blockquote></p><p>It will not affect the output of your scripts, so you can just ignorethese warnings. Another alternative is to simply make them globalvariables instead; the closure issue is not an problem for variableswhose values never change. This warning should no longer appear forunmodified code in Perl 5.004_05 and higher.</p></li><li><p><a name="INDEX-3308" />Regular expressions that arecompiled with the <em class="emphasis">o</em> flag will remain compiledacross all requests for that script, not just for onerequest.</p></li><li><p><a name="INDEX-3309" /> <a name="INDEX-3310" />File agefunctions, such as <tt class="function">-M</tt>, calculate their valuesrelative to the time the application began, but with<em class="emphasis">mod_perl</em>, that is typically the time the serverbegins. You can get this value from <tt class="literal">$^T</tt> . Thusadding <tt class="literal">(time - $^T)</tt> to the age of a file willyield the true age.</p></li><li><p><tt class="literal">BEGIN</tt><a name="INDEX-3311" /> <a name="INDEX-3312" /> blocks are executed once when your scriptis compiled, not at the beginning of each request. However,<tt class="literal">END</tt> blocks are executed at the end of eachrequest, so you can use these as you normally would.</p></li><li><p><tt class="literal">__END__</tt><a name="INDEX-3313" /> and <tt class="literal">__DATA__</tt> cannot be used within CGI scripts withApache::Registry. They will cause your scripts tofail.</p></li><li><p>Typically, your scripts should not call exit in<em class="emphasis">mod_perl</em>, or it will cause Apache to exitinstead (remember, the Perl interpreter is embedded within the webserver). However, <em class="emphasis">Apache::Registry</em> overrides thestandard <tt class="function">exit</tt><a name="INDEX-3314" /> command so it is safe forthese scripts.</p></li></ul><p>If it's too much of a hassle to convert your application to runeffectively under Apache::Registry, then you should investigate the<a name="INDEX-3315" /><a name="INDEX-3316" />Apache::PerlRunmodule. This module uses the Perl interpreter embedded within Apache,but doesn't<a name="INDEX-3317" />cache compiled versions of your code. As aresult, it can run sloppy CGI scripts, but without the fullperformance improvement of Apache::Registry. It will, nonetheless, befaster than a typical CGI application.</p><p>Increasing the speed of CGI scripts is only part of what<em class="emphasis">mod_perl</em> can do. It also allows you do writecode in Perl that interacts with the Apache response cycle, so youcan do things like handle authentication and authorization yourself.A full discussion of <em class="emphasis">mod_perl</em> is certainlybeyond the scope of this book. If you want to learn more about<em class="emphasis">mod_perl</em>, then you should definitely start withStas Bekman's<em class="emphasis">mod_perl</em><a name="INDEX-3318" /> guide, available at<a href="http://perl.apache.org/guide/">http://perl.apache.org/guide/</a>.Then look at <em class="citetitle">Writing Apache Modules with Perl andC</em>, which provides <a name="INDEX-3319" /> <a name="INDEX-3320" />a very thorough, <a name="INDEX-3321" /> <a name="INDEX-3322" /> <a name="INDEX-3323" />although technical,<a name="INDEX-3324" /><a name="INDEX-3325" /><a name="INDEX-3326" />overview<a name="INDEX-3327" /><a name="INDEX-3328" /><a name="INDEX-3329" />of<em class="emphasis">mod_perl</em>.</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch17_02.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="appa_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">17.2. FastCGI</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">A. Works Cited and Further Reading</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 © 2001</a> O'Reilly & 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 + -