📄 ch12_12.htm
字号:
<HTML><HEAD><TITLE>Recipe 12.11. Overriding Built-In Functions (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:41:46Z"><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="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"><LINKREL="prev"HREF="ch12_11.htm"TITLE="12.10. Speeding Up Module Loading with Autoloader"><LINKREL="next"HREF="ch12_13.htm"TITLE="12.12. Reporting Errors and Warnings Like Built-Ins"></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="ch12_11.htm"TITLE="12.10. Speeding Up Module Loading with Autoloader"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.10. Speeding Up Module Loading with Autoloader"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_13.htm"TITLE="12.12. Reporting Errors and Warnings Like Built-Ins"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.12. Reporting Errors and Warnings Like Built-Ins"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch12-33189">12.11. Overriding Built-In Functions</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1110">Problem<ACLASS="indexterm"NAME="ch12-idx-1000005257-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005257-1"></A><ACLASS="indexterm"NAME="ch12-idx-1000005257-2"></A><ACLASS="indexterm"NAME="ch12-idx-1000005257-3"></A></A></H3><PCLASS="para">You want to replace a standard, built-in function with your own version.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1116">Solution</A></H3><PCLASS="para">Import that function from another module into your own namespace.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1122">Discussion</A></H3><PCLASS="para">Many (but not all) of Perl's built-in functions may be overridden. This is not something to be attempted lightly, but it is possible. You might do this, for example, if you are running on a platform that doesn't support the function that you'd like to emulate. Or, you might want to add your own wrapper around the built-in.</P><PCLASS="para">Not all reserved words have the same status. Those that return a negative number in the C-language <CODECLASS="literal">keyword()</CODE> function in the <EMCLASS="emphasis">toke.c</EM> file in your Perl source kit may be overridden. Keywords that cannot be overridden as of 5.004 are <CODECLASS="literal">chop</CODE>, <CODECLASS="literal">defined</CODE>, <CODECLASS="literal">delete</CODE>, <CODECLASS="literal">do</CODE>, <CODECLASS="literal">dump</CODE>, <CODECLASS="literal">each</CODE>, <CODECLASS="literal">else</CODE>, <CODECLASS="literal">elsif</CODE>, <CODECLASS="literal">eval</CODE>, <CODECLASS="literal">exists</CODE>, <CODECLASS="literal">for</CODE>, <CODECLASS="literal">foreach</CODE>, <CODECLASS="literal">format</CODE>, <CODECLASS="literal">glob</CODE>, <CODECLASS="literal">goto</CODE>, <CODECLASS="literal">grep</CODE>, <CODECLASS="literal">if</CODE>, <CODECLASS="literal">keys</CODE>, <CODECLASS="literal">last</CODE>, <CODECLASS="literal">local</CODE>, <CODECLASS="literal">m</CODE>, <CODECLASS="literal">map</CODE>, <CODECLASS="literal">my</CODE>, <CODECLASS="literal">next</CODE>, <CODECLASS="literal">no</CODE>, <CODECLASS="literal">package</CODE>, <CODECLASS="literal">pop</CODE>, <CODECLASS="literal">pos</CODE>, <CODECLASS="literal">print</CODE>, <CODECLASS="literal">printf</CODE>, <CODECLASS="literal">prototype</CODE>, <CODECLASS="literal">push</CODE>, <CODECLASS="literal">q</CODE>, <CODECLASS="literal">qq</CODE>, <CODECLASS="literal">qw</CODE>, <CODECLASS="literal">qx</CODE>, <CODECLASS="literal">redo</CODE>, <CODECLASS="literal">return</CODE>, <CODECLASS="literal">s</CODE>, <CODECLASS="literal">scalar</CODE>, <CODECLASS="literal">shift</CODE>, <CODECLASS="literal">sort</CODE>, <CODECLASS="literal">splice</CODE>, <CODECLASS="literal">split</CODE>, <CODECLASS="literal">study</CODE>, <CODECLASS="literal">sub</CODE>, <CODECLASS="literal">tie</CODE>, <CODECLASS="literal">tied</CODE>, <CODECLASS="literal">tr</CODE>, <CODECLASS="literal">undef</CODE>, <CODECLASS="literal">unless</CODE>, <CODECLASS="literal">unshift</CODE>, <CODECLASS="literal">untie</CODE>, <CODECLASS="literal">until</CODE>, <CODECLASS="literal">use</CODE>, <CODECLASS="literal">while</CODE>, and <CODECLASS="literal">y</CODE>. The rest can.</P><PCLASS="para">A standard Perl module that does this is <ACLASS="indexterm"NAME="ch12-idx-1000005258-0"></A>Cwd, which can overload <CODECLASS="literal">chdir</CODE>. Others are the by-name versions of the functions returning lists: File::stat, Net::hostent, Net::netent, Net::protoent, Net::servent, Time::gmtime, Time::localtime, Time::tm, User::grent, and User::pwent. These modules all override built-in functions like <CODECLASS="literal">stat</CODE> or <CODECLASS="literal">getpwnam</CODE> to return an object that can be accessed using a name, like <CODECLASS="literal">getpwnam("daemon")->dir</CODE>. To do this, they have to override the original, list-returning versions of those functions.</P><PCLASS="para">Overriding may be done uniquely by importing the function from another package. This import only takes effect in the importing package, not in all possible packages. It's not enough simply to predeclare the function. You have to import it. This is a guard against accidentally redefining built-ins.</P><PCLASS="para">Let's say that you'd like to replace the built-in <CODECLASS="literal">time</CODE> function, whose answer is in integer seconds, with one that returns a floating point number instead. You could make a FineTime module with an optionally exported time function as follows:</P><PRECLASS="programlisting">package FineTime;use strict;require Exporter;use vars qw(@ISA @EXPORT_OK);@ISA = qw(Exporter);@EXPORT_OK = qw(time);sub time() { ..... } # TBA</PRE><PCLASS="para">Then the user who wants to use this augmented version of <CODECLASS="literal">time</CODE> would say something like:</P><PRECLASS="programlisting">use FineTime qw(time);$start = time();1 while print time() - $start, "\n";</PRE><PCLASS="para">This code assumes that your system has a function you can stick in the "TBA" definition above. See <ACLASS="xref"HREF="ch12_15.htm"TITLE="Using h2ph to Translate C #include Files">Recipe 12.14</A> for strategies that may work on your system.</P><PCLASS="para">For overriding of methods and operators, see <ACLASS="xref"HREF="ch13_01.htm"TITLE="Classes, Objects, and Ties">Chapter 13</A>.<ACLASS="indexterm"NAME="ch12-idx-1000005260-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005260-1"></A><ACLASS="indexterm"NAME="ch12-idx-1000005260-2"></A><ACLASS="indexterm"NAME="ch12-idx-1000005260-3"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1166">See Also</A></H3><PCLASS="para">The section on <ACLASS="olink"HREF="../prog/ch05_02.htm#PERL2-CH-5-SECT-2.2">"Overriding Built-in Functions"</A> in <ACLASS="olink"HREF="../prog/ch05_01.htm">Chapter 5</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A> and in <ICLASS="filename">perlsub </I>(1)</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="ch12_11.htm"TITLE="12.10. Speeding Up Module Loading with Autoloader"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.10. Speeding Up Module Loading with Autoloader"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="ch12_13.htm"TITLE="12.12. Reporting Errors and Warnings Like Built-Ins"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.12. Reporting Errors and Warnings Like Built-Ins"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">12.10. Speeding Up Module Loading with Autoloader</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">12.12. Reporting Errors and Warnings Like Built-Ins</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 © 2002</a> O'Reilly & 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 + -