perlsub.html

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

HTML
734 行
字号
scoped--magical built-ins like <a href="../../lib/Pod/perlvar.html#item___"><code>$/</code></a> must currently be <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a>ized
with <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> instead.</p>
<p>Unlike dynamic variables created by the <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a> operator, lexical
variables declared with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> are totally hidden from the outside
world, including any called subroutines.  This is true if it's the
same subroutine called from itself or elsewhere--every call gets
its own copy.</p>
<p>This doesn't mean that a <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> variable declared in a statically
enclosing lexical scope would be invisible.  Only dynamic scopes
are cut off.   For example, the <code>bumpx()</code> function below has access
to the lexical $x variable because both the <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> and the <a href="../../lib/Pod/perlfunc.html#item_sub"><code>sub</code></a>
occurred at the same scope, presumably file scope.</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="number">10</span><span class="operator">;</span>
    <span class="keyword">sub</span><span class="variable"> bumpx </span><span class="operator">{</span> <span class="variable">$x</span><span class="operator">++</span> <span class="operator">}</span>
</pre>
<p>An <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a>, however, can see lexical variables of the scope it is
being evaluated in, so long as the names aren't hidden by declarations within
the <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> itself.  See <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.</p>
<p>The parameter list to <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> may be assigned to if desired, which allows you
to initialize your variables.  (If no initializer is given for a
particular variable, it is created with the undefined value.)  Commonly
this is used to name input parameters to a subroutine.  Examples:</p>
<pre>
    <span class="variable">$arg</span> <span class="operator">=</span> <span class="string">"fred"</span><span class="operator">;</span>        <span class="comment"># "global" variable</span>
    <span class="variable">$n</span> <span class="operator">=</span> <span class="variable">cube_root</span><span class="operator">(</span><span class="number">27</span><span class="operator">);</span>
    <span class="keyword">print</span> <span class="string">"$arg thinks the root is $n\n"</span><span class="operator">;</span>
     <span class="variable">fred</span> <span class="variable">thinks</span> <span class="variable">the</span> <span class="variable">root</span> <span class="variable">is</span> <span class="number">3</span>
</pre>
<pre>
    <span class="keyword">sub</span><span class="variable"> cube_root </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$arg</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>  <span class="comment"># name doesn't matter</span>
        <span class="variable">$arg</span> <span class="operator">**=</span> <span class="number">1</span><span class="operator">/</span><span class="number">3</span><span class="operator">;</span>
        <span class="keyword">return</span> <span class="variable">$arg</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>The <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> is simply a modifier on something you might assign to.  So when
you do assign to variables in its argument list, <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> doesn't
change whether those variables are viewed as a scalar or an array.  So</p>
<pre>
    <span class="keyword">my</span> <span class="operator">(</span><span class="variable">$foo</span><span class="operator">)</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">STDIN</span><span class="operator">&gt;;</span>                <span class="comment"># WRONG?</span>
    <span class="keyword">my</span> <span class="variable">@FOO</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">STDIN</span><span class="operator">&gt;;</span>
</pre>
<p>both supply a list context to the right-hand side, while</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$foo</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">STDIN</span><span class="operator">&gt;;</span>
</pre>
<p>supplies a scalar context.  But the following declares only one variable:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$foo</span><span class="operator">,</span> <span class="variable">$bar</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>                  <span class="comment"># WRONG</span>
</pre>
<p>That has the same effect as</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$foo</span><span class="operator">;</span>
    <span class="variable">$bar</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>The declared variable is not introduced (is not visible) until after
the current statement.  Thus,</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="variable">$x</span><span class="operator">;</span>
</pre>
<p>can be used to initialize a new $x with the value of the old $x, and
the expression</p>
<pre>
    my $x = 123 and $x == 123</pre>
<p>is false unless the old $x happened to have the value <code>123</code>.</p>
<p>Lexical scopes of control structures are not bounded precisely by the
braces that delimit their controlled blocks; control expressions are
part of that scope, too.  Thus in the loop</p>
<pre>
    <span class="keyword">while</span> <span class="operator">(</span><span class="keyword">my</span> <span class="variable">$line</span> <span class="operator">=</span> <span class="operator">&lt;&gt;)</span> <span class="operator">{</span>
        <span class="variable">$line</span> <span class="operator">=</span> <span class="keyword">lc</span> <span class="variable">$line</span><span class="operator">;</span>
    <span class="operator">}</span> <span class="keyword">continue</span> <span class="operator">{</span>
        <span class="keyword">print</span> <span class="variable">$line</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>the scope of $line extends from its declaration throughout the rest of
the loop construct (including the <a href="../../lib/Pod/perlfunc.html#item_continue"><code>continue</code></a> clause), but not beyond
it.  Similarly, in the conditional</p>
<pre>
    <span class="keyword">if</span> <span class="operator">((</span><span class="keyword">my</span> <span class="variable">$answer</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">STDIN</span><span class="operator">&gt;)</span> <span class="operator">=~</span> <span class="regex">/^yes$/i</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="variable">user_agrees</span><span class="operator">();</span>
    <span class="operator">}</span> <span class="keyword">elsif</span> <span class="operator">(</span><span class="variable">$answer</span> <span class="operator">=~</span> <span class="regex">/^no$/i</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="variable">user_disagrees</span><span class="operator">();</span>
    <span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
        <span class="keyword">chomp</span> <span class="variable">$answer</span><span class="operator">;</span>
        <span class="keyword">die</span> <span class="string">"'$answer' is neither 'yes' nor 'no'"</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>the scope of $answer extends from its declaration through the rest
of that conditional, including any <code>elsif</code> and <code>else</code> clauses, 
but not beyond it.  See <a href="../../lib/Pod/perlsyn.html#simple_statements">Simple statements in the perlsyn manpage</a> for information
on the scope of variables in statements with modifiers.</p>
<p>The <code>foreach</code> loop defaults to scoping its index variable dynamically
in the manner of <a href="../../lib/Pod/perlfunc.html#item_local"><code>local</code></a>.  However, if the index variable is
prefixed with the keyword <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a>, or if there is already a lexical
by that name in scope, then a new lexical is created instead.  Thus
in the loop</p>
<pre>
    <span class="keyword">for</span> <span class="keyword">my</span> <span class="variable">$i</span> <span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">3</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="variable">some_function</span><span class="operator">();</span>
    <span class="operator">}</span>
</pre>
<p>the scope of $i extends to the end of the loop, but not beyond it,
rendering the value of $i inaccessible within <code>some_function()</code>.</p>
<p>Some users may wish to encourage the use of lexically scoped variables.
As an aid to catching implicit uses to package variables,
which are always global, if you say</p>
<pre>
    <span class="keyword">use</span> <span class="variable">strict</span> <span class="string">'vars'</span><span class="operator">;</span>
</pre>
<p>then any variable mentioned from there to the end of the enclosing
block must either refer to a lexical variable, be predeclared via
<a href="../../lib/Pod/perlfunc.html#item_our"><code>our</code></a> or <code>use vars</code>, or else must be fully qualified with the package name.
A compilation error results otherwise.  An inner block may countermand
this with <code>no strict 'vars'</code>.</p>
<p>A <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> has both a compile-time and a run-time effect.  At compile
time, the compiler takes notice of it.  The principal usefulness
of this is to quiet <code>use strict 'vars'</code>, but it is also essential
for generation of closures as detailed in <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.  Actual
initialization is delayed until run time, though, so it gets executed
at the appropriate time, such as each time through a loop, for
example.</p>
<p>Variables declared with <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> are not part of any package and are therefore
never fully qualified with the package name.  In particular, you're not
allowed to try to make a package variable (or other global) lexical:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$pack::var</span><span class="operator">;</span>      <span class="comment"># ERROR!  Illegal syntax</span>
    <span class="keyword">my</span> <span class="variable">$_</span><span class="operator">;</span>              <span class="comment"># also illegal (currently)</span>
</pre>
<p>In fact, a dynamic variable (also known as package or global variables)
are still accessible using the fully qualified <code>::</code> notation even while a
lexical of the same name is also visible:</p>
<pre>
    <span class="keyword">package</span> <span class="variable">main</span><span class="operator">;</span>
    <span class="keyword">local</span> <span class="variable">$x</span> <span class="operator">=</span> <span class="number">10</span><span class="operator">;</span>
    <span class="keyword">my</span>    <span class="variable">$x</span> <span class="operator">=</span> <span class="number">20</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"$x and $::x\n"</span><span class="operator">;</span>
</pre>
<p>That will print out <code>20</code> and <code>10</code>.</p>
<p>You may declare <a href="../../lib/Pod/perlfunc.html#item_my"><code>my</code></a> variables at the outermost scope of a file
to hide any such identifiers from the world outside that file.  This
is similar in spirit to C's static variables when they are used at
the file level.  To do this with a subroutine requires the use of
a closure (an anonymous function that accesses enclosing lexicals).
If you want to create a private subroutine that cannot be called
from outside that block, it can declare a lexical variable containing

⌨️ 快捷键说明

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