perlfaq7.html

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

HTML
828 行
字号
        <span class="keyword">require</span> <span class="number">5.006</span><span class="operator">;</span> <span class="comment"># our() did not exist before 5.6</span>
        <span class="keyword">use</span> <span class="variable">vars</span> <span class="string">'$var'</span><span class="operator">;</span>
</pre>
<pre>
        <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>
</pre>
<pre>
        <span class="operator">{</span>
          <span class="keyword">our</span> <span class="variable">$var</span><span class="operator">;</span>
          <span class="keyword">print</span> <span class="string">"global  is $var\n"</span><span class="operator">;</span>
        <span class="operator">}</span>
</pre>
<p>
</p>
<h2><a name="what_s_the_difference_between_deep_and_shallow_binding">What's the difference between deep and shallow binding?</a></h2>
<p>In deep binding, lexical variables mentioned in anonymous subroutines
are the same ones that were in scope when the subroutine was created.
In shallow binding, they are whichever variables with the same names
happen to be in scope when the subroutine is called.  Perl always uses
deep binding of lexical variables (i.e., those created with my()).
However, dynamic variables (aka global, local, or package variables)
are effectively shallowly bound.  Consider this just one more reason
not to use them.  See the answer to <a href="#what_s_a_closure">What's a closure?</a>.</p>
<p>
</p>
<h2><a name="why_doesn_t_my__foo_____file___work_right">Why doesn't &quot;my($foo) = &lt;FILE&gt;;&quot; work right?</a></h2>
<p><a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> give list context to the right hand side
of <code>=</code>.  The &lt;FH&gt; read operation, like so many of Perl's
functions and operators, can tell which context it was called in and
behaves appropriately.  In general, the <a href="../../lib/Pod/perlfunc.html#item_scalar"><code>scalar()</code></a> function can help.
This function does nothing to the data itself (contrary to popular myth)
but rather tells its argument to behave in whatever its scalar fashion is.
If that function doesn't have a defined scalar behavior, this of course
doesn't help you (such as with sort()).</p>
<p>To enforce scalar context in this particular case, however, you need
merely omit the parentheses:</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">&lt;</span><span class="variable">FILE</span><span class="operator">&gt;;</span>           <span class="comment"># WRONG</span>
    <span class="keyword">local</span><span class="operator">(</span><span class="variable">$foo</span><span class="operator">)</span> <span class="operator">=</span> <span class="keyword">scalar</span><span class="operator">(&lt;</span><span class="variable">FILE</span><span class="operator">&gt;);</span>   <span class="comment"># ok</span>
    <span class="keyword">local</span> <span class="variable">$foo</span>  <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">FILE</span><span class="operator">&gt;;</span>           <span class="comment"># right</span>
</pre>
<p>You should probably be using lexical variables anyway, although the
issue is the same here:</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">FILE</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">FILE</span><span class="operator">&gt;;</span>  <span class="comment"># right</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_redefine_a_builtin_function__operator__or_method">How do I redefine a builtin function, operator, or method?</a></h2>
<p>Why do you want to do that? :-)</p>
<p>If you want to override a predefined function, such as open(),
then you'll have to import the new definition from a different
module.  See <a href="../../lib/Pod/perlsub.html#overriding_builtin_functions">Overriding Built-in Functions in the perlsub manpage</a>.  There's
also an example in <a href="../../lib/Pod/perltoot.html#class__template">Class::Template in the perltoot manpage</a>.</p>
<p>If you want to overload a Perl operator, such as <code>+</code> or <code>**</code>,
then you'll want to use the <code>use overload</code> pragma, documented
in <a href="../../lib/overload.html">the overload manpage</a>.</p>
<p>If you're talking about obscuring method calls in parent classes,
see <a href="../../lib/Pod/perltoot.html#overridden_methods">Overridden Methods in the perltoot manpage</a>.</p>
<p>
</p>
<h2><a name="what_s_the_difference_between_calling_a_function_as__foo_and_foo__">What's the difference between calling a function as &amp;foo and foo()?</a></h2>
<p>When you call a function as <code>&amp;foo</code>, you allow that function access to
your current @_ values, and you bypass prototypes.
The function doesn't get an empty @_--it gets yours!  While not
strictly speaking a bug (it's documented that way in <a href="../../lib/Pod/perlsub.html">the perlsub manpage</a>), it
would be hard to consider this a feature in most cases.</p>
<p>When you call your function as <code>&amp;foo()</code>, then you <em>do</em> get a new @_,
but prototyping is still circumvented.</p>
<p>Normally, you want to call a function using <code>foo()</code>.  You may only
omit the parentheses if the function is already known to the compiler
because it already saw the definition (<a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> but not <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>),
or via a forward reference or <code>use subs</code> declaration.  Even in this
case, you get a clean @_ without any of the old values leaking through
where they don't belong.</p>
<p>
</p>
<h2><a name="how_do_i_create_a_switch_or_case_statement">How do I create a switch or case statement?</a></h2>
<p>This is explained in more depth in the <a href="../../lib/Pod/perlsyn.html">the perlsyn manpage</a>.  Briefly, there's
no official case statement, because of the variety of tests possible
in Perl (numeric comparison, string comparison, glob comparison,
regex matching, overloaded comparisons, ...).
Larry couldn't decide how best to do this, so he left it out, even
though it's been on the wish list since perl1.</p>
<p>Starting from Perl 5.8 to get switch and case one can use the
Switch extension and say:</p>
<pre>
        <span class="keyword">use</span> <span class="variable">Switch</span><span class="operator">;</span>
</pre>
<p>after which one has switch and case.  It is not as fast as it could be
because it's not really part of the language (it's done using source
filters) but it is available, and it's very flexible.</p>
<p>But if one wants to use pure Perl, the general answer is to write a
construct like this:</p>
<pre>
    <span class="keyword">for</span> <span class="operator">(</span><span class="variable">$variable_to_test</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">if</span>    <span class="operator">(</span><span class="regex">/pat1/</span><span class="operator">)</span>  <span class="operator">{</span> <span class="operator">}</span>     <span class="comment"># do something</span>
        <span class="keyword">elsif</span> <span class="operator">(</span><span class="regex">/pat2/</span><span class="operator">)</span>  <span class="operator">{</span> <span class="operator">}</span>     <span class="comment"># do something else</span>
        <span class="keyword">elsif</span> <span class="operator">(</span><span class="regex">/pat3/</span><span class="operator">)</span>  <span class="operator">{</span> <span class="operator">}</span>     <span class="comment"># do something else</span>
        <span class="keyword">else</span>            <span class="operator">{</span> <span class="operator">}</span>     <span class="comment"># default</span>
    <span class="operator">}</span>
