perlobj.html

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

HTML
614
字号
<?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>perlobj - Perl objects</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>perlobj - Perl objects</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="#an_object_is_simply_a_reference">An Object is Simply a Reference</a></li>
		<li><a href="#a_class_is_simply_a_package">A Class is Simply a Package</a></li>
		<li><a href="#a_method_is_simply_a_subroutine">A Method is Simply a Subroutine</a></li>
		<li><a href="#method_invocation">Method Invocation</a></li>
		<li><a href="#indirect_object_syntax">Indirect Object Syntax</a></li>
		<li><a href="#default_universal_methods">Default UNIVERSAL methods</a></li>
		<li><a href="#destructors">Destructors</a></li>
		<li><a href="#summary">Summary</a></li>
		<li><a href="#twophased_garbage_collection">Two-Phased Garbage Collection</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>perlobj - Perl objects</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>First you need to understand what references are in Perl.
See <a href="../../lib/Pod/perlref.html">the perlref manpage</a> for that.  Second, if you still find the following
reference work too complicated, a tutorial on object-oriented programming
in Perl can be found in <a href="../../lib/Pod/perltoot.html">the perltoot manpage</a> and <a href="../../lib/Pod/perltooc.html">the perltooc manpage</a>.</p>
<p>If you're still with us, then
here are three very simple definitions that you should find reassuring.</p>
<ol>
<li>
<p>An object is simply a reference that happens to know which class it
belongs to.</p>
</li>
<li>
<p>A class is simply a package that happens to provide methods to deal
with object references.</p>
</li>
<li>
<p>A method is simply a subroutine that expects an object reference (or
a package name, for class methods) as the first argument.</p>
</li>
</ol>
<p>We'll cover these points now in more depth.</p>
<p>
</p>
<h2><a name="an_object_is_simply_a_reference">An Object is Simply a Reference</a></h2>
<p>Unlike say C++, Perl doesn't provide any special syntax for
constructors.  A constructor is merely a subroutine that returns a
reference to something &quot;blessed&quot; into a class, generally the
class that the subroutine is defined in.  Here is a typical
constructor:</p>
<pre>
    <span class="keyword">package</span> <span class="variable">Critter</span><span class="operator">;</span>
    <span class="keyword">sub</span><span class="variable"> new </span><span class="operator">{</span> <span class="keyword">bless</span> <span class="operator">{}</span> <span class="operator">}</span>
</pre>
<p>That word <code>new</code> isn't special.  You could have written
a construct this way, too:</p>
<pre>
    <span class="keyword">package</span> <span class="variable">Critter</span><span class="operator">;</span>
    <span class="keyword">sub</span><span class="variable"> spawn </span><span class="operator">{</span> <span class="keyword">bless</span> <span class="operator">{}</span> <span class="operator">}</span>
</pre>
<p>This might even be preferable, because the C++ programmers won't
be tricked into thinking that <code>new</code> works in Perl as it does in C++.
It doesn't.  We recommend that you name your constructors whatever
makes sense in the context of the problem you're solving.  For example,
constructors in the Tk extension to Perl are named after the widgets
they create.</p>
<p>One thing that's different about Perl constructors compared with those in
C++ is that in Perl, they have to allocate their own memory.  (The other
things is that they don't automatically call overridden base-class
constructors.)  The <code>{}</code> allocates an anonymous hash containing no
key/value pairs, and returns it  The <a href="../../lib/Pod/perlfunc.html#item_bless"><code>bless()</code></a> takes that reference and
tells the object it references that it's now a Critter, and returns
the reference.  This is for convenience, because the referenced object
itself knows that it has been blessed, and the reference to it could
have been returned directly, like this:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> new </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$self</span> <span class="operator">=</span> <span class="operator">{}</span><span class="operator">;</span>
        <span class="keyword">bless</span> <span class="variable">$self</span><span class="operator">;</span>
        <span class="keyword">return</span> <span class="variable">$self</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>You often see such a thing in more complicated constructors
that wish to call methods in the class as part of the construction:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> new </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$self</span> <span class="operator">=</span> <span class="operator">{}</span><span class="operator">;</span>
        <span class="keyword">bless</span> <span class="variable">$self</span><span class="operator">;</span>
        <span class="variable">$self</span><span class="operator">-&gt;</span><span class="variable">initialize</span><span class="operator">();</span>
        <span class="keyword">return</span> <span class="variable">$self</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>If you care about inheritance (and you should; see
<a href="../../lib/Pod/perlmodlib.html#modules__creation__use__and_abuse">Modules: Creation, Use, and Abuse in the perlmodlib manpage</a>),
then you want to use the two-arg form of bless
so that your constructors may be inherited:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> new </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$class</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$self</span> <span class="operator">=</span> <span class="operator">{}</span><span class="operator">;</span>
        <span class="keyword">bless</span> <span class="variable">$self</span><span class="operator">,</span> <span class="variable">$class</span><span class="operator">;</span>
        <span class="variable">$self</span><span class="operator">-&gt;</span><span class="variable">initialize</span><span class="operator">();</span>
        <span class="keyword">return</span> <span class="variable">$self</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>Or if you expect people to call not just <code>CLASS-&gt;new()</code> but also
<code>$obj-&gt;new()</code>, then use something like the following.  (Note that using
this to call <code>new()</code> on an instance does not automatically perform any
copying.  If you want a shallow or deep copy of an object, you'll have to
specifically allow for that.)  The <code>initialize()</code> method used will be of
whatever $class we blessed the object into:</p>
<pre>
    <span class="keyword">sub</span><span class="variable"> new </span><span class="operator">{</span>
        <span class="keyword">my</span> <span class="variable">$this</span> <span class="operator">=</span> <span class="keyword">shift</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$class</span> <span class="operator">=</span> <span class="keyword">ref</span><span class="operator">(</span><span class="variable">$this</span><span class="operator">)</span> <span class="operator">||</span> <span class="variable">$this</span><span class="operator">;</span>
        <span class="keyword">my</span> <span class="variable">$self</span> <span class="operator">=</span> <span class="operator">{}</span><span class="operator">;</span>
        <span class="keyword">bless</span> <span class="variable">$self</span><span class="operator">,</span> <span class="variable">$class</span><span class="operator">;</span>
        <span class="variable">$self</span><span class="operator">-&gt;</span><span class="variable">initialize</span><span class="operator">();</span>
        <span class="keyword">return</span> <span class="variable">$self</span><span class="operator">;</span>
    <span class="operator">}</span>
</pre>
<p>Within the class package, the methods will typically deal with the
reference as an ordinary reference.  Outside the class package,
the reference is generally treated as an opaque value that may
be accessed only through the class's methods.</p>
<p>Although a constructor can in theory re-bless a referenced object
currently belonging to another class, this is almost certainly going
to get you into trouble.  The new class is responsible for all
cleanup later.  The previous blessing is forgotten, as an object
may belong to only one class at a time.  (Although of course it's
free to inherit methods from many classes.)  If you find yourself
having to do this, the parent class is probably misbehaving, though.</p>
<p>A clarification:  Perl objects are blessed.  References are not.  Objects
know which package they belong to.  References do not.  The <a href="../../lib/Pod/perlfunc.html#item_bless"><code>bless()</code></a>
function uses the reference to find the object.  Consider
the following example:</p>
<pre>
    <span class="variable">$a</span> <span class="operator">=</span> <span class="operator">{}</span><span class="operator">;</span>
    <span class="variable">$b</span> <span class="operator">=</span> <span class="variable">$a</span><span class="operator">;</span>
    <span class="keyword">bless</span> <span class="variable">$a</span><span class="operator">,</span> <span class="variable">BLAH</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"\$b is a "</span><span class="operator">,</span> <span class="keyword">ref</span><span class="operator">(</span><span class="variable">$b</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>This reports $b as being a BLAH, so obviously <a href="../../lib/Pod/perlfunc.html#item_bless"><code>bless()</code></a>
operated on the object and not on the reference.</p>
<p>
</p>
<h2><a name="a_class_is_simply_a_package">A Class is Simply a Package</a></h2>
<p>Unlike say C++, Perl doesn't provide any special syntax for class
definitions.  You use a package as a class by putting method
definitions into the class.</p>
<p>There is a special array within each package called @ISA, which says
where else to look for a method if you can't find it in the current
package.  This is how Perl implements inheritance.  Each element of the
@ISA array is just the name of another package that happens to be a
class package.  The classes are searched (depth first) for missing
methods in the order that they occur in @ISA.  The classes accessible
through @ISA are known as base classes of the current class.</p>
<p>All classes implicitly inherit from class <code>UNIVERSAL</code> as their
last base class.  Several commonly used methods are automatically
supplied in the UNIVERSAL class; see <a href="#default_universal_methods">Default UNIVERSAL methods</a> for
more details.</p>
<p>If a missing method is found in a base class, it is cached
in the current class for efficiency.  Changing @ISA or defining new
subroutines invalidates the cache and causes Perl to do the lookup again.</p>
<p>If neither the current class, its named base classes, nor the UNIVERSAL
class contains the requested method, these three places are searched
all over again, this time looking for a method named AUTOLOAD().  If an
AUTOLOAD is found, this method is called on behalf of the missing method,
setting the package global $AUTOLOAD to be the fully qualified name of
the method that was intended to be called.</p>
<p>If none of that works, Perl finally gives up and complains.</p>
<p>If you want to stop the AUTOLOAD inheritance say simply</p>
<pre>

⌨️ 快捷键说明

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