constant.html

来自「perl教程」· HTML 代码 · 共 270 行 · 第 1/2 页

HTML
270
字号
</p>
<h2><a name="list_constants">List constants</a></h2>
<p>Constants may be lists of more (or less) than one value.  A constant
with no values evaluates to <a href="../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> in scalar context.  Note that
constants with more than one value do <em>not</em> return their last value in
scalar context as one might expect.  They currently return the number
of values, but <strong>this may change in the future</strong>.  Do not use constants
with multiple values in scalar context.</p>
<p><strong>NOTE:</strong> This implies that the expression defining the value of a
constant is evaluated in list context.  This may produce surprises:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">constant</span> <span class="string">TIMESTAMP</span> <span class="operator">=&gt;</span> <span class="keyword">localtime</span><span class="operator">;</span>                <span class="comment"># WRONG!</span>
    <span class="keyword">use</span> <span class="variable">constant</span> <span class="string">TIMESTAMP</span> <span class="operator">=&gt;</span> <span class="keyword">scalar</span> <span class="keyword">localtime</span><span class="operator">;</span>         <span class="comment"># right</span>
</pre>
<p>The first line above defines <code>TIMESTAMP</code> as a 9-element list, as
returned by <a href="../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a> in list context.  To set it to the string
returned by <a href="../lib/Pod/perlfunc.html#item_localtime"><code>localtime()</code></a> in scalar context, an explicit <a href="../lib/Pod/perlfunc.html#item_scalar"><code>scalar</code></a>
keyword is required.</p>
<p>List constants are lists, not arrays.  To index or slice them, they
must be placed in parentheses.</p>
<pre>
    <span class="keyword">my</span> <span class="variable">@workdays</span> <span class="operator">=</span> <span class="variable">WEEKDAYS</span><span class="operator">[</span><span class="number">1</span> <span class="operator">..</span> <span class="number">5</span><span class="operator">]</span><span class="operator">;</span>            <span class="comment"># WRONG!</span>
    <span class="keyword">my</span> <span class="variable">@workdays</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">WEEKDAYS</span><span class="operator">)</span><span class="operator">[</span><span class="number">1</span> <span class="operator">..</span> <span class="number">5</span><span class="operator">]</span><span class="operator">;</span>          <span class="comment"># right</span>
</pre>
<p>
</p>
<h2><a name="defining_multiple_constants_at_once">Defining multiple constants at once</a></h2>
<p>Instead of writing multiple <code>use constant</code> statements, you may define
multiple constants in a single statement by giving, instead of the
constant name, a reference to a hash where the keys are the names of
the constants to be defined.  Obviously, all constants defined using
this method must have a single value.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">constant</span> <span class="operator">{</span>
        <span class="string">FOO</span> <span class="operator">=&gt;</span> <span class="string">"A single value"</span><span class="operator">,</span>
        <span class="string">BAR</span> <span class="operator">=&gt;</span> <span class="string">"This"</span><span class="operator">,</span> <span class="string">"won't"</span><span class="operator">,</span> <span class="string">"work!"</span><span class="operator">,</span>        <span class="comment"># Error!</span>
    <span class="operator">};</span>
</pre>
<p>This is a fundamental limitation of the way hashes are constructed in
Perl.  The error messages produced when this happens will often be
quite cryptic -- in the worst case there may be none at all, and
you'll only later find that something is broken.</p>
<p>When defining multiple constants, you cannot use the values of other
constants defined in the same declaration.  This is because the
calling package doesn't know about any constant within that group
until <em>after</em> the <a href="../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement is finished.</p>
<pre>
    <span class="keyword">use</span> <span class="variable">constant</span> <span class="operator">{</span>
        <span class="string">BITMASK</span> <span class="operator">=&gt;</span> <span class="number">0xAFBAEBA8</span><span class="operator">,</span>
        <span class="string">NEGMASK</span> <span class="operator">=&gt;</span> <span class="operator">~</span><span class="variable">BITMASK</span><span class="operator">,</span>                    <span class="comment"># Error!</span>
    <span class="operator">};</span>
