perlmod.html

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

HTML
622
字号
<?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>perlmod - Perl 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__',2);</script>
<h1><a>perlmod - Perl modules</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

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

		<li><a href="#packages">Packages</a></li>
		<li><a href="#symbol_tables">Symbol Tables</a></li>
		<li><a href="#begin__check__init_and_end">BEGIN, CHECK, INIT and END</a></li>
		<li><a href="#perl_classes">Perl Classes</a></li>
		<li><a href="#perl_modules">Perl Modules</a></li>
		<li><a href="#making_your_module_threadsafe">Making your module threadsafe</a></li>
	</ul>

	<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlmod - Perl modules (packages and symbol tables)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>
</p>
<h2><a name="packages">Packages</a></h2>
<p>Perl provides a mechanism for alternative namespaces to protect
packages from stomping on each other's variables.  In fact, there's
really no such thing as a global variable in Perl.  The package
statement declares the compilation unit as being in the given
namespace.  The scope of the package declaration is from the
declaration itself through the end of the enclosing block, <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>,
or file, whichever comes first (the same scope as the <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> and
<a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> operators).  Unqualified dynamic identifiers will be in
this namespace, except for those few identifiers that if unqualified,
default to the main package instead of the current one as described
below.  A package statement affects only dynamic variables--including
those you've used <a href="../../lib/Pod/perlfunc.html#item_local"><code>local()</code></a> on--but <em>not</em> lexical variables created
with my().  Typically it would be the first declaration in a file
included by the <a href="../../lib/Pod/perlfunc.html#item_do"><code>do</code></a>, <a href="../../lib/Pod/perlfunc.html#item_require"><code>require</code></a>, or <a href="../../lib/Pod/perlfunc.html#item_use"><code>use</code></a> operators.  You can
switch into a package in more than one place; it merely influences
which symbol table is used by the compiler for the rest of that
block.  You can refer to variables and filehandles in other packages
by prefixing the identifier with the package name and a double
colon: <code>$Package::Variable</code>.  If the package name is null, the
<code>main</code> package is assumed.  That is, <code>$::sail</code> is equivalent to
<code>$main::sail</code>.</p>
<p>The old package delimiter was a single quote, but double colon is now the
preferred delimiter, in part because it's more readable to humans, and
in part because it's more readable to <strong>emacs</strong> macros.  It also makes C++
programmers feel like they know what's going on--as opposed to using the
single quote as separator, which was there to make Ada programmers feel
like they knew what was going on.  Because the old-fashioned syntax is still
supported for backwards compatibility, if you try to use a string like
<code>&quot;This is $owner's house&quot;</code>, you'll be accessing <code>$owner::s</code>; that is,
the $s variable in package <code>owner</code>, which is probably not what you meant.
Use braces to disambiguate, as in <code>&quot;This is ${owner}'s house&quot;</code>.</p>
<p>Packages may themselves contain package separators, as in
<code>$OUTER::INNER::var</code>.  This implies nothing about the order of
name lookups, however.  There are no relative packages: all symbols
are either local to the current package, or must be fully qualified
from the outer package name down.  For instance, there is nowhere
within package <code>OUTER</code> that <code>$INNER::var</code> refers to
<code>$OUTER::INNER::var</code>.  <code>INNER</code> refers to a totally
separate global package.</p>
<p>Only identifiers starting with letters (or underscore) are stored
in a package's symbol table.  All other symbols are kept in package
<code>main</code>, including all punctuation variables, like $_.  In addition,
when unqualified, the identifiers STDIN, STDOUT, STDERR, ARGV,
ARGVOUT, ENV, INC, and SIG are forced to be in package <code>main</code>,
even when used for other purposes than their built-in ones.  If you
have a package called <a href="../../lib/Pod/perlguts.html#item_m"><code>m</code></a>, <a href="../../lib/Pod/perlguts.html#item_s"><code>s</code></a>, or <code>y</code>, then you can't use the
qualified form of an identifier because it would be instead interpreted
as a pattern match, a substitution, or a transliteration.</p>
<p>Variables beginning with underscore used to be forced into package
main, but we decided it was more useful for package writers to be able
to use leading underscore to indicate private variables and method names.
However, variables and functions named with a single <code>_</code>, such as
$_ and <code>sub _</code>, are still forced into the package <code>main</code>.  See also
<a href="../../lib/Pod/perlvar.html#technical_note_on_the_syntax_of_variable_names">Technical Note on the Syntax of Variable Names in the perlvar manpage</a>.</p>
<p><a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval</code></a>ed strings are compiled in the package in which the <a href="../../lib/Pod/perlfunc.html#item_eval"><code>eval()</code></a> was
compiled.  (Assignments to <code>$SIG{}</code>, however, assume the signal
handler specified is in the <code>main</code> package.  Qualify the signal handler
name if you wish to have a signal handler in a package.)  For an
example, examine <em>perldb.pl</em> in the Perl library.  It initially switches
to the <code>DB</code> package so that the debugger doesn't interfere with variables
in the program you are trying to debug.  At various points, however, it
temporarily switches back to the <code>main</code> package to evaluate various
expressions in the context of the <code>main</code> package (or wherever you came
from).  See <a href="../../lib/Pod/perldebug.html">the perldebug manpage</a>.</p>
<p>The special symbol <code>__PACKAGE__</code> contains the current package, but cannot
(easily) be used to construct variable names.</p>
<p>See <a href="../../lib/Pod/perlsub.html">the perlsub manpage</a> for other scoping issues related to <a href="../../lib/Pod/perlfunc.html#item_my"><code>my()</code></a> and local(),
and <a href="../../lib/Pod/perlref.html">the perlref manpage</a> regarding closures.</p>
<p>
</p>
<h2><a name="symbol_tables">Symbol Tables</a></h2>
<p>The symbol table for a package happens to be stored in the hash of that
name with two colons appended.  The main symbol table's name is thus
<code>%main::</code>, or <code>%::</code> for short.  Likewise the symbol table for the nested
package mentioned earlier is named <code>%OUTER::INNER::</code>.</p>
<p>The value in each entry of the hash is what you are referring to when you
use the <code>*name</code> typeglob notation.  In fact, the following have the same
effect, though the first is more efficient because it does the symbol
table lookups at compile time:</p>
<pre>
    <span class="keyword">local</span> <span class="variable">*main::foo</span>    <span class="operator">=</span> <span class="variable">*main::bar</span><span class="operator">;</span>
    <span class="keyword">local</span> <span class="variable">$main::</span><span class="operator">{</span><span class="string">foo</span><span class="operator">}</span>  <span class="operator">=</span> <span class="variable">$main::</span><span class="operator">{</span><span class="string">bar</span><span class="operator">}</span><span class="operator">;</span>
</pre>
<p>(Be sure to note the <strong>vast</strong> difference between the second line above
and <code>local $main::foo = $main::bar</code>. The former is accessing the hash
<code>%main::</code>, which is the symbol table of package <code>main</code>. The latter is
simply assigning scalar <code>$bar</code> in package <code>main</code> to scalar <code>$foo</code> of
the same package.)</p>
<p>You can use this to print out all the variables in a package, for
instance.  The standard but antiquated <em>dumpvar.pl</em> library and
the CPAN module Devel::Symdump make use of this.</p>
<p>Assignment to a typeglob performs an aliasing operation, i.e.,</p>
<pre>
    <span class="variable">*dick</span> <span class="operator">=</span> <span class="variable">*richard</span><span class="operator">;</span>
</pre>
<p>causes variables, subroutines, formats, and file and directory handles
accessible via the identifier <code>richard</code> also to be accessible via the
identifier <code>dick</code>.  If you want to alias only a particular variable or
subroutine, assign a reference instead:</p>
<pre>
    <span class="variable">*dick</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$richard</span><span class="operator">;</span>
</pre>
<p>Which makes $richard and $dick the same variable, but leaves

⌨️ 快捷键说明

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