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

📄 ch08_169.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>overload (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_168.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_170.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.169. overload</h2><p><a name="INDEX-1526" />Lets yousubstitute class methods or your own subroutines for standard Perloperators. For example, the code:</p><blockquote><pre class="code">package Number;use overload    "+"  =&gt; \add,    "*=" =&gt; "muas";</pre></blockquote><p>declares function <tt class="literal">add</tt> for addition and method<tt class="literal">muas</tt> in the Number class (or one of its baseclasses) for the assignment form <tt class="literal">*=</tt> ofmultiplication.</p><p>Arguments to <tt class="literal">use overload</tt> are key/value pairs, inwhich the key is the operation being overloaded, and the value is thefunction or method that is to be substituted. Legal values are valuespermitted inside a <tt class="literal">&amp;{ ... }</tt> call, so asubroutine name, a subroutine reference, or an anonymous subroutineare all legal. Legal keys (overloadable operations) are:</p><a name="ch08-495-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Type</p></th><th><p>Operations</p></th></tr><tr><td><p>Arithmetic</p></td><td><p><tt class="literal">+ - * / % ** &lt;&lt; &gt;&gt; x</tt> . <tt class="literal">+= -=*= /= %= **= &lt;&lt;= &gt;&gt;= x= .=</tt></p></td></tr><tr><td><p>Comparison</p></td><td><p><tt class="literal">&lt; &lt;= &gt; &gt;= == != &lt;=&gt;</tt> <tt class="literal">ltle gt ge eq ne cmp</tt></p></td></tr><tr><td><p>Bit and unary</p></td><td><p><tt class="literal">% ^ | neg ! ~</tt></p></td></tr><tr><td><p>Increment, decrement</p></td><td><p><tt class="literal">++ -</tt></p></td></tr><tr><td><p>Transcendental</p></td><td><p><tt class="literal">atan2 cos sin exp abs log sqrt</tt></p></td></tr><tr><td><p>Boolean, string, numeric conversion</p></td><td><p><tt class="literal">bool</tt> "" <tt class="literal">0+</tt></p></td></tr><tr><td><p>Special</p></td><td><p><tt class="literal">nomethod fallback =</tt></p></td></tr></table><p><p>The functions specified with the <tt class="literal">use overload</tt>directive are typically called with three arguments. If thecorresponding operation is binary, then the first two arguments arethe two arguments of the operation. However, the first argumentshould always be an object in the package, so in some cases, theorder of the arguments will be interchanged before the method iscalled. The third argument provides information on the order and canhave these values:</p><dl><dt><i>False (<tt class="literal">0</tt>)</i></dt><dd>The order of arguments is as in the current operation.</p></dd><dt><i>True (<tt class="literal">1</tt>)</i></dt><dd>The arguments are reversed.</p></dd><dt><i>Undefined (<tt class="literal">undef</tt>)</i></dt><dd>The current operation is an assignment variant, but the usualfunction is called instead.</p></dd></dl><p>Unary operations are considered binary operations with the secondargument undefined.</p><p>The special <tt class="literal">nomethod</tt> key should be followed by areference to a function of four parameters and called when theoverloading mechanism cannot find a method for an operation. Thefirst three arguments are the arguments for the corresponding methodif it was found; the fourth argument is the symbol corresponding tothe missing method. If several methods are tried, the last one isused.</p><p>For example, <tt class="literal">1-$a</tt> can be equivalent to: </p><blockquote><pre class="code">&amp;nomethodMethod($a, 1, 1, "-")</pre></blockquote><p>if the pair <tt class="literal">"nomethod" =&gt; "nomethodMethod"</tt> wasspecified in the <tt class="literal">use overload</tt> directive.</p><p>The special <tt class="literal">fallback</tt> key governs what to do if amethod for a particular operation is not found. There are threepossible cases, depending on the value associated with the<tt class="literal">fallback</tt> key:</p><dl><dt><i>Undefined</i></dt><dd>Tries to use a substituted method. If that fails, it tries to callthe method specified for <tt class="literal">nomethod</tt>; if that alsofails, an exception is raised.</p></dd><dt><i>True</i></dt><dd>The same as undefined, but no exception is raised. Instead, Perlsilently reverts to non-overloaded operation.</p></dd><dt><i>Defined, but false</i></dt><dd>Tries to call the method specified for <tt class="literal">nomethod</tt>;if that fails, an exception is raised.</p></dd></dl><p>The overload module provides the following public functions.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Method</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>overload::Method(<em class="replaceable">obj</em>, <em class="replaceable">op</em>)</pre><p>Returns the undefined value or a reference to the method thatimplements <em class="replaceable"><tt>op</tt></em>.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>Overloaded</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>overload::Overloaded(<em class="replaceable">arg</em>)</pre><p>Returns true if <em class="replaceable">arg</em> is subject tooverloading of some operations.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>StrVal</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>overload::StrVal(<em class="replaceable">arg</em>)</pre><p>Gives string value of <em class="replaceable">arg</em> if stringifyoverloading is absent.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_168.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_170.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.168. ops</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.170. PerlIO</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><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 + -