exporter.html

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

HTML
396
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../displayToc.js"></script>
<script language="JavaScript" src="../tocParas.js"></script>
<script language="JavaScript" src="../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../scineplex.css">
<title>Exporter - Implements default import method for modules</title>
<link rel="stylesheet" href="../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',1);</script>
<h1><a>Exporter - Implements default import method for modules</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#synopsis">SYNOPSIS</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#how_to_export">How to Export</a></li>
		<li><a href="#selecting_what_to_export">Selecting What To Export</a></li>
		<li><a href="#how_to_import">How to Import</a></li>
	</ul>

	<li><a href="#advanced_features">Advanced features</a></li>
	<ul>

		<li><a href="#specialised_import_lists">Specialised Import Lists</a></li>
		<li><a href="#exporting_without_using_exporter_s_import_method">Exporting without using Exporter's import method</a></li>
		<li><a href="#exporting_without_inheriting_from_exporter">Exporting without inheriting from Exporter</a></li>
		<li><a href="#module_version_checking">Module Version Checking</a></li>
		<li><a href="#managing_unknown_symbols">Managing Unknown Symbols</a></li>
		<li><a href="#tag_handling_utility_functions">Tag Handling Utility Functions</a></li>
		<li><a href="#generating_combined_tags">Generating combined tags</a></li>
		<li><a href="#autoloaded_constants"><code>AUTOLOAD</code>ed Constants</a></li>
	</ul>

</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>Exporter - Implements default import method for modules</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<p>In module YourModule.pm:</p>
<pre>
  <span class="keyword">package</span> <span class="variable">YourModule</span><span class="operator">;</span>
  <span class="keyword">require</span> <span class="variable">Exporter</span><span class="operator">;</span>
  <span class="variable">@ISA</span> <span class="operator">=</span> <span class="string">qw(Exporter)</span><span class="operator">;</span>
  <span class="variable">@EXPORT_OK</span> <span class="operator">=</span> <span class="string">qw(munge frobnicate)</span><span class="operator">;</span>  <span class="comment"># symbols to export on request</span>
</pre>
<p>or</p>
<pre>
  <span class="keyword">package</span> <span class="variable">YourModule</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">Exporter</span> <span class="string">'import'</span><span class="operator">;</span> <span class="comment"># gives you Exporter's import() method directly</span>
  <span class="variable">@EXPORT_OK</span> <span class="operator">=</span> <span class="string">qw(munge frobnicate)</span><span class="operator">;</span>  <span class="comment"># symbols to export on request</span>
</pre>
<p>In other files which wish to use YourModule:</p>
<pre>
  <span class="keyword">use</span> <span class="variable">ModuleName</span> <span class="string">qw(frobnicate)</span><span class="operator">;</span>      <span class="comment"># import listed symbols</span>
  <span class="variable">frobnicate</span> <span class="operator">(</span><span class="variable">$left</span><span class="operator">,</span> <span class="variable">$right</span><span class="operator">)</span>          <span class="comment"># calls YourModule::frobnicate</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The Exporter module implements an <a href="../lib/Pod/perlfunc.html#item_import"><code>import</code></a> method which allows a module
to export functions and variables to its users' namespaces. Many modules
use Exporter rather than implementing their own <a href="../lib/Pod/perlfunc.html#item_import"><code>import</code></a> method because
Exporter provides a highly flexible interface, with an implementation optimised
for the common case.</p>
<p>Perl automatically calls the <a href="../lib/Pod/perlfunc.html#item_import"><code>import</code></a> method when processing a
<a href="../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement for a module. Modules and <a href="../lib/Pod/perlfunc.html#item_use"><code>use</code></a> are documented
in <a href="../lib/Pod/perlfunc.html">the perlfunc manpage</a> and <a href="../lib/Pod/perlmod.html">the perlmod manpage</a>. Understanding the concept of
modules and how the <a href="../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement operates is important to
understanding the Exporter.</p>
<p>
</p>
<h2><a name="how_to_export">How to Export</a></h2>
<p>The arrays <code>@EXPORT</code> and <code>@EXPORT_OK</code> in a module hold lists of
symbols that are going to be exported into the users name space by
default, or which they can request to be exported, respectively.  The
symbols can represent functions, scalars, arrays, hashes, or typeglobs.
The symbols must be given by full name with the exception that the
ampersand in front of a function is optional, e.g.</p>
<pre>
    <span class="variable">@EXPORT</span>    <span class="operator">=</span> <span class="string">qw(afunc $scalar @array)</span><span class="operator">;</span>   <span class="comment"># afunc is a function</span>
    <span class="variable">@EXPORT_OK</span> <span class="operator">=</span> <span class="string">qw(&amp;bfunc %hash *typeglob)</span><span class="operator">;</span> <span class="comment"># explicit prefix on &amp;bfunc</span>
