perlmod.html
来自「perl教程」· HTML 代码 · 共 622 行 · 第 1/4 页
HTML
622 行
@richard and @dick as separate arrays. Tricky, eh?</p>
<p>There is one subtle difference between the following statements:</p>
<pre>
<span class="variable">*foo</span> <span class="operator">=</span> <span class="variable">*bar</span><span class="operator">;</span>
<span class="variable">*foo</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$bar</span><span class="operator">;</span>
</pre>
<p><code>*foo = *bar</code> makes the typeglobs themselves synonymous while
<code>*foo = \$bar</code> makes the SCALAR portions of two distinct typeglobs
refer to the same scalar value. This means that the following code:</p>
<pre>
<span class="variable">$bar</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
<span class="variable">*foo</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$bar</span><span class="operator">;</span> <span class="comment"># Make $foo an alias for $bar</span>
</pre>
<pre>
<span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">$bar</span> <span class="operator">=</span> <span class="number">2</span><span class="operator">;</span> <span class="comment"># Restrict changes to block</span>
<span class="keyword">print</span> <span class="variable">$foo</span><span class="operator">;</span> <span class="comment"># Prints '1'!</span>
<span class="operator">}</span>
</pre>
<p>Would print '1', because <code>$foo</code> holds a reference to the <em>original</em>
<code>$bar</code> -- the one that was stuffed away by <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> and which will be
restored when the block ends. Because variables are accessed through the
typeglob, you can use <code>*foo = *bar</code> to create an alias which can be
localized. (But be aware that this means you can't have a separate
<code>@foo</code> and <code>@bar</code>, etc.)</p>
<p>What makes all of this important is that the Exporter module uses glob
aliasing as the import/export mechanism. Whether or not you can properly
localize a variable that has been exported from a module depends on how
it was exported:</p>
<pre>
<span class="variable">@EXPORT</span> <span class="operator">=</span> <span class="string">qw($FOO)</span><span class="operator">;</span> <span class="comment"># Usual form, can't be localized</span>
<span class="variable">@EXPORT</span> <span class="operator">=</span> <span class="string">qw(*FOO)</span><span class="operator">;</span> <span class="comment"># Can be localized</span>
</pre>
<p>You can work around the first case by using the fully qualified name
(<code>$Package::FOO</code>) where you need a local value, or by overriding it
by saying <code>*FOO = *Package::FOO</code> in your script.</p>
<p>The <code>*x = \$y</code> mechanism may be used to pass and return cheap references
into or from subroutines if you don't want to copy the whole
thing. It only works when assigning to dynamic variables, not
lexicals.</p>
<pre>
<span class="variable">%some_hash</span> <span class="operator">=</span> <span class="operator">();</span> <span class="comment"># can't be my()</span>
<span class="variable">*some_hash</span> <span class="operator">=</span> <span class="variable">fn</span><span class="operator">(</span> <span class="operator">\</span><span class="variable">%another_hash</span> <span class="operator">);</span>
<span class="keyword">sub</span><span class="variable"> fn </span><span class="operator">{</span>
<span class="keyword">local</span> <span class="variable">*hashsym</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="comment"># now use %hashsym normally, and you</span>
<span class="comment"># will affect the caller's %another_hash</span>
<span class="keyword">my</span> <span class="variable">%nhash</span> <span class="operator">=</span> <span class="operator">();</span> <span class="comment"># do what you want</span>
<span class="keyword">return</span> <span class="operator">\</span><span class="variable">%nhash</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<p>On return, the reference will overwrite the hash slot in the
symbol table specified by the *some_hash typeglob. This
is a somewhat tricky way of passing around references cheaply
when you don't want to have to remember to dereference variables
explicitly.</p>
<p>Another use of symbol tables is for making "constant" scalars.</p>
<pre>
<span class="variable">*PI</span> <span class="operator">=</span> <span class="operator">\</span><span class="number">3.14159265358979</span><span class="operator">;</span>
</pre>
<p>Now you cannot alter <code>$PI</code>, which is probably a good thing all in all.
This isn't the same as a constant subroutine, which is subject to
optimization at compile-time. A constant subroutine is one prototyped
to take no arguments and to return a constant expression. See
<a href="../../lib/Pod/perlsub.html">the perlsub manpage</a> for details on these. The <code>use constant</code> pragma is a
convenient shorthand for these.</p>
<p>You can say <code>*foo{PACKAGE}</code> and <code>*foo{NAME}</code> to find out what name and
package the *foo symbol table entry comes from. This may be useful
in a subroutine that gets passed typeglobs as arguments:</p>
<pre>
<span class="keyword">sub</span><span class="variable"> identify_typeglob </span><span class="operator">{</span>
<span class="keyword">my</span> <span class="variable">$glob</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">'You gave me '</span><span class="operator">,</span> <span class="variable">*</span><span class="operator">{</span><span class="variable">$glob</span><span class="operator">}{</span><span class="string">PACKAGE</span><span class="operator">}</span><span class="operator">,</span> <span class="string">'::'</span><span class="operator">,</span> <span class="variable">*</span><span class="operator">{</span><span class="variable">$glob</span><span class="operator">}{</span><span class="string">NAME</span><span class="operator">}</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="variable">identify_typeglob</span> <span class="variable">*foo</span><span class="operator">;</span>
<span class="variable">identify_typeglob</span> <span class="variable">*bar::baz</span><span class="operator">;</span>
</pre>
<p>This prints</p>
<pre>
You gave me main::foo
You gave me bar::baz</pre>
<p>The <code>*foo{THING}</code> notation can also be used to obtain references to the
individual elements of *foo. See <a href="../../lib/Pod/perlref.html">the perlref manpage</a>.</p>
<p>Subroutine definitions (and declarations, for that matter) need
not necessarily be situated in the package whose symbol table they
occupy. You can define a subroutine outside its package by
explicitly qualifying the name of the subroutine:</p>
<pre>
<span class="keyword">package</span> <span class="variable">main</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> Some_package::foo </span><span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span> <span class="comment"># &foo defined in Some_package</span>
</pre>
<p>This is just a shorthand for a typeglob assignment at compile time:</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="variable">*Some_package::foo</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span> <span class="operator">}</span>
</pre>
<p>and is <em>not</em> the same as writing:</p>
<pre>
<span class="operator">{</span>
<span class="keyword">package</span> <span class="variable">Some_package</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> foo </span><span class="operator">{</span> <span class="operator">...</span> <span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>In the first two versions, the body of the subroutine is
lexically in the main package, <em>not</em> in Some_package. So
something like this:</p>
<pre>
<span class="keyword">package</span> <span class="variable">main</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$Some_package::name</span> <span class="operator">=</span> <span class="string">"fred"</span><span class="operator">;</span>
<span class="variable">$main::name</span> <span class="operator">=</span> <span class="string">"barney"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">sub</span><span class="variable"> Some_package::foo </span><span class="operator">{</span>
<span class="keyword">print</span> <span class="string">"in "</span><span class="operator">,</span> <span class="keyword">__PACKAGE__</span><span class="operator">,</span> <span class="string">": \$name is '$name'\n"</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">Some_package::foo</span><span class="operator">();</span>
</pre>
<p>prints:</p>
<pre>
in main: $name is 'barney'</pre>
<p>rather than:</p>
<pre>
in Some_package: $name is 'fred'</pre>
<p>This also has implications for the use of the SUPER:: qualifier
(see <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>).</p>
<p>
</p>
<h2><a name="begin__check__init_and_end">BEGIN, CHECK, INIT and END</a></h2>
<p>Four specially named code blocks are executed at the beginning and at the end
of a running Perl program. These are the <code>BEGIN</code>, <code>CHECK</code>, <code>INIT</code>, and
<code>END</code> blocks.</p>
<p>These code blocks can be prefixed with <a href="../../lib/Pod/perlfunc.html#item_sub"><code>sub</code></a> to give the appearance of a
subroutine (although this is not considered good style). One should note
that these code blocks don't really exist as named subroutines (despite
their appearance). The thing that gives this away is the fact that you can
have <strong>more than one</strong> of these code blocks in a program, and they will get
<strong>all</strong> executed at the appropriate moment. So you can't execute any of
these code blocks by name.</p>
<p>A <code>BEGIN</code> code block is executed as soon as possible, that is, the moment
it is completely defined, even before the rest of the containing file (or
string) is parsed. You may have multiple <code>BEGIN</code> blocks within a file (or
eval'ed string) -- they will execute in order of definition. Because a <code>BEGIN</code>
code block executes immediately, it can pull in definitions of subroutines
and such from other files in time to be visible to the rest of the compile
and run time. Once a <code>BEGIN</code> has run, it is immediately undefined and any
code it used is returned to Perl's memory pool.</p>
<p>It should be noted that <code>BEGIN</code> code blocks <strong>are</strong> executed inside string
<a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a>'s. The <code>CHECK</code> and <code>INIT</code> code blocks are <strong>not</strong> executed inside
a string eval, which e.g. can be a problem in a mod_perl environment.</p>
<p>An <code>END</code> code block is executed as late as possible, that is, after
perl has finished running the program and just before the interpreter
is being exited, even if it is exiting as a result of a <a href="../../lib/Pod/perlfunc.html#item_die"><code>die()</code></a> function.
(But not if it's morphing into another program via <a href="../../lib/Pod/perlfunc.html#item_exec"><code>exec</code></a>, or
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?