perlsub.html
来自「perl教程」· HTML 代码 · 共 734 行 · 第 1/5 页
HTML
734 行
an anonymous sub reference:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$secret_version</span> <span class="operator">=</span> <span class="string">'1.001-beta'</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$secret_sub</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">print</span> <span class="variable">$secret_version</span> <span class="operator">};</span>
<span class="operator">&</span><span class="variable">$secret_sub</span><span class="operator">();</span>
</pre>
<p>As long as the reference is never returned by any function within the
module, no outside module can see the subroutine, because its name is not in
any package's symbol table. Remember that it's not <em>REALLY</em> called
<code>$some_pack::secret_version</code> or anything; it's just $secret_version,
unqualified and unqualifiable.</p>
<p>This does not work with object methods, however; all object methods
have to be in the symbol table of some package to be found. See
<a href="../../lib/Pod/perlref.html#function_templates">Function Templates in the perlref manpage</a> for something of a work-around to
this.</p>
<p>
</p>
<h2><a name="persistent_private_variables">Persistent Private Variables</a></h2>
<p>Just because a lexical variable is lexically (also called statically)
scoped to its enclosing block, <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>, or <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a> FILE, this doesn't mean that
within a function it works like a C static. It normally works more
like a C auto, but with implicit garbage collection.</p>
<p>Unlike local variables in C or C++, Perl's lexical variables don't
necessarily get recycled just because their scope has exited.
If something more permanent is still aware of the lexical, it will
stick around. So long as something else references a lexical, that
lexical won't be freed--which is as it should be. You wouldn't want
memory being free until you were done using it, or kept around once you
were done. Automatic garbage collection takes care of this for you.</p>
<p>This means that you can pass back or save away references to lexical
variables, whereas to return a pointer to a C auto is a grave error.
It also gives us a way to simulate C's function statics. Here's a
mechanism for giving a function private variables with both lexical
scoping and a static lifetime. If you do want to create something like
C's static variables, just enclose the whole function in an extra block,
and put the static variable outside the function but in the block.</p>
<pre>
<span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$secret_val</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> gimme_another </span><span class="operator">{</span>
<span class="keyword">return</span> <span class="operator">++</span><span class="variable">$secret_val</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
<span class="comment"># $secret_val now becomes unreachable by the outside</span>
<span class="comment"># world, but retains its value between calls to gimme_another</span>
</pre>
<p>If this function is being sourced in from a separate file
via <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> or <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>, then this is probably just fine. If it's
all in the main program, you'll need to arrange for the <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a>
to be executed early, either by putting the whole block above
your main program, or more likely, placing merely a <code>BEGIN</code>
code block around it to make sure it gets executed before your program
starts to run:</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$secret_val</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> gimme_another </span><span class="operator">{</span>
<span class="keyword">return</span> <span class="operator">++</span><span class="variable">$secret_val</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>See <a href="../../lib/Pod/perlmod.html#begin__check__init_and_end">BEGIN, CHECK, INIT and END in the perlmod manpage</a> about the
special triggered code blocks, <code>BEGIN</code>, <code>CHECK</code>, <code>INIT</code> and <code>END</code>.</p>
<p>If declared at the outermost scope (the file scope), then lexicals
work somewhat like C's file statics. They are available to all
functions in that same file declared below them, but are inaccessible
from outside that file. This strategy is sometimes used in modules
to create private variables that the whole module can see.</p>
<p>
</p>
<h2><a name="temporary_values_via_local__">Temporary Values via <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a></a></h2>
<p><strong>WARNING</strong>: In general, you should be using <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> instead of <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a>, because
it's faster and safer. Exceptions to this include the global punctuation
variables, global filehandles and formats, and direct manipulation of the
Perl symbol table itself. <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> is mostly used when the current value
of a variable must be visible to called subroutines.</p>
<p>Synopsis:</p>
<pre>
<span class="comment"># localization of values</span>
</pre>
<pre>
<span class="keyword">local</span> <span class="variable">$foo</span><span class="operator">;</span> <span class="comment"># make $foo dynamically local</span>
<span class="keyword">local</span> <span class="operator">(</span><span class="variable">@wid</span><span class="operator">,</span> <span class="variable">%get</span><span class="operator">);</span> <span class="comment"># make list of variables local</span>
<span class="keyword">local</span> <span class="variable">$foo</span> <span class="operator">=</span> <span class="string">"flurp"</span><span class="operator">;</span> <span class="comment"># make $foo dynamic, and init it</span>
<span class="keyword">local</span> <span class="variable">@oof</span> <span class="operator">=</span> <span class="variable">@bar</span><span class="operator">;</span> <span class="comment"># make @oof dynamic, and init it</span>
</pre>
<pre>
<span class="keyword">local</span> <span class="variable">$hash</span><span class="operator">{</span><span class="string">key</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">"val"</span><span class="operator">;</span> <span class="comment"># sets a local value for this hash entry</span>
<span class="keyword">local</span> <span class="operator">(</span><span class="variable">$cond</span> <span class="operator">?</span> <span class="variable">$v1</span> <span class="operator">:</span> <span class="variable">$v2</span><span class="operator">);</span> <span class="comment"># several types of lvalues support</span>
<span class="comment"># localization</span>
</pre>
<pre>
<span class="comment"># localization of symbols</span>
</pre>
<pre>
<span class="keyword">local</span> <span class="variable">*FH</span><span class="operator">;</span> <span class="comment"># localize $FH, @FH, %FH, &FH ...</span>
<span class="keyword">local</span> <span class="variable">*merlyn</span> <span class="operator">=</span> <span class="variable">*randal</span><span class="operator">;</span> <span class="comment"># now $merlyn is really $randal, plus</span>
<span class="comment"># @merlyn is really @randal, etc</span>
<span class="keyword">local</span> <span class="variable">*merlyn</span> <span class="operator">=</span> <span class="string">'randal'</span><span class="operator">;</span> <span class="comment"># SAME THING: promote 'randal' to *randal</span>
<span class="keyword">local</span> <span class="variable">*merlyn</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$randal</span><span class="operator">;</span> <span class="comment"># just alias $merlyn, not @merlyn etc</span>
</pre>
<p>A <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> modifies its listed variables to be "local" to the
enclosing block, <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>, or <a href="../../lib/Pod/perlfunc.html#item_do"><code>do FILE</code></a>--and to <em>any subroutine
called from within that block</em>. A <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> just gives temporary
values to global (meaning package) variables. It does <em>not</em> create
a local variable. This is known as dynamic scoping. Lexical scoping
is done with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a>, which works more like C's auto declarations.</p>
<p>Some types of lvalues can be localized as well : hash and array elements
and slices, conditionals (provided that their result is always
localizable), and symbolic references. As for simple variables, this
creates new, dynamically scoped values.</p>
<p>If more than one variable or expression is given to <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a>, they must be
placed in parentheses. This operator works
by saving the current values of those variables in its argument list on a
hidden stack and restoring them upon exiting the block, subroutine, or
eval. This means that called subroutines can also reference the local
variable, but not the global one. The argument list may be assigned to if
desired, which allows you to initialize your local variables. (If no
initializer is given for a particular variable, it is created with an
undefined value.)</p>
<p>Because <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> is a run-time operator, it gets executed each time
through a loop. Consequently, it's more efficient to localize your
variables outside the loop.</p>
<p>
</p>
<h3><a name="grammatical_note_on_local__">Grammatical note on <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a></a></h3>
<p>A <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> is simply a modifier on an lvalue expression. When you assign to
a <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a>ized variable, the <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> doesn't change whether its list is viewed
as a scalar or an array. So</p>
<pre>
<span class="keyword">local</span><span class="operator">(</span><span class="variable">$foo</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">STDIN</span><span class="operator">>;</span>
<span class="keyword">local</span> <span class="variable">@FOO</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">STDIN</span><span class="operator">>;</span>
</pre>
<p>both supply a list context to the right-hand side, while</p>
<pre>
<span class="keyword">local</span> <span class="variable">$foo</span> <span class="operator">=</span> <span class="operator"><</span><span class="variable">STDIN</span><span class="operator">>;</span>
</pre>
<p>supplies a scalar context.</p>
<p>
</p>
<h3><a name="localization_of_special_variables">Localization of special variables</a></h3>
<p>If you localize a special variable, you'll be giving a new value to it,
but its magic won't go away. That means that all side-effects related
to this magic still work with the localized value.</p>
<p>This feature allows code like this to work :</p>
<pre>
<span class="comment"># Read the whole contents of FILE in $slurp</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?