perlmod.html
来自「perl教程」· HTML 代码 · 共 622 行 · 第 1/4 页
HTML
622 行
<span class="keyword">sub</span><span class="variable"> func2</span><span class="operator">()</span> <span class="operator">{}</span> <span class="comment"># proto'd void</span>
<span class="keyword">sub</span><span class="variable"> func3</span><span class="operator">(</span>$$<span class="operator">)</span> <span class="operator">{}</span> <span class="comment"># proto'd to 2 scalars</span>
</pre>
<pre>
<span class="comment"># this one isn't exported, but could be called!</span>
<span class="keyword">sub</span><span class="variable"> func4</span><span class="operator">(</span>\%<span class="operator">)</span> <span class="operator">{}</span> <span class="comment"># proto'd to 1 hash ref</span>
</pre>
<pre>
<span class="keyword">END</span> <span class="operator">{</span> <span class="operator">}</span> <span class="comment"># module clean-up code here (global destructor)</span>
</pre>
<pre>
<span class="comment">## YOUR CODE GOES HERE</span>
</pre>
<pre>
<span class="number">1</span><span class="operator">;</span> <span class="comment"># don't forget to return a true value from the file</span>
</pre>
<p>Then go on to declare and use your variables in functions without
any qualifications. See <a href="../../lib/Exporter.html">the Exporter manpage</a> and the <a href="../../lib/Pod/perlmodlib.html">the perlmodlib manpage</a> for
details on mechanics and style issues in module creation.</p>
<p>Perl modules are included into your program by saying</p>
<pre>
<span class="keyword">use</span> <span class="variable">Module</span><span class="operator">;</span>
</pre>
<p>or</p>
<pre>
<span class="keyword">use</span> <span class="variable">Module</span> <span class="variable">LIST</span><span class="operator">;</span>
</pre>
<p>This is exactly equivalent to</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="keyword">require</span> <span class="variable">Module</span><span class="operator">;</span> <span class="variable">import</span> <span class="variable">Module</span><span class="operator">;</span> <span class="operator">}</span>
</pre>
<p>or</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="keyword">require</span> <span class="variable">Module</span><span class="operator">;</span> <span class="variable">import</span> <span class="variable">Module</span> <span class="variable">LIST</span><span class="operator">;</span> <span class="operator">}</span>
</pre>
<p>As a special case</p>
<pre>
<span class="keyword">use</span> <span class="variable">Module</span> <span class="operator">();</span>
</pre>
<p>is exactly equivalent to</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span> <span class="keyword">require</span> <span class="variable">Module</span><span class="operator">;</span> <span class="operator">}</span>
</pre>
<p>All Perl module files have the extension <em>.pm</em>. The <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> operator
assumes this so you don't have to spell out "<em>Module.pm</em>" in quotes.
This also helps to differentiate new modules from old <em>.pl</em> and
<em>.ph</em> files. Module names are also capitalized unless they're
functioning as pragmas; pragmas are in effect compiler directives,
and are sometimes called "pragmatic modules" (or even "pragmata"
if you're a classicist).</p>
<p>The two statements:</p>
<pre>
<span class="keyword">require</span> <span class="variable">SomeModule</span><span class="operator">;</span>
<span class="keyword">require</span> <span class="string">"SomeModule.pm"</span><span class="operator">;</span>
</pre>
<p>differ from each other in two ways. In the first case, any double
colons in the module name, such as <code>Some::Module</code>, are translated
into your system's directory separator, usually "/". The second
case does not, and would have to be specified literally. The other
difference is that seeing the first <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> clues in the compiler
that uses of indirect object notation involving "SomeModule", as
in <code>$ob = purge SomeModule</code>, are method calls, not function calls.
(Yes, this really can make a difference.)</p>
<p>Because the <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement implies a <code>BEGIN</code> block, the importing
of semantics happens as soon as the <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement is compiled,
before the rest of the file is compiled. This is how it is able
to function as a pragma mechanism, and also how modules are able to
declare subroutines that are then visible as list or unary operators for
the rest of the current file. This will not work if you use <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>
instead of <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a>. With <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> you can get into this problem:</p>
<pre>
<span class="keyword">require</span> <span class="variable">Cwd</span><span class="operator">;</span> <span class="comment"># make Cwd:: accessible</span>
<span class="variable">$here</span> <span class="operator">=</span> <span class="variable">Cwd::getcwd</span><span class="operator">();</span>
</pre>
<pre>
<span class="keyword">use</span> <span class="variable">Cwd</span><span class="operator">;</span> <span class="comment"># import names from Cwd::</span>
<span class="variable">$here</span> <span class="operator">=</span> <span class="variable">getcwd</span><span class="operator">();</span>
</pre>
<pre>
<span class="keyword">require</span> <span class="variable">Cwd</span><span class="operator">;</span> <span class="comment"># make Cwd:: accessible</span>
<span class="variable">$here</span> <span class="operator">=</span> <span class="variable">getcwd</span><span class="operator">();</span> <span class="comment"># oops! no main::getcwd()</span>
</pre>
<p>In general, <code>use Module ()</code> is recommended over <a href="../../lib/Pod/perlfunc.html#item_require"><code>require Module</code></a>,
because it determines module availability at compile time, not in the
middle of your program's execution. An exception would be if two modules
each tried to <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> each other, and each also called a function from
that other module. In that case, it's easy to use <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a> instead.</p>
<p>Perl packages may be nested inside other package names, so we can have
package names containing <code>::</code>. But if we used that package name
directly as a filename it would make for unwieldy or impossible
filenames on some systems. Therefore, if a module's name is, say,
<code>Text::Soundex</code>, then its definition is actually found in the library
file <em>Text/Soundex.pm</em>.</p>
<p>Perl modules always have a <em>.pm</em> file, but there may also be
dynamically linked executables (often ending in <em>.so</em>) or autoloaded
subroutine definitions (often ending in <em>.al</em>) associated with the
module. If so, these will be entirely transparent to the user of
the module. It is the responsibility of the <em>.pm</em> file to load
(or arrange to autoload) any additional functionality. For example,
although the POSIX module happens to do both dynamic loading and
autoloading, the user can say just <a href="../../lib/Pod/perlfunc.html#item_use"><code>use POSIX</code></a> to get it all.</p>
<p>
</p>
<h2><a name="making_your_module_threadsafe">Making your module threadsafe</a></h2>
<p>Since 5.6.0, Perl has had support for a new type of threads called
interpreter threads (ithreads). These threads can be used explicitly
and implicitly.</p>
<p>Ithreads work by cloning the data tree so that no data is shared
between different threads. These threads can be used by using the <code>threads</code>
module or by doing <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> on win32 (fake <a href="../../lib/Pod/perlfunc.html#item_fork"><code>fork()</code></a> support). When a
thread is cloned all Perl data is cloned, however non-Perl data cannot
be cloned automatically. Perl after 5.7.2 has support for the <code>CLONE</code>
special subroutine. In <code>CLONE</code> you can do whatever
you need to do,
like for example handle the cloning of non-Perl data, if necessary.
<code>CLONE</code> will be called once as a class method for every package that has it
defined (or inherits it). It will be called in the context of the new thread,
so all modifications are made in the new area. Currently CLONE is called with
no parameters other than the invocant package name, but code should not assume
that this will remain unchanged, as it is likely that in future extra parameters
will be passed in to give more information about the state of cloning.</p>
<p>If you want to CLONE all objects you will need to keep track of them per
package. This is simply done using a hash and Scalar::Util::weaken().</p>
<p>Perl after 5.8.7 has support for the <code>CLONE_SKIP</code> special subroutine.
Like <code>CLONE</code>, <code>CLONE_SKIP</code> is called once per package; however, it is
called just before cloning starts, and in the context of the parent
thread. If it returns a true value, then no objects of that class will
be cloned; or rather, they will be copied as unblessed, undef values.
This provides a simple mechanism for making a module threadsafe; just add
<code>sub CLONE_SKIP { 1 }</code> at the top of the class, and <code>DESTROY()</code> will be
now only be called once per object. Of course, if the child thread needs
to make use of the objects, then a more sophisticated approach is
needed.</p>
<p>Like <code>CLONE</code>, <code>CLONE_SKIP</code> is currently called with no parameters other
than the invocant package name, although that may change. Similarly, to
allow for future expansion, the return value should be a single <code>0</code> or
<code>1</code> value.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>See <a href="../../lib/Pod/perlmodlib.html">the perlmodlib manpage</a> for general style issues related to building Perl
modules and classes, as well as descriptions of the standard library
and CPAN, <a href="../../lib/Exporter.html">the Exporter manpage</a> for how Perl's standard import/export mechanism
works, <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a> and <a href="../../lib/Pod/perltooc.html">the perltooc manpage</a> for an in-depth tutorial on
creating classes, <a href="../../lib/Pod/perlobj.html">the perlobj manpage</a> for a hard-core reference document on
objects, <a href="../../lib/Pod/perlsub.html">the perlsub manpage</a> for an explanation of functions and scoping,
and <a href="../../lib/Pod/perlxstut.html">the perlxstut manpage</a> and <a href="../../lib/Pod/perlguts.html">the perlguts manpage</a> for more information on writing
extension modules.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?