</pre>
<p>Here's a simple example of a switch based on pattern matching, this
time lined up in a way to make it look more like a switch statement.
We'll do a multiway conditional based on the type of reference stored
in $whatchamacallit:</p>
<pre>
    <span class="variable">SWITCH</span><span class="operator">:</span> <span class="keyword">for</span> <span class="operator">(</span><span class="keyword">ref</span> <span class="variable">$whatchamacallit</span><span class="operator">)</span> <span class="operator">{</span>
</pre>
<pre>
        <span class="regex">/^$/</span>            <span class="operator">&amp;&amp;</span> <span class="keyword">die</span> <span class="string">"not a reference"</span><span class="operator">;</span>
</pre>
<pre>
        <span class="regex">/SCALAR/</span>        <span class="operator">&amp;&amp;</span> <span class="keyword">do</span> <span class="operator">{</span>
                                <span class="variable">print_scalar</span><span class="operator">(</span><span class="variable">$$ref</span><span class="operator">);</span>
                                <span class="keyword">last</span> <span class="variable">SWITCH</span><span class="operator">;</span>
                        <span class="operator">};</span>
</pre>
<pre>
        <span class="regex">/ARRAY/</span>         <span class="operator">&amp;&amp;</span> <span class="keyword">do</span> <span class="operator">{</span>
                                <span class="variable">print_array</span><span class="operator">(</span><span class="variable">@$ref</span><span class="operator">);</span>
                                <span class="keyword">last</span> <span class="variable">SWITCH</span><span class="operator">;</span>
                        <span class="operator">};</span>
</pre>
<pre>
        <span class="regex">/HASH/</span>          <span class="operator">&amp;&amp;</span> <span class="keyword">do</span> <span class="operator">{</span>
                                <span class="variable">print_hash</span><span class="operator">(</span><span class="variable">%$ref</span><span class="operator">);</span>
                                <span class="keyword">last</span> <span class="variable">SWITCH</span><span class="operator">;</span>
                        <span class="operator">};</span>
</pre>
<pre>
        <span class="regex">/CODE/</span>          <span class="operator">&amp;&amp;</span> <span class="keyword">do</span> <span class="operator">{</span>
                                <span class="keyword">warn</span> <span class="string">"can't print function ref"</span><span class="operator">;</span>
                                <span class="keyword">last</span> <span class="variable">SWITCH</span><span class="operator">;</span>
                        <span class="operator">};</span>
</pre>
<pre>
        <span class="comment"># DEFAULT</span>
</pre>
<pre>
        <span class="keyword">warn</span> <span class="string">"User defined type skipped"</span><span class="operator">;</span>
</pre>
<pre>
    }</pre>
<p>See <code>perlsyn/&quot;Basic BLOCKs and Switch Statements&quot;</code> for many other
examples in this style.</p>
<p>Sometimes you should change the positions of the constant and the variable.
For example, let's say you wanted to test which of many answers you were
given, but in a case-insensitive way that also allows abbreviations.
You can use the following technique if the strings all start with
different characters or if you want to arrange the matches so that
one takes precedence over another, as <code>&quot;SEND&quot;</code> has precedence over
<code>&quot;STOP&quot;</code> here:</p>
<pre>
    <span class="keyword">chomp</span><span class="operator">(</span><span class="variable">$answer</span> <span class="operator">=</span> <span class="operator">&lt;&gt;);</span>
    <span class="keyword">if</span>    <span class="operator">(</span><span class="string">"SEND"</span>  <span class="operator">=~</span> <span class="regex">/^\Q$answer/i</span><span class="operator">)</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"Action is send\n"</span>  <span class="operator">}</span>
    <span class="keyword">elsif</span> <span class="operator">(</span><span class="string">"STOP"</span>  <span class="operator">=~</span> <span class="regex">/^\Q$answer/i</span><span class="operator">)</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"Action is stop\n"</span>  <span class="operator">}</span>
    <span class="keyword">elsif</span> <span class="operator">(</span><span class="string">"ABORT"</span> <span class="operator">=~</span> <span class="regex">/^\Q$answer/i</span><span class="operator">)</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"Action is abort\n"</span> <span class="operator">}</span>
    <span class="keyword">elsif</span> <span class="operator">(</span><span class="string">"LIST"</span>  <span class="operator">=~</span> <span class="regex">/^\Q$answer/i</span><span class="operator">)</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"Act

⌨️ 快捷键说明

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