📄 ch31_02.htm
字号:
<html><head><title>use autouse (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="use autouse"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch31_01.htm">Chapter 31: Pragmatic Modules</a></td><td align="right" valign="top" width="172"><a href="ch31_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">31.2. use autouse</h2><blockquote><pre class="programlisting">use autouse 'Carp' => qw(carp croak);carp "this carp was predeclared and autoused";</pre></blockquote><p><a name="INDEX-5418"></a>This pragma provides a mechanism for run-time demand loading of aparticular module only when a function from that module really getscalled. It does this by providing a stub function that replacesitself with the real call once triggered. This is similar in spiritto the way the standard <tt class="literal">AutoLoader</tt> and <tt class="literal">SelfLoader</tt> modulesbehave. In short, it's a performance hack to help make your Perlprogram start up faster (on average) by avoiding compilation of modulesthat might never ever be called during a given execution run.</p><p>How <tt class="literal">autouse</tt> behaves depends on whether the module is already loaded.For example, if the module <tt class="literal">Module</tt> is already loaded, then thedeclaration:<blockquote><pre class="programlisting">use autouse 'Module' => qw(func1 func2($;$) Module::func3);</pre></blockquote>is equivalent to the simple import of two functions:<blockquote><pre class="programlisting">use Module qw(func1 func2);</pre></blockquote>This assumes that <tt class="literal">Module</tt> defines <tt class="literal">func2()</tt> with prototype <tt class="literal">($;$)</tt>, andthat <tt class="literal">func1()</tt> and <tt class="literal">func3()</tt> have no prototypes. (More generally, this alsoassumes that <tt class="literal">Module</tt> uses <tt class="literal">Exporter</tt>'s standard <tt class="literal">import</tt> method;otherwise, a fatal error is raised.) In any event, it completely ignores <tt class="literal">Module::func3</tt> since that is presumably already declared.</p><p>If, on the other hand, <tt class="literal">Module</tt> has not yet been loaded when the<tt class="literal">autouse</tt> pragma is parsed, the pragma declares functions <tt class="literal">func1</tt> and<tt class="literal">func2</tt> to be in the current package. It also declares a function<tt class="literal">Module::func3</tt> (which could be construed as mildly antisocial, wereit not for the fact that the nonexistence of the <tt class="literal">Module</tt> modulehas even more antisocial consequences). When these functions arecalled, they make sure the <tt class="literal">Module</tt> in question is loaded and thenreplace themselves with calls to the real functions just loaded.</p><p>Because the <tt class="literal">autouse</tt> pragma moves portions of your program'sexecution from compile time to run time, this can have unpleasantramifications. For example, if the module you <tt class="literal">autouse</tt> has someinitialization that is expected to be done early, this may not happenearly enough. Autousing can also hide bugs in your code whenimportant checks are moved from compile time to run time.</p><p>In particular, if the prototype you've specified on <tt class="literal">autouse</tt> lineis wrong, you will not find out about it until the corresponding functionis executed (which may be months or years later, for a rarely called function).To partially alleviate this problem, you could write yourcode like this during code development:<blockquote><pre class="programlisting">use Chase;use autouse Chase => qw(hue($) cry(&$));cry "this cry was predeclared and autoused";</pre></blockquote>The first line ensures that errors in your argument specificationwill be found early. When your program graduates from developmentinto production mode, you can comment out the regular loading ofthe <tt class="literal">Chase</tt> module and leave just the autousing call in place. That way you get safety during development and performance during production.</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch31_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch31_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.1. use attributes</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">31.3. use base</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -