perlfaq7.html
来自「perl教程」· HTML 代码 · 共 828 行 · 第 1/5 页
HTML
828 行
<p>Those preferring to be very, very clever might see the O'Reilly book,
<em>Mastering Regular Expressions</em>, by Jeffrey Friedl. Page 273's
<code>Build_MatchMany_Function()</code> is particularly interesting. A complete
citation of this book is given in <a href="../../lib/Pod/perlfaq2.html">the perlfaq2 manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_passing_methods">Passing Methods</a></strong>
<dd>
<p>To pass an object method into a subroutine, you can do this:</p>
</dd>
<dd>
<pre>
<span class="variable">call_a_lot</span><span class="operator">(</span><span class="number">10</span><span class="operator">,</span> <span class="variable">$some_obj</span><span class="operator">,</span> <span class="string">"methname"</span><span class="operator">)</span>
<span class="keyword">sub</span><span class="variable"> call_a_lot </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="operator">(</span><span class="variable">$count</span><span class="operator">,</span> <span class="variable">$widget</span><span class="operator">,</span> <span class="variable">$trick</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">@_</span><span class="operator">;</span>
<span class="keyword">for</span> <span class="operator">(</span><span class="keyword">my</span> <span class="variable">$i</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span> <span class="variable">$i</span> <span class="operator"><</span> <span class="variable">$count</span><span class="operator">;</span> <span class="variable">$i</span><span class="operator">++)</span> <span class="operator">{</span>
<span class="variable">$widget</span><span class="operator">-></span><span class="variable">$trick</span><span class="operator">();</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
</dd>
<dd>
<p>Or, you can use a closure to bundle up the object, its
method call, and arguments:</p>
</dd>
<dd>
<pre>
<span class="keyword">my</span> <span class="variable">$whatnot</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="variable">$some_obj</span><span class="operator">-></span><span class="variable">obfuscate</span><span class="operator">(</span><span class="variable">@args</span><span class="operator">)</span> <span class="operator">};</span>
<span class="variable">func</span><span class="operator">(</span><span class="variable">$whatnot</span><span class="operator">);</span>
<span class="keyword">sub</span><span class="variable"> func </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$code</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="operator">&</span><span class="variable">$code</span><span class="operator">();</span>
<span class="operator">}</span>
</pre>
</dd>
<dd>
<p>You could also investigate the <code>can()</code> method in the UNIVERSAL class
(part of the standard perl distribution).</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="how_do_i_create_a_static_variable">How do I create a static variable?</a></h2>
<p>(contributed by brian d foy)</p>
<p>Perl doesn't have "static" variables, which can only be accessed from
the function in which they are declared. You can get the same effect
with lexical variables, though.</p>
<p>You can fake a static variable by using a lexical variable which goes
out of scope. In this example, you define the subroutine <code>counter</code>, and
it uses the lexical variable <code>$count</code>. Since you wrap this in a BEGIN
block, <code>$count</code> is defined at compile-time, but also goes out of
scope at the end of the BEGIN block. The BEGIN block also ensures that
the subroutine and the value it uses is defined at compile-time so the
subroutine is ready to use just like any other subroutine, and you can
put this code in the same place as other subroutines in the program
text (i.e. at the end of the code, typically). The subroutine
<code>counter</code> still has a reference to the data, and is the only way you
can access the value (and each time you do, you increment the value).
The data in chunk of memory defined by <code>$count</code> is private to
<code>counter</code>.</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$count</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> counter </span><span class="operator">{</span> <span class="variable">$count</span><span class="operator">++</span> <span class="operator">}</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$start</span> <span class="operator">=</span> <span class="variable">count</span><span class="operator">();</span>
</pre>
<pre>
<span class="operator">....</span> <span class="comment"># code that calls count();</span>
</pre>
<pre>
<span class="keyword">my</span> <span class="variable">$end</span> <span class="operator">=</span> <span class="variable">count</span><span class="operator">();</span>
</pre>
<p>In the previous example, you created a function-private variable
because only one function remembered its reference. You could define
multiple functions while the variable is in scope, and each function
can share the "private" variable. It's not really "static" because you
can access it outside the function while the lexical variable is in
scope, and even create references to it. In this example,
<code>increment_count</code> and <code>return_count</code> share the variable. One
function adds to the value and the other simply returns the value.
They can both access <code>$count</code>, and since it has gone out of scope,
there is no other way to access it.</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$count</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> increment_count </span><span class="operator">{</span> <span class="variable">$count</span><span class="operator">++</span> <span class="operator">}</span>
<span class="keyword">sub</span><span class="variable"> return_count </span><span class="operator">{</span> <span class="variable">$count</span> <span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>To declare a file-private variable, you still use a lexical variable.
A file is also a scope, so a lexical variable defined in the file
cannot be seen from any other file.</p>
<p>See <a href="../../lib/Pod/perlsub.html#persistent_private_variables">Persistent Private Variables in the perlsub manpage</a> for more information.
The discussion of closures in <a href="../../lib/Pod/perlref.html">the perlref manpage</a> may help you even though we
did not use anonymous subroutines in this answer. See
<a href="../../lib/Pod/perlsub.html#persistent_private_variables">Persistent Private Variables in the perlsub manpage</a> for details.</p>
<p>
</p>
<h2><a name="what_s_the_difference_between_dynamic_and_lexical__static__scoping_between_local___and_my__">What's the difference between dynamic and lexical (static) scoping? Between <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> and my()?</a></h2>
<p><a href="../../lib/Pod/perlfunc.html#item_local"><code>local($x)</code></a> saves away the old value of the global variable <code>$x</code>
and assigns a new value for the duration of the subroutine <em>which is
visible in other functions called from that subroutine</em>. This is done
at run-time, so is called dynamic scoping. <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> always affects global
variables, also called package variables or dynamic variables.</p>
<p><a href="../../lib/Pod/perlfunc.html#item_my"><code>my($x)</code></a> creates a new variable that is only visible in the current
subroutine. This is done at compile-time, so it is called lexical or
static scoping. <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> always affects private variables, also called
lexical variables or (improperly) static(ly scoped) variables.</p>
<p>For instance:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> visible </span><span class="operator">{</span>
<span class="keyword">print</span> <span class="string">"var has value $var\n"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> dynamic </span><span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$var</span> <span class="operator">=</span> <span class="string">'local'</span><span class="operator">;</span> <span class="comment"># new temporary value for the still-global</span>
<span class="variable">visible</span><span class="operator">();</span> <span class="comment"># variable called $var</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> lexical </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$var</span> <span class="operator">=</span> <span class="string">'private'</span><span class="operator">;</span> <span class="comment"># new private variable, $var</span>
<span class="variable">visible</span><span class="operator">();</span> <span class="comment"># (invisible outside of sub scope)</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">$var</span> <span class="operator">=</span> <span class="string">'global'</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">visible</span><span class="operator">();</span> <span class="comment"># prints global</span>
<span class="variable">dynamic</span><span class="operator">();</span> <span class="comment"># prints local</span>
<span class="variable">lexical</span><span class="operator">();</span> <span class="comment"># prints global</span>
</pre>
<p>Notice how at no point does the value "private" get printed. That's
because $var only has that value within the block of the <code>lexical()</code>
function, and it is hidden from called subroutine.</p>
<p>In summary, <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> doesn't make what you think of as private, local
variables. It gives a global variable a temporary value. <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> is
what you're looking for if you want private variables.</p>
<p>See <a href="../../lib/Pod/perlsub.html#private_variables_via_my__">Private Variables via my() in the perlsub manpage</a> and
<a href="../../lib/Pod/perlsub.html#temporary_values_via_local__">Temporary Values via local() in the perlsub manpage</a> for excruciating details.</p>
<p>
</p>
<h2><a name="how_can_i_access_a_dynamic_variable_while_a_similarly_named_lexical_is_in_scope">How can I access a dynamic variable while a similarly named lexical is in scope?</a></h2>
<p>If you know your package, you can just mention it explicitly, as in
$Some_Pack::var. Note that the notation $::var is <strong>not</strong> the dynamic $var
in the current package, but rather the one in the "main" package, as
though you had written $main::var.</p>
<pre>
<span class="keyword">use</span> <span class="variable">vars</span> <span class="string">'$var'</span><span class="operator">;</span>
<span class="keyword">local</span> <span class="variable">$var</span> <span class="operator">=</span> <span class="string">"global"</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$var</span> <span class="operator">=</span> <span class="string">"lexical"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">"lexical is $var\n"</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"global is $main::var\n"</span><span class="operator">;</span>
</pre>
<p>Alternatively you can use the compiler directive <a href="../../lib/Pod/perlfunc.html#item_our"><code>our()</code></a> to bring a
dynamic variable into the current lexical scope.</p>
<pre>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?