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

📄 ch13_06.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Overloading Constants (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 &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Overloading Constants"><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="ch13_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch13_01.htm">Chapter 13: Overloading</a></td><td align="right" valign="top" width="172"><a href="ch13_07.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">13.6. Overloading Constants</h2><p><a name="INDEX-2637"></a><a name="INDEX-2638"></a><a name="INDEX-2639"></a>You can change how constants are interpreted by Perl with<tt class="literal">overload::constant</tt>, which is most usefully placedin a package's <tt class="literal">import</tt> method.  (If you do this, youshould properly invoke <tt class="literal">overload::remove_constant</tt> inthe package's <tt class="literal">unimport</tt> method so that the packagecan clean up after itself when you ask it to.)</p><p>Both <tt class="literal">overload::constant</tt> and<tt class="literal">overload::remove_constant</tt> expect a list ofkey/value pairs.  The keys should be any of<tt class="literal">integer</tt>, <tt class="literal">float</tt>,<tt class="literal">binary</tt>, <tt class="literal">q</tt>, and<tt class="literal">qr</tt>, and each value should be the name of asubroutine, an anonymous subroutine, or a code reference that willhandle the constants.<blockquote><pre class="programlisting">sub import { overload::constant ( integer =&gt; \&amp;integer_handler,                                  float   =&gt; \&amp;float_handler,                                  binary  =&gt; \&amp;base_handler,                                  q       =&gt; \&amp;string_handler,                                  qr      =&gt; \&amp;regex_handler ) }</pre></blockquote>Any handlers you provide for <tt class="literal">integer</tt> and<tt class="literal">float</tt> will be invoked whenever the Perl tokenerencounters a constant number.  This is independent of the <tt class="literal">useconstant</tt> pragma; simple statements such as<blockquote><pre class="programlisting">$year = cube(12) + 1;        # integer$pi   = 3.14159265358979;    # float</pre></blockquote>will trigger whatever handler you requested.</p><p>The <tt class="literal">binary</tt> key lets you intercept binary, octal,and hexadecimal constants.  <tt class="literal">q</tt> handles single-quotedstrings (including strings introduced with <tt class="literal">q</tt>) andconstant substrings within <tt class="literal">qq</tt>- and<tt class="literal">qx</tt>-quoted strings and here documents.  Finally,<tt class="literal">qr</tt> handles constant pieces within regularexpressions, as described at the end of <a href="ch05_01.htm">Chapter 5, "Pattern Matching"</a>.</p><p>The handler will be passed three arguments.  The first argument is theoriginal constant, in whatever form it was provided to Perl.  Thesecond argument is how Perl actually interpreted the constant; forinstance, <tt class="literal">123_456</tt> will appear as<tt class="literal">123456</tt>.</p><p><a name="INDEX-2640"></a>The third argument is defined only for strings handled by the<tt class="literal">q</tt> and <tt class="literal">qr</tt> handlers, and will beone of <tt class="literal">qq</tt>, <tt class="literal">q</tt>,<tt class="literal">s</tt>, or <tt class="literal">tr</tt> depending on how thestring is to be used.  <tt class="literal">qq</tt> means that the string isfrom an interpolated context, such as double quotes, backticks, an<tt class="literal">m//</tt> match, or the pattern of an<tt class="literal">s///</tt> substitution.  <tt class="literal">q</tt> means thatthe string is from an uninterpolated context, <tt class="literal">s</tt>means that the constant is a replacement string in an<tt class="literal">s///</tt> substitution, and <tt class="literal">tr</tt> meansthat it's a component of a <tt class="literal">tr///</tt> or<tt class="literal">y///</tt> expression.</p><p>The handler should return a scalar, which will be used in place of theconstant.  Often, that scalar will be a reference to an overloadedobject, but there's nothing preventing you from doing something moredastardly:<blockquote><pre class="programlisting">package DigitDoubler;    # A module to be placed in DigitDoubler.pmuse overload;sub import { overload::constant ( integer =&gt; \&amp;handler,                                  float   =&gt; \&amp;handler ) }sub handler {    my ($orig, $interp, $context) = @_;    return $interp * 2;          # double all constants}1;</pre></blockquote>Note that <tt class="literal">handler</tt> is shared by both keys, whichworks okay in this case. Now when you say:<blockquote><pre class="programlisting">use DigitDoubler;$trouble = 123;      # trouble is now 246$jeopardy = 3.21;    # jeopardy is now 6.42</pre></blockquote>you redefine the world.</p><p><a name="INDEX-2641"></a>If you intercept string constants, it is recommended that you providea concatenation operator ("<tt class="literal">.</tt>") as well, since aninterpolated expression like <tt class="literal">"ab$cd!!"</tt> is merely ashortcut for the longer<tt class="literal">'ab'&nbsp;.&nbsp;$cd&nbsp;.&nbsp;'!!'</tt>.Similarly, negative numbers are considered negations of positiveconstants, so you should provide a handler for <tt class="literal">neg</tt>when you intercept integers or floats. (We didn't need to do thatearlier, because we're returning actual numbers, not overloaded objectreferences.)</p><p>Note that <tt class="literal">overload::constant</tt> does not propagateinto run-time compilation inside <tt class="literal">eval</tt>, which can beeither a bug or a feature depending on how you look at it.</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="ch13_05.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="ch13_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">13.5. When an Overload Handler Is Missing (nomethod and fallback)</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">13.7. Public Overload Functions</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 &copy; 2001</a> O'Reilly &amp; 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 + -