📄 ch11_03.htm
字号:
<html><head><title>Overriding Built-in Functions (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="Overriding Built-in Functions"><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="ch11_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch11_01.htm">Chapter 11: Modules</a></td><td align="right" valign="top" width="172"><a href="ch12_01.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">11.3. Overriding Built-in Functions</h2><p><a name="INDEX-2328"></a><a name="INDEX-2329"></a><a name="INDEX-2330"></a><a name="INDEX-2331"></a>Many built-in functions may be <em class="emphasis">overridden</em>,although (like knocking holes in your walls) you should do this onlyoccasionally and for good reason. Typically, this might be done by apackage attempting to emulate missing built-in functionality on anon-Unix system. (Do not confuse overriding with<em class="emphasis">overloading</em>, which adds additionalobject-oriented meanings to built-in operators, but doesn't overridemuch of anything. See the discussion of the<tt class="literal">overload</tt> module in <a href="ch13_01.htm">Chapter 13, "Overloading"</a> for more on that.)</p><p><a name="INDEX-2332"></a>Overriding may be done only by importing the name from amodule--ordinary predeclaration isn't good enough. To be perfectlyforthcoming, it's the assignment of a code reference to a typeglobthat triggers the override, as in <tt class="literal">*open = \&myopen</tt>.Furthermore, the assignment must occur in some other package; thismakes accidental overriding through typeglob aliasing intentionallydifficult. However, if you really want to do your own overriding,don't despair, because the <tt class="literal">subs</tt> pragma lets you predeclaresubroutines via the import syntax, so those names then override thebuilt-in ones:<blockquote><pre class="programlisting">use subs qw(chdir chroot chmod chown);chdir $somewhere;sub chdir { ... }</pre></blockquote>In general, modules should not export built-in names like<tt class="literal">open</tt> or <tt class="literal">chdir</tt> as part of theirdefault <tt class="literal">@EXPORT</tt> list, since these names may sneakinto someone else's namespace and change the semanticsunexpectedly. If the module includes the name in the<tt class="literal">@EXPORT_OK</tt> list instead, importers will be forcedto explicitly request that the built-in name be overridden, thuskeeping everyone honest.</p><p><a name="INDEX-2333"></a><a name="INDEX-2334"></a>The original versions of the built-in functions are always accessiblevia the <tt class="literal">CORE</tt> pseudopackage. Therefore,<tt class="literal">CORE::chdir</tt> will always be the version originallycompiled into Perl, even if the <tt class="literal">chdir</tt> keyword hasbeen overridden.</p><p>Well, almost always. The foregoing mechanism for overriding built-infunctions is restricted, quite deliberately, to the package thatrequests the import. But there is a more sweeping mechanism you canuse when you wish to override a built-in function everywhere, withoutregard to namespace boundaries. This is achieved by defining thefunction in the <tt class="literal">CORE::GLOBAL</tt> pseudopackage. Belowis an example that replaces the <tt class="literal">glob</tt> operator withsomething that understands regular expressions. (Note that thisexample does not implement everything needed to cleanly overridePerl's built-in <tt class="literal">glob</tt>, which behaves differentlydepending on whether it appears in a scalar or list context. Indeed,many Perl built-ins have such context-sensitive behaviors, and anyproperly written override should adequately support these. For afully functional example of glob overriding, study the<tt class="literal">File::Glob</tt> module bundled with Perl.) Anyway,here's the antisocial version:<blockquote><pre class="programlisting">*CORE::GLOBAL::glob = sub { my $pat = shift; my @got; local *D; if (opendir D, '.') { @got = grep /$pat/, readdir D; closedir D; } return @got;}package Whatever;print <^[a-z_]+\.pm\$>; # show all pragmas in the current directory</pre></blockquote>By overriding <tt class="literal">glob</tt> globally, this preemptivelyforces a new (and subversive) behavior for the <tt class="literal">glob</tt>operator in <em class="emphasis">every</em> namespace, without thecognizance or cooperation of modules that own those namespaces.Naturally, this must be done with extreme caution--if it must be doneat all. And it probably mustn't.</p><p>Our overriding philosophy is: it's nice to be important, but it's moreimportant to be nice.</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="ch11_02.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="ch12_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">11.2. Creating Modules</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">12. Objects</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 + -