</pre>
<p>
</p>
<h2><a name="magic_constants">Magic constants</a></h2>
<p>Magical values and references can be made into constants at compile
time, allowing for way cool stuff like this.  (These error numbers
aren't totally portable, alas.)</p>
<pre>
    <span class="keyword">use</span> <span class="variable">constant</span> <span class="string">E2BIG</span> <span class="operator">=&gt;</span> <span class="operator">(</span><span class="variable">$!</span> <span class="operator">=</span> <span class="number">7</span><span class="operator">);</span>
    <span class="keyword">print</span>   <span class="variable">E2BIG</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>        <span class="comment"># something like "Arg list too long"</span>
    <span class="keyword">print</span> <span class="number">0</span><span class="operator">+</span><span class="variable">E2BIG</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>        <span class="comment"># "7"</span>
</pre>
<p>You can't produce a tied constant by giving a tied scalar as the
value.  References to tied variables, however, can be used as
constants without any problems.</p>
<p>
</p>
<hr />
<h1><a name="technical_notes">TECHNICAL NOTES</a></h1>
<p>In the current implementation, scalar constants are actually
inlinable subroutines. As of version 5.004 of Perl, the appropriate
scalar constant is inserted directly in place of some subroutine
calls, thereby saving the overhead of a subroutine call. See
<a href="../lib/Pod/perlsub.html#constant_functions">Constant Functions in the perlsub manpage</a> for details about how and when this
happens.</p>
<p>In the rare case in which you need to discover at run time whether a
particular constant has been declared via this module, you may use
this function to examine the hash <code>%constant::declared</code>. If the given
constant name does not include a package name, the current package is
used.</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> declared </span><span class="operator">(</span>$<span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">use</span> <span class="variable">constant</span> <span class="number">1.01</span><span class="operator">;</span>              <span class="comment"># don't omit this!</span>
        <span class="keyword">my</span> <span class="variable">$name</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
        <span class="variable">$name</span> <span class="operator">=~</span> <span class="regex">s/^::/main::/</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$pkg</span> <span class="operator">=</span> <span class="keyword">caller</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$full_name</span> <span class="operator">=</span> <span class="variable">$name</span> <span class="operator">=~</span> <span class="regex">/::/</span> <span class="operator">?</span> <span class="variable">$name</span> <span class="operator">:</span> <span class="string">"${pkg}::$name"</span><span class="operator">;</span>
        <span class="variable">$constant::declared</span><span class="operator">{</span><span class="variable">$full_name</span><span class="operator">}</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>In the current version of Perl, list constants are not inlined
and some symbols may be redefined without generating a warning.</p>
<p>It is not possible to have a subroutine or a keyword with the same
name as a constant in the same package. This is probably a Good Thing.</p>
<p>A constant with a name in the list <code>STDIN STDOUT STDERR ARGV ARGVOUT
ENV INC SIG</code> is not allowed anywhere but in package <code>main::</code>, for
technical reasons.</p>
<p>Unlike constants in some languages, these cannot be overridden
on the command line or via environment variables.</p>
<p>You can get into trouble if you use constants in a context which
automatically quotes barewords (as is true for any subroutine call).
For example, you can't say <code>$hash{CONSTANT}</code> because <code>CONSTANT</code> will
be interpreted as a string.  Use <code>$hash{CONSTANT()}</code> or
<code>$hash{+CONSTANT}</code> to prevent the bareword quoting mechanism from
kicking in.  Similarly, since the <code>=&gt;</code> operator quotes a bareword
immediately to its left, you have to say <code>CONSTANT() =&gt; 'value'</code>
(or simply use a comma in place of the big arrow) instead of
<code>CONSTANT =&gt; 'value'</code>.</p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Tom Phoenix, &lt;<em><a href="mailto:rootbeer@redcat.com">rootbeer@redcat.com</a></em>&gt;, with help from
many other folks.</p>
<p>Multiple constant declarations at once added by Casey West,
&lt;<em><a href="mailto:casey@geeknest.com">casey@geeknest.com</a></em>&gt;.</p>
<p>Documentation mostly rewritten by Ilmari Karonen,
&lt;<em><a href="mailto:perl@itz.pp.sci.fi">perl@itz.pp.sci.fi</a></em>&gt;.</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>Copyright (C) 1997, 1999 Tom Phoenix</p>
<p>This module is free software; you can redistribute it or modify it
under the same terms as Perl itself.</p>

</body>

</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?