perlmod.html
来自「perl教程」· HTML 代码 · 共 622 行 · 第 1/4 页
HTML
622 行
being blown out of the water by a signal--you have to trap that yourself
(if you can).) You may have multiple <code>END</code> blocks within a file--they
will execute in reverse order of definition; that is: last in, first
out (LIFO). <code>END</code> blocks are not executed when you run perl with the
<a href="../../lib/Pod/perlrun.html#item__2dc"><code>-c</code></a> switch, or if compilation fails.</p>
<p>Note that <code>END</code> code blocks are <strong>not</strong> executed at the end of a string
<a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a>: if any <code>END</code> code blocks are created in a string <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a>,
they will be executed just as any other <code>END</code> code block of that package
in LIFO order just before the interpreter is being exited.</p>
<p>Inside an <code>END</code> code block, <a href="../../lib/Pod/perlvar.html#item__"><code>$?</code></a> contains the value that the program is
going to pass to <a href="../../lib/Pod/perlfunc.html#item_exit"><code>exit()</code></a>. You can modify <a href="../../lib/Pod/perlvar.html#item__"><code>$?</code></a> to change the exit
value of the program. Beware of changing <a href="../../lib/Pod/perlvar.html#item__"><code>$?</code></a> by accident (e.g. by
running something via <a href="../../lib/Pod/perlfunc.html#item_system"><code>system</code></a>).</p>
<p><code>CHECK</code> and <code>INIT</code> code blocks are useful to catch the transition between
the compilation phase and the execution phase of the main program.</p>
<p><code>CHECK</code> code blocks are run just after the <strong>initial</strong> Perl compile phase ends
and before the run time begins, in LIFO order. <code>CHECK</code> code blocks are used
in the Perl compiler suite to save the compiled state of the program.</p>
<p><code>INIT</code> blocks are run just before the Perl runtime begins execution, in
"first in, first out" (FIFO) order. For example, the code generators
documented in <em>perlcc</em> make use of <code>INIT</code> blocks to initialize and
resolve pointers to XSUBs.</p>
<p>When you use the <strong>-n</strong> and <strong>-p</strong> switches to Perl, <code>BEGIN</code> and
<code>END</code> work just as they do in <strong>awk</strong>, as a degenerate case.
Both <code>BEGIN</code> and <code>CHECK</code> blocks are run when you use the <strong>-c</strong>
switch for a compile-only syntax check, although your main code
is not.</p>
<p>The <strong>begincheck</strong> program makes it all clear, eventually:</p>
<pre>
<span class="comment">#!/usr/bin/perl</span>
</pre>
<pre>
<span class="comment"># begincheck</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">" 8. Ordinary code runs at runtime.\n"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">END</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"14. So this is the end of the tale.\n"</span> <span class="operator">}</span>
<span class="keyword">INIT</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 5. INIT blocks run FIFO just before runtime.\n"</span> <span class="operator">}</span>
<span class="keyword">CHECK</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 4. So this is the fourth line.\n"</span> <span class="operator">}</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">" 9. It runs in order, of course.\n"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 1. BEGIN blocks run FIFO during compilation.\n"</span> <span class="operator">}</span>
<span class="keyword">END</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"13. Read perlmod for the rest of the story.\n"</span> <span class="operator">}</span>
<span class="keyword">CHECK</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 3. CHECK blocks run LIFO at compilation's end.\n"</span> <span class="operator">}</span>
<span class="keyword">INIT</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 6. Run this again, using Perl's -c switch.\n"</span> <span class="operator">}</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">"10. This is anti-obfuscated code.\n"</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">END</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">"12. END blocks run LIFO at quitting time.\n"</span> <span class="operator">}</span>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 2. So this line comes out second.\n"</span> <span class="operator">}</span>
<span class="keyword">INIT</span> <span class="operator">{</span> <span class="keyword">print</span> <span class="string">" 7. You'll see the difference right away.\n"</span> <span class="operator">}</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="string">"11. It merely _looks_ like it should be confusing.\n"</span><span class="operator">;</span>
</pre>
<pre>
__END__</pre>
<p>
</p>
<h2><a name="perl_classes">Perl Classes</a></h2>
<p>There is no special class syntax in Perl, but a package may act
as a class if it provides subroutines to act as methods. Such a
package may also derive some of its methods from another class (package)
by listing the other package <code>name(s)</code> in its global @ISA array (which
must be a package global, not a lexical).</p>
<p>For more on this, see <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a> and <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a>.</p>
<p>
</p>
<h2><a name="perl_modules">Perl Modules</a></h2>
<p>A module is just a set of related functions in a library file, i.e.,
a Perl package with the same name as the file. It is specifically
designed to be reusable by other modules or programs. It may do this
by providing a mechanism for exporting some of its symbols into the
symbol table of any package using it, or it may function as a class
definition and make its semantics available implicitly through
method calls on the class and its objects, without explicitly
exporting anything. Or it can do a little of both.</p>
<p>For example, to start a traditional, non-OO module called Some::Module,
create a file called <em>Some/Module.pm</em> and start with this template:</p>
<pre>
<span class="keyword">package</span> <span class="variable">Some::Module</span><span class="operator">;</span> <span class="comment"># assumes Some/Module.pm</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">strict</span><span class="operator">;</span>
<span class="keyword">use</span> <span class="variable">warnings</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span>
<span class="keyword">use</span> <span class="variable">Exporter</span> <span class="operator">();</span>
<span class="keyword">our</span> <span class="operator">(</span><span class="variable">$VERSION</span><span class="operator">,</span> <span class="variable">@ISA</span><span class="operator">,</span> <span class="variable">@EXPORT</span><span class="operator">,</span> <span class="variable">@EXPORT_OK</span><span class="operator">,</span> <span class="variable">%EXPORT_TAGS</span><span class="operator">);</span>
</pre>
<pre>
<span class="comment"># set the version for version checking</span>
<span class="variable">$VERSION</span> <span class="operator">=</span> <span class="number">1.00</span><span class="operator">;</span>
<span class="comment"># if using RCS/CVS, this may be preferred</span>
<span class="variable">$VERSION</span> <span class="operator">=</span> <span class="keyword">sprintf</span> <span class="string">"%d.%03d"</span><span class="operator">,</span> <span class="string">q$Revision: 1.1 $</span> <span class="operator">=~</span> <span class="regex">/(\d+)/g</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">@ISA</span> <span class="operator">=</span> <span class="string">qw(Exporter)</span><span class="operator">;</span>
<span class="variable">@EXPORT</span> <span class="operator">=</span> <span class="string">qw(&func1 &func2 &func4)</span><span class="operator">;</span>
<span class="variable">%EXPORT_TAGS</span> <span class="operator">=</span> <span class="operator">(</span> <span class="operator">);</span> <span class="comment"># eg: TAG => [ qw!name1 name2! ],</span>
</pre>
<pre>
<span class="comment"># your exported package globals go here,</span>
<span class="comment"># as well as any optionally exported functions</span>
<span class="variable">@EXPORT_OK</span> <span class="operator">=</span> <span class="string">qw($Var1 %Hashit &func3)</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="keyword">our</span> <span class="variable">@EXPORT_OK</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># exported package globals go here</span>
<span class="keyword">our</span> <span class="variable">$Var1</span><span class="operator">;</span>
<span class="keyword">our</span> <span class="variable">%Hashit</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># non-exported package globals go here</span>
<span class="keyword">our</span> <span class="variable">@more</span><span class="operator">;</span>
<span class="keyword">our</span> <span class="variable">$stuff</span><span class="operator">;</span>
</pre>
<pre>
<span class="comment"># initialize package globals, first exported ones</span>
<span class="variable">$Var1</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>
<span class="variable">%Hashit</span> <span class="operator">=</span> <span class="operator">();</span>
</pre>
<pre>
<span class="comment"># then the others (which are still accessible as $Some::Module::stuff)</span>
<span class="variable">$stuff</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>
<span class="variable">@more</span> <span class="operator">=</span> <span class="operator">();</span>
</pre>
<pre>
<span class="comment"># all file-scoped lexicals must be created before</span>
<span class="comment"># the functions below that use them.</span>
</pre>
<pre>
<span class="comment"># file-private lexicals go here</span>
<span class="keyword">my</span> <span class="variable">$priv_var</span> <span class="operator">=</span> <span class="string">''</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">%secret_hash</span> <span class="operator">=</span> <span class="operator">();</span>
</pre>
<pre>
<span class="comment"># here's a file-private function as a closure,</span>
<span class="comment"># callable as &$priv_func; it cannot be prototyped.</span>
<span class="keyword">my</span> <span class="variable">$priv_func</span> <span class="operator">=</span> <span class="keyword">sub</span><span class="variable"> </span><span class="operator">{</span>
<span class="comment"># stuff goes here.</span>
<span class="operator">};</span>
</pre>
<pre>
<span class="comment"># make all your functions, whether exported or not;</span>
<span class="comment"># remember to put something interesting in the {} stubs</span>
<span class="keyword">sub</span><span class="variable"> func1 </span><span class="operator">{}</span> <span class="comment"># no prototype</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?