📄 ch31_07.htm
字号:
<html><head><title>use constant (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 constant"><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_06.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_08.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.7. use constant</h2><blockquote><pre class="programlisting">use constant BUFFER_SIZE => 4096;use constant ONE_YEAR => 365.2425 * 24 * 60 * 60;use constant PI => 4 * atan2 1, 1;use constant DEBUGGING => 0;use constant ORACLE => 'oracle@cs.indiana.edu';use constant USERNAME => scalar getpwuid($<);use constant USERINFO => getpwuid($<);sub deg2rad { PI * $_[0] / 180 }print "This line does nothing" unless DEBUGGING;# references can be declared constantuse constant CHASH => { foo => 42 };use constant CARRAY => [ 1,2,3,4 ];use constant CPSEUDOHASH => [ { foo => 1}, 42 ];use constant CCODE => sub { "bite $_[0]\n" };print CHASH->{foo};print CARRAY->[$i];print CPSEUDOHASH->{foo};print CCODE->("me");print CHASH->[10]; # compile-time error</pre></blockquote><p>This pragma declares the named symbol to be an immutable constant<a href="#FOOTNOTE-2">[2]</a> with the given scalar or list value.You must make a separate declaration for each symbol. Values are evaluatedin list context. You may override this with <tt class="literal">scalar</tt> as we did above.</p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2] Implemented as a subroutine taking no arguments and returning thesame constant each time.</p></blockquote><p>Since these constants don't have a <tt class="literal">$</tt> on the front, you can'tinterpolate them directly into double-quotish strings, although you maydo so indirectly:<blockquote><pre class="programlisting">print "The value of PI is @{[ PI ]}.\n";</pre></blockquote>Because list constants are returned as lists, not as arrays, you mustsubscript a list-valued constant using extra parentheses as you wouldany other list expression:<blockquote><pre class="programlisting">$homedir = USERINFO[7]; # WRONG$homedir = (USERINFO)[7]; # ok</pre></blockquote>Although using all capital letters for constants is recommended to helpthem stand out and to help avoid potential collisions with other keywordsand subroutine names, this is merely a convention. Constant names mustbegin with a letter, but it need not be a capital one.</p><p>Constants are not private to the lexical scope in which they occur.Instead, they are simply argumentless subroutines in the symbol table of thepackage issuing the declaration. You may refer to a constant<em class="replaceable">CONST</em> from package <tt class="literal">Other</tt> as <tt class="literal">Other::</tt><em class="replaceable">CONST</em>. Read more aboutcompile-time inlining of such subroutines in the section "Inlining ConstantFunctions" in <a href="ch06_01.htm">Chapter 6, "Subroutines"</a>.</p><p>As with all <tt class="literal">use</tt> directives, <tt class="literal">use constant</tt> happens at compiletime. It's therefore misleading at best to place a constant declarationinside a conditional statement, such as <tt class="literal">if ($foo) { use constant ... }</tt>.</p><p>Omitting the value for a symbol gives it the value of <tt class="literal">undef</tt> inscalar context or the empty list, <tt class="literal">()</tt>, in a list context. But it is probably best to declare these explicitly:<blockquote><pre class="programlisting">use constant CAMELIDS => ();use constant CAMEL_HOME => undef;</pre></blockquote></p><h3 class="sect2">31.7.1. Restrictions on use constant</h3><p>List constants are not currently inlined the way scalar constants are.And it is not possible to have a subroutine or keyword with the same nameas a constant. This is probably a Good Thing.</p><p>You cannot declare more than one named constant at a time:<blockquote><pre class="programlisting">use constant FOO => 4, BAR => 5; # WRONG</pre></blockquote>That defines a constant named <tt class="literal">FOO</tt> whose return list is<tt class="literal">(4, "BAR", 5)</tt>. You need this instead:<blockquote><pre class="programlisting">use constant FOO => 4use constant BAR => 5;</pre></blockquote>You can get yourself into trouble if you use a constant in a contextthat automatically quotes bare names. (This is true for any subroutinecall, not just constants.) For example, you can't say <tt class="literal">$hash{</tt><em class="replaceable">CONSTANT</em><tt class="literal">}</tt>because <em class="replaceable">CONSTANT</em> will be interpreted as astring. Use <tt class="literal">$hash{</tt><em class="replaceable">CONSTANT</em><tt class="literal">()}</tt>or <tt class="literal">$hash{+</tt><em class="replaceable">CONSTANT</em><tt class="literal">}</tt>to prevent the quoting mechanism from kicking in. Similarly,since the <tt class="literal">=></tt> operator quotes its left operand if thatoperand is a bare name, you must say<em class="replaceable">CONSTANT</em><tt class="literal">() => 'value'</tt>instead of <em class="replaceable">CONSTANT</em><tt class="literal"> => 'value'</tt> .</p><p>At some point, you'll be able to use a <tt class="literal">constant</tt> attribute onvariable declarations:<blockquote><pre class="programlisting">my $PI : constant = 4 * atan2(1,1);</pre></blockquote>This has all the advantages of being a variable rather than a subroutine.It has all the disadvantages of not being implemented yet.</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_06.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_08.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.6. use charnames</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.8. use diagnostics</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 + -