</pre>
<p>If you are only exporting function names it is recommended to omit the
ampersand, as the implementation is faster this way.</p>
<p>
</p>
<h2><a name="selecting_what_to_export">Selecting What To Export</a></h2>
<p>Do <strong>not</strong> export method names!</p>
<p>Do <strong>not</strong> export anything else by default without a good reason!</p>
<p>Exports pollute the namespace of the module user.  If you must export
try to use @EXPORT_OK in preference to @EXPORT and avoid short or
common symbol names to reduce the risk of name clashes.</p>
<p>Generally anything not exported is still accessible from outside the
module using the ModuleName::item_name (or $blessed_ref-&gt;method)
syntax.  By convention you can use a leading underscore on names to
informally indicate that they are 'internal' and not for public use.</p>
<p>(It is actually possible to get private functions by saying:</p>
<pre>
  <span class="keyword">my</span> <span class="variable">$subref</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="variable">$subref</span><span class="operator">-&gt;(</span><span class="variable">@args</span><span class="operator">);</span>            <span class="comment"># Call it as a function</span>
  <span class="variable">$obj</span><span class="operator">-&gt;</span><span class="variable">$subref</span><span class="operator">(</span><span class="variable">@args</span><span class="operator">);</span>        <span class="comment"># Use it as a method</span>
</pre>
<p>However if you use them for methods it is up to you to figure out
how to make inheritance work.)</p>
<p>As a general rule, if the module is trying to be object oriented
then export nothing. If it's just a collection of functions then
@EXPORT_OK anything but use @EXPORT with caution. For function and
method names use barewords in preference to names prefixed with
ampersands for the export lists.</p>
<p>Other module design guidelines can be found in <a href="../lib/Pod/perlmod.html">the perlmod manpage</a>.</p>
<p>
</p>
<h2><a name="how_to_import">How to Import</a></h2>
<p>In other files which wish to use your module there are three basic ways for
them to load your module and import its symbols:</p>
<dl>
<dt><strong><a name="item_use_modulename_3b"><code>use ModuleName;</code></a></strong>

<dd>
<p>This imports all the symbols from ModuleName's @EXPORT into the namespace
of the <a href="../lib/Pod/perlfunc.html#item_use"><code>use</code></a> statement.</p>
</dd>
</li>
<dt><strong><a name="item_modulename"><code>use ModuleName ();</code></a></strong>

<dd>
<p>This causes perl to load your module but does not import any symbols.</p>
</dd>
</li>
<dt><strong><a name="item_qw"><code>use ModuleName qw(...);</code></a></strong>

<dd>
<p>This imports only the symbols listed by the caller into their namespace.
All listed symbols must be in your @EXPORT or @EXPORT_OK, else an error
occurs. The advanced export features of Exporter are accessed like this,
but with list entries that are syntactically distinct from symbol names.</p>
</dd>
</li>
</dl>
<p>Unless you want to use its advanced features, this is probably all you
need to know to use Exporter.</p>
<p>
</p>
<hr />
<h1><a name="advanced_features">Advanced features</a></h1>
<p>
</p>
<h2><a name="specialised_import_lists">Specialised Import Lists</a></h2>
<p>If any of the entries in an import list begins with !, : or / then
the list is treated as a series of specifications which either add to
or delete from the list of names to import. They are processed left to
right. Specifications are in the form:</p>
<pre>
    <span class="operator">[</span><span class="operator">!</span><span class="operator">]</span><span class="variable">name</span>         <span class="variable">This</span> <span class="variable">name</span> <span class="variable">only</span>
    <span class="operator">[</span><span class="operator">!</span><span class="operator">]</span><span class="operator">:</span><span class="variable">DEFAULT</span>     <span class="variable">All</span> <span class="variable">names</span> <span class="variable">in</span> <span class="variable">@EXPORT</span>
    <span class="operator">[</span><span class="operator">!</span><span class="operator">]</span><span class="operator">:</span><span class="variable">tag</span>         <span class="variable">All</span> <span class="variable">names</span> <span class="variable">in</span> <span class="variable">$EXPORT_TAGS</span><span class="operator">{</span><span class="string">tag</span><span class="operator">}</span> <span class="variable">anonymous</span> <span class="variable">list</span>
    <span class="operator">[</span><span class="operator">!</span><span class="operator">]</span><span class="operator">/</span><span class="variable">pattern</span><span class="operator">/</span>    <span class="variable">All</span> <span class="variable">names</span> <span class="variable">in</span> <span class="variable">@EXPORT</span> <span class="keyword">and</span> <span class="variable">@EXPORT_OK</span> <span class="variable">which</span> <span class="variable">match</span>
</pre>
<p>A leading ! indicates that matching names should be deleted from the
list of names to import.  If the first specification is a deletion it
is treated as though preceded by :DEFAULT. If you just want to import
extra names in addition to the default set you will still need to
include :DEFAULT explicitly.</p>
<p>e.g., Module.pm defines:</p>
<pre>
    <span class="variable">@EXPORT</span>      <span class="operator">=</span> <span class="string">qw(A1 A2 A3 A4 A5)</span><span class="operator">;</span>
    <span class="variable">@EXPORT_OK</span>   <span class="operator">=</span> <span class="string">qw(B1 B2 B3 B4 B5)</span><span class="operator">;</span>
    <span class="variable">%EXPORT_TAGS</span> <span class="operator">=</span> <span class="operator">(</span><span class="string">T1</span> <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">qw(A1 A2 B1 B2)</span><span class="operator">]</span><span class="operator">,</span> <span class="string">T2</span> <span class="operator">=&gt;</span> <span class="operator">[</span><span class="string">qw(A1 A2 B3 B4)</span><span class="operator">]</span><span class="operator">);</span>
</pre>
<pre>
    Note that you cannot use tags in @EXPORT or @EXPORT_OK.
    Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK.</pre>
<p>An application using Module can say something like:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Module</span> <span class="string">qw(:DEFAULT :T2 !B3 A3)</span><span class="operator">;</span>

⌨️ 快捷键说明

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