attributes.html
来自「perl教程」· HTML 代码 · 共 385 行 · 第 1/2 页
HTML
385 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../displayToc.js"></script>
<script language="JavaScript" src="../tocParas.js"></script>
<script language="JavaScript" src="../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../scineplex.css">
<title>attributes - get/set subroutine or variable attributes</title>
<link rel="stylesheet" href="../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',1);</script>
<h1><a>attributes - get/set subroutine or variable attributes</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#builtin_attributes">Built-in Attributes</a></li>
<li><a href="#available_subroutines">Available Subroutines</a></li>
<li><a href="#packagespecific_attribute_handling">Package-specific Attribute Handling</a></li>
<li><a href="#syntax_of_attribute_lists">Syntax of Attribute Lists</a></li>
</ul>
<li><a href="#exports">EXPORTS</a></li>
<ul>
<li><a href="#default_exports">Default exports</a></li>
<li><a href="#available_exports">Available exports</a></li>
<li><a href="#export_tags_defined">Export tags defined</a></li>
</ul>
<li><a href="#examples">EXAMPLES</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>attributes - get/set subroutine or variable attributes</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">sub</span><span class="variable"> foo : method </span><span class="operator">;</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$x</span><span class="operator">,</span><span class="variable">@y</span><span class="operator">,</span><span class="variable">%z</span><span class="operator">)</span> <span class="operator">:</span> <span class="variable">Bent</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$s</span> <span class="operator">=</span> <span class="variable">sub</span> <span class="operator">:</span> <span class="variable">method</span> <span class="operator">{</span> <span class="operator">...</span> <span class="operator">};</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">attributes</span> <span class="operator">();</span> <span class="comment"># optional, to get subroutine declarations</span>
<span class="keyword">my</span> <span class="variable">@attrlist</span> <span class="operator">=</span> <span class="variable">attributes::get</span><span class="operator">(\&</span><span class="variable">foo</span><span class="operator">);</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">attributes</span> <span class="string">'get'</span><span class="operator">;</span> <span class="comment"># import the attributes::get subroutine</span>
<span class="keyword">my</span> <span class="variable">@attrlist</span> <span class="operator">=</span> <span class="variable">get</span> <span class="operator">\&</span><span class="variable">foo</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Subroutine declarations and definitions may optionally have attribute lists
associated with them. (Variable <a href="../lib/Pod/perlfunc.html#item_my"><code>my</code></a> declarations also may, but see the
warning below.) Perl handles these declarations by passing some information
about the call site and the thing being declared along with the attribute
list to this module. In particular, the first example above is equivalent to
the following:</p>
<pre>
<span class="keyword">use</span> <span class="variable">attributes</span> <span class="keyword">__PACKAGE__</span><span class="operator">,</span> <span class="operator">\&</span><span class="variable">foo</span><span class="operator">,</span> <span class="string">'method'</span><span class="operator">;</span>
</pre>
<p>The second example in the synopsis does something equivalent to this:</p>
<pre>
<span class="keyword">use</span> <span class="variable">attributes</span> <span class="operator">();</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$x</span><span class="operator">,</span><span class="variable">@y</span><span class="operator">,</span><span class="variable">%z</span><span class="operator">);</span>
<span class="variable">attributes:</span>:-<span class="operator">></span><span class="variable">import</span><span class="operator">(</span><span class="keyword">__PACKAGE__</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">$x</span><span class="operator">,</span> <span class="string">'Bent'</span><span class="operator">);</span>
<span class="variable">attributes:</span>:-<span class="operator">></span><span class="variable">import</span><span class="operator">(</span><span class="keyword">__PACKAGE__</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">@y</span><span class="operator">,</span> <span class="string">'Bent'</span><span class="operator">);</span>
<span class="variable">attributes:</span>:-<span class="operator">></span><span class="variable">import</span><span class="operator">(</span><span class="keyword">__PACKAGE__</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">%z</span><span class="operator">,</span> <span class="string">'Bent'</span><span class="operator">);</span>
<span class="operator">(</span><span class="variable">$x</span><span class="operator">,</span><span class="variable">@y</span><span class="operator">,</span><span class="variable">%z</span><span class="operator">)</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>Yes, that's a lot of expansion.</p>
<p><strong>WARNING</strong>: attribute declarations for variables are still evolving.
The semantics and interfaces of such declarations could change in
future versions. They are present for purposes of experimentation
with what the semantics ought to be. Do not rely on the current
implementation of this feature.</p>
<p>There are only a few attributes currently handled by Perl itself (or
directly by this module, depending on how you look at it.) However,
package-specific attributes are allowed by an extension mechanism.
(See <a href="#packagespecific_attribute_handling">Package-specific Attribute Handling</a> below.)</p>
<p>The setting of subroutine attributes happens at compile time.
Variable attributes in <a href="../lib/Pod/perlfunc.html#item_our"><code>our</code></a> declarations are also applied at compile time.
However, <a href="../lib/Pod/perlfunc.html#item_my"><code>my</code></a> variables get their attributes applied at run-time.
This means that you have to <em>reach</em> the run-time component of the <a href="../lib/Pod/perlfunc.html#item_my"><code>my</code></a>
before those attributes will get applied. For example:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">:</span> <span class="variable">Bent</span> <span class="operator">=</span> <span class="number">42</span> <span class="keyword">if</span> <span class="number">0</span><span class="operator">;</span>
</pre>
<p>will neither assign 42 to $x <em>nor</em> will it apply the <code>Bent</code> attribute
to the variable.</p>
<p>An attempt to set an unrecognized attribute is a fatal error. (The
error is trappable, but it still stops the compilation within that
<a href="../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>.) Setting an attribute with a name that's all lowercase
letters that's not a built-in attribute (such as "foo") will result in
a warning with <strong>-w</strong> or <code>use warnings 'reserved'</code>.</p>
<p>
</p>
<h2><a name="builtin_attributes">Built-in Attributes</a></h2>
<p>The following are the built-in attributes for subroutines:</p>
<dl>
<dt><strong><a name="item_locked">locked</a></strong>
<dd>
<p><strong>5.005 threads only! The use of the "locked" attribute currently
only makes sense if you are using the deprecated "Perl 5.005 threads"
implementation of threads.</strong></p>
</dd>
<dd>
<p>Setting this attribute is only meaningful when the subroutine or
method is to be called by multiple threads. When set on a method
subroutine (i.e., one marked with the <strong>method</strong> attribute below),
Perl ensures that any invocation of it implicitly locks its first
argument before execution. When set on a non-method subroutine,
Perl ensures that a lock is taken on the subroutine itself before
execution. The semantics of the lock are exactly those of one
explicitly taken with the <a href="../lib/Pod/perlfunc.html#item_lock"><code>lock</code></a> operator immediately after the
subroutine is entered.</p>
</dd>
</li>
<dt><strong><a name="item_method">method</a></strong>
<dd>
<p>Indicates that the referenced subroutine is a method.
This has a meaning when taken together with the <strong>locked</strong> attribute,
as described there. It also means that a subroutine so marked
will not trigger the "Ambiguous call resolved as CORE::%s" warning.</p>
</dd>
</li>
<dt><strong><a name="item_lvalue">lvalue</a></strong>
<dd>
<p>Indicates that the referenced subroutine is a valid lvalue and can
be assigned to. The subroutine must return a modifiable value such
as a scalar variable, as described in <a href="../lib/Pod/perlsub.html">the perlsub manpage</a>.</p>
</dd>
</li>
</dl>
<p>For global variables there is <code>unique</code> attribute: see <a href="../lib/Pod/perlfunc.html#our">our in the perlfunc manpage</a>.</p>
<p>
</p>
<h2><a name="available_subroutines">Available Subroutines</a></h2>
<p>The following subroutines are available for general use once this module
has been loaded:</p>
<dl>
<dt><strong><a name="item_get">get</a></strong>
<dd>
<p>This routine expects a single parameter--a reference to a
subroutine or variable. It returns a list of attributes, which may be
empty. If passed invalid arguments, it uses <a href="../lib/Pod/perlfunc.html#item_die"><code>die()</code></a> (via <a href="../lib/Carp.html">Carp::croak</a>)
to raise a fatal exception. If it can find an appropriate package name
for a class method lookup, it will include the results from a
<a href="#item_fetch_type_attributes"><code>FETCH_type_ATTRIBUTES</code></a> call in its return list, as described in
<a href="#packagespecific_attribute_handling">Package-specific Attribute Handling</a> below.
Otherwise, only <a href="#builtin_attributes">built-in attributes</a> will be returned.</p>
</dd>
</li>
<dt><strong><a name="item_reftype">reftype</a></strong>
<dd>
<p>This routine expects a single parameter--a reference to a subroutine or
variable. It returns the built-in type of the referenced variable,
ignoring any package into which it might have been blessed.
This can be useful for determining the <em>type</em> value which forms part of
the method names described in <a href="#packagespecific_attribute_handling">Package-specific Attribute Handling</a> below.</p>
</dd>
</li>
</dl>
<p>Note that these routines are <em>not</em> exported by default.</p>
<p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?