perlfaq7.html

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

HTML
828 行
字号
</pre>
<p>Additionally, you can enable and disable categories of warnings.
You turn off the categories you want to ignore and you can still
get other categories of warnings.  See <a href="../../lib/Pod/perllexwarn.html">the perllexwarn manpage</a> for the
complete details, including the category names and hierarchy.</p>
<pre>
        <span class="operator">{</span>
        <span class="keyword">no</span> <span class="variable">warnings</span> <span class="string">'uninitialized'</span><span class="operator">;</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="variable">$b</span> <span class="operator">+</span> <span class="variable">$c</span><span class="operator">;</span>
        <span class="operator">}</span>
</pre>
<p>If you have an older version of Perl, the <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a> variable (documented
in <a href="../../lib/Pod/perlvar.html">the perlvar manpage</a>) controls runtime warnings for a block:</p>
<pre>
    <span class="operator">{</span>
        <span class="keyword">local</span> <span class="variable">$^W</span> <span class="operator">=</span> <span class="number">0</span><span class="operator">;</span>        <span class="comment"># temporarily turn off warnings</span>
        <span class="variable">$a</span> <span class="operator">=</span> <span class="variable">$b</span> <span class="operator">+</span> <span class="variable">$c</span><span class="operator">;</span>         <span class="comment"># I know these might be undef</span>
    <span class="operator">}</span>
</pre>
<p>Note that like all the punctuation variables, you cannot currently
use <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> on <a href="../../lib/Pod/perlvar.html#item___w"><code>$^W</code></a>, only local().</p>
<p>
</p>
<h2><a name="what_s_an_extension">What's an extension?</a></h2>
<p>An extension is a way of calling compiled C code from Perl.  Reading
<a href="../../lib/Pod/perlxstut.html">the perlxstut manpage</a> is a good place to learn more about extensions.</p>
<p>
</p>
<h2><a name="why_do_perl_operators_have_different_precedence_than_c_operators">Why do Perl operators have different precedence than C operators?</a></h2>
<p>Actually, they don't.  All C operators that Perl copies have the same
precedence in Perl as they do in C.  The problem is with operators that C
doesn't have, especially functions that give a list context to everything
on their right, eg. print, chmod, exec, and so on.  Such functions are
called &quot;list operators&quot; and appear as such in the precedence table in
<a href="../../lib/Pod/perlop.html">the perlop manpage</a>.</p>
<p>A common mistake is to write:</p>
<pre>
    <span class="keyword">unlink</span> <span class="variable">$file</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"snafu"</span><span class="operator">;</span>
</pre>
<p>This gets interpreted as:</p>
<pre>
    <span class="keyword">unlink</span> <span class="operator">(</span><span class="variable">$file</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"snafu"</span><span class="operator">);</span>
</pre>
<p>To avoid this problem, either put in extra parentheses or use the
super low precedence <code>or</code> operator:</p>
<pre>
    <span class="operator">(</span><span class="keyword">unlink</span> <span class="variable">$file</span><span class="operator">)</span> <span class="operator">||</span> <span class="keyword">die</span> <span class="string">"snafu"</span><span class="operator">;</span>
    <span class="keyword">unlink</span> <span class="variable">$file</span> <span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"snafu"</span><span class="operator">;</span>
</pre>
<p>The &quot;English&quot; operators (<code>and</code>, <code>or</code>, <code>xor</code>, and <code>not</code>)
deliberately have precedence lower than that of list operators for
just such situations as the one above.</p>
<p>Another operator with surprising precedence is exponentiation.  It
binds more tightly even than unary minus, making <code>-2**2</code> product a
negative not a positive four.  It is also right-associating, meaning
that <code>2**3**2</code> is two raised to the ninth power, not eight squared.</p>
<p>Although it has the same precedence as in C, Perl's <code>?:</code> operator
produces an lvalue.  This assigns $x to either $a or $b, depending
on the trueness of $maybe:</p>
<pre>
    <span class="operator">(</span><span class="variable">$maybe</span> <span class="operator">?</span> <span class="variable">$a</span> <span class="operator">:</span> <span class="variable">$b</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">$x</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="how_do_i_declare_create_a_structure">How do I declare/create a structure?</a></h2>
<p>In general, you don't &quot;declare&quot; a structure.  Just use a (probably
anonymous) hash reference.  See <a href="../../lib/Pod/perlref.html">the perlref manpage</a> and <a href="../../lib/Pod/perldsc.html">the perldsc manpage</a> for details.
Here's an example:</p>
<pre>
    <span class="variable">$person</span> <span class="operator">=</span> <span class="operator">{}</span><span class="operator">;</span>                   <span class="comment"># new anonymous hash</span>
    <span class="variable">$person</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">AGE</span><span class="operator">}</span>  <span class="operator">=</span> <span class="number">24</span><span class="operator">;</span>           <span class="comment"># set field AGE to 24</span>
    <span class="variable">$person</span><span class="operator">-&gt;</span><span class="operator">{</span><span class="string">NAME</span><span class="operator">}</span> <span class="operator">=</span> <span class="string">"Nat"</span><span class="operator">;</span>        <span class="comment"># set field NAME to "Nat"</span>
</pre>
<p>If you're looking for something a bit more rigorous, try <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a>.</p>
<p>
</p>
<h2><a name="how_do_i_create_a_module">How do I create a module?</a></h2>
<p>(contributed by brian d foy)</p>
<p><a href="../../lib/Pod/perlmod.html">the perlmod manpage</a>, <a href="../../lib/Pod/perlmodlib.html">the perlmodlib manpage</a>, <a href="../../lib/Pod/perlmodstyle.html">the perlmodstyle manpage</a> explain modules
in all the gory details. <a href="../../lib/Pod/perlnewmod.html">the perlnewmod manpage</a> gives a brief
overview of the process along with a couple of suggestions
about style.</p>
<p>If you need to include C code or C library interfaces in
your module, you'll need h2xs.  h2xs will create the module
distribution structure and the initial interface files
you'll need.  <a href="../../lib/Pod/perlxs.html">the perlxs manpage</a> and <a href="../../lib/Pod/perlxstut.html">the perlxstut manpage</a> explain the details.</p>
<p>If you don't need to use C code, other tools such as
ExtUtils::ModuleMaker and Module::Starter, can help you
create a skeleton module distribution.</p>
<p>You may also want to see Sam Tregar's &quot;Writing Perl Modules
for CPAN&quot; ( <a href="http://apress.com/book/bookDisplay.html?bID=14">http://apress.com/book/bookDisplay.html?bID=14</a> )
which is the best hands-on guide to creating module
distributions.</p>
<p>
</p>
<h2><a name="how_do_i_create_a_class">How do I create a class?</a></h2>
<p>See <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a> for an introduction to classes and objects, as well as
<a href="../../lib/Pod/perlobj.html">the perlobj manpage</a> and <a href="../../lib/Pod/perlbot.html">the perlbot manpage</a>.</p>
<p>
</p>
<h2><a name="how_can_i_tell_if_a_variable_is_tainted">How can I tell if a variable is tainted?</a></h2>
<p>You can use the <code>tainted()</code> function of the Scalar::Util module, available
from CPAN (or included with Perl since release 5.8.0).
See also <a href="../../lib/Pod/perlsec.html#laundering_and_detecting_tainted_data">Laundering and Detecting Tainted Data in the perlsec manpage</a>.</p>
<p>
</p>
<h2><a name="what_s_a_closure">What's a closure?</a></h2>
<p>Closures are documented in <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.</p>
<p><em>Closure</em> is a computer science term with a precise but
hard-to-explain meaning. Closures are implemented in Perl as anonymous
subroutines with lasting references to lexical variables outside their
own scopes.  These lexicals magically refer to the variables that were
around when the subroutine was defined (deep binding).</p>
<p>Closures make sense in any programming language where you can have the
return value of a function be itself a function, as you can in Perl.
Note that some languages provide anonymous functions but are not
capable of providing proper closures: the Python language, for
example.  For more information on closures, check out any textbook on
functional programming.  Scheme is a language that not only supports
but encourages closures.</p>
<p>Here's a classic function-generating function:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> add_function_generator </span><span class="operator">{</span>
      <span class="keyword">return</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">shift</span><span class="operator">()</span> <span class="operator">+</span> <span class="keyword">shift</span><span class="operator">()</span> <span class="operator">};</span>
    <span class="operator">}</span>
</pre>
<pre>
    <span class="variable">$add_sub</span> <span class="operator">=</span> <span class="variable">add_function_generator</span><span class="operator">();</span>
    <span class="variable">$sum</span> <span class="operator">=</span> <span class="variable">$add_sub</span><span class="operator">-&gt;(</span><span class="number">4</span><span class="operator">,</span><span class="number">5</span><span class="operator">);</span>                <span class="comment"># $sum is 9 now.</span>
</pre>
<p>The closure works as a <em>function template</em> with some customization
slots left out to be filled later.  The anonymous subroutine returned
by <code>add_function_generator()</code> isn't technically a closure because it
refers to no lexicals outside its own scope.</p>
<p>Contrast this with the following <code>make_adder()</code> function, in which the
returned anonymous function contains a reference to a lexical variable
outside the scope of that function itself.  Such a reference requires
that Perl return a proper closure, thus locking in for all time the
value that the lexical had when the function was created.</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> make_adder </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$addpiece</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
        <span class="keyword">return</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="keyword">shift</span><span class="operator">()</span> <span class="operator">+</span> <span class="variable">$addpiece</span> <span class="operator">};</span>
    <span class="operator">}</span>
</pre>
<pre>
    <span class="variable">$f1</span> <span class="operator">=</span> <span class="variable">make_adder</span><span class="operator">(</span><span class="number">20</span><span class="operator">);</span>
    <span class="variable">$f2</span> <span class="operator">=</span> <span class="variable">make_adder</span><span class="operator">(</span><span class="number">555</span><span class="operator">);</span>
</pre>
<p>Now <code>&amp;$f1($n)</code> is always 20 plus whatever $n you pass in, whereas
<code>&amp;$f2($n)</code> is always 555 plus whatever $n you pass in.  The $addpiece
in the closure sticks around.</p>
<p>Closures are often used for less esoteric purposes.  For example, when
you want to pass in a bit of code into a function:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$line</span><span class="operator">;</span>
    <span class="variable">timeout</span><span class="operator">(</span> <span class="number">30</span><span class="operator">,</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="variable">$line</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="operator">);</span>
</pre>
<p>If the code to execute had been passed in as a string,
<code>'$line = &lt;STDIN&gt;'</code>, there would have been no way for the
hypothetical <code>timeout()</code> function to access the lexical variable
$line back in its caller's scope.</p>
<p>
</p>
<h2><a name="what_is_variable_suicide_and_how_can_i_prevent_it">What is variable suicide and how can I prevent it?</a></h2>
<p>This problem was fixed in perl 5.004_05, so preventing it means upgrading

⌨️ 快捷键说明

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