perlboot.html
来自「perl教程」· HTML 代码 · 共 851 行 · 第 1/5 页
HTML
851 行
<pre>
a Mouse goes squeak!
[but you can barely hear it!]</pre>
<p>Here, <code>Mouse</code> has its own speaking routine, so <code>Mouse->speak</code>
doesn't immediately invoke <code>Animal->speak</code>. This is known as
"overriding". In fact, we didn't even need to say that a <code>Mouse</code> was
an <code>Animal</code> at all, since all of the methods needed for <code>speak</code> are
completely defined with <code>Mouse</code>.</p>
<p>But we've now duplicated some of the code from <code>Animal->speak</code>,
and this can once again be a maintenance headache. So, can we avoid
that? Can we say somehow that a <code>Mouse</code> does everything any other
<code>Animal</code> does, but add in the extra comment? Sure!</p>
<p>First, we can invoke the <code>Animal::speak</code> method directly:</p>
<pre>
<span class="comment"># Animal package from before</span>
<span class="operator">{</span> <span class="keyword">package</span> <span class="variable">Mouse</span><span class="operator">;</span>
<span class="variable">@ISA</span> <span class="operator">=</span> <span class="string">qw(Animal)</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> sound </span><span class="operator">{</span> <span class="string">"squeak"</span> <span class="operator">}</span>
<span class="keyword">sub</span><span class="variable"> speak </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="variable">Animal::speak</span><span class="operator">(</span><span class="variable">$class</span><span class="operator">);</span>
<span class="keyword">print</span> <span class="string">"[but you can barely hear it!]\n"</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>Note that we have to include the <code>$class</code> parameter (almost surely
the value of <code>"Mouse"</code>) as the first parameter to <code>Animal::speak</code>,
since we've stopped using the method arrow. Why did we stop? Well,
if we invoke <code>Animal->speak</code> there, the first parameter to the
method will be <code>"Animal"</code> not <code>"Mouse"</code>, and when time comes for it
to call for the <code>sound</code>, it won't have the right class to come back
to this package.</p>
<p>Invoking <code>Animal::speak</code> directly is a mess, however. What if
<code>Animal::speak</code> didn't exist before, and was being inherited from a
class mentioned in <code>@Animal::ISA</code>? Because we are no longer using
the method arrow, we get one and only one chance to hit the right
subroutine.</p>
<p>Also note that the <code>Animal</code> classname is now hardwired into the
subroutine selection. This is a mess if someone maintains the code,
changing <code>@ISA</code> for <Mouse> and didn't notice <code>Animal</code> there in
<code>speak</code>. So, this is probably not the right way to go.</p>
<p>
</p>
<h2><a name="starting_the_search_from_a_different_place">Starting the search from a different place</a></h2>
<p>A better solution is to tell Perl to search from a higher place
in the inheritance chain:</p>
<pre>
<span class="comment"># same Animal as before</span>
<span class="operator">{</span> <span class="keyword">package</span> <span class="variable">Mouse</span><span class="operator">;</span>
<span class="comment"># same @ISA, &sound as before</span>
<span class="keyword">sub</span><span class="variable"> speak </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="variable">$class</span><span class="operator">-></span><span class="variable">Animal::speak</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"[but you can barely hear it!]\n"</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>Ahh. This works. Using this syntax, we start with <code>Animal</code> to find
<code>speak</code>, and use all of <code>Animal</code>'s inheritance chain if not found
immediately. And yet the first parameter will be <code>$class</code>, so the
found <code>speak</code> method will get <code>Mouse</code> as its first entry, and
eventually work its way back to <code>Mouse::sound</code> for the details.</p>
<p>But this isn't the best solution. We still have to keep the <code>@ISA</code>
and the initial search package coordinated. Worse, if <code>Mouse</code> had
multiple entries in <code>@ISA</code>, we wouldn't necessarily know which one
had actually defined <code>speak</code>. So, is there an even better way?</p>
<p>
</p>
<h2><a name="the_super_way_of_doing_things">The SUPER way of doing things</a></h2>
<p>By changing the <code>Animal</code> class to the <code>SUPER</code> class in that
invocation, we get a search of all of our super classes (classes
listed in <code>@ISA</code>) automatically:</p>
<pre>
<span class="comment"># same Animal as before</span>
<span class="operator">{</span> <span class="keyword">package</span> <span class="variable">Mouse</span><span class="operator">;</span>
<span class="comment"># same @ISA, &sound as before</span>
<span class="keyword">sub</span><span class="variable"> speak </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="variable">$class</span><span class="operator">-></span><span class="variable">SUPER::speak</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="string">"[but you can barely hear it!]\n"</span><span class="operator">;</span>
<span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>So, <code>SUPER::speak</code> means look in the current package's <code>@ISA</code> for
<code>speak</code>, invoking the first one found. Note that it does <em>not</em> look in
the <code>@ISA</code> of <code>$class</code>.</p>
<p>
</p>
<h2><a name="where_we_re_at_so_far___">Where we're at so far...</a></h2>
<p>So far, we've seen the method arrow syntax:</p>
<pre>
<span class="variable">Class</span><span class="operator">-></span><span class="variable">method</span><span class="operator">(</span><span class="variable">@args</span><span class="operator">);</span>
</pre>
<p>or the equivalent:</p>
<pre>
<span class="variable">$a</span> <span class="operator">=</span> <span class="string">"Class"</span><span class="operator">;</span>
<span class="variable">$a</span><span class="operator">-></span><span class="variable">method</span><span class="operator">(</span><span class="variable">@args</span><span class="operator">);</span>
</pre>
<p>which constructs an argument list of:</p>
<pre>
("Class", @args)</pre>
<p>and attempts to invoke</p>
<pre>
<span class="variable">Class::method</span><span class="operator">(</span><span class="string">"Class"</span><span class="operator">,</span> <span class="variable">@Args</span><span class="operator">);</span>
</pre>
<p>However, if <code>Class::method</code> is not found, then <code>@Class::ISA</code> is examined
(recursively) to locate a package that does indeed contain <code>method</code>,
and that subroutine is invoked instead.</p>
<p>Using this simple syntax, we have class methods, (multiple)
inheritance, overriding, and extending. Using just what we've seen so
far, we've been able to factor out common code, and provide a nice way
to reuse implementations with variations. This is at the core of what
objects provide, but objects also provide instance data, which we
haven't even begun to cover.</p>
<p>
</p>
<h2><a name="a_horse_is_a_horse__of_course_of_course__or_is_it">A horse is a horse, of course of course -- or is it?</a></h2>
<p>Let's start with the code for the <code>Animal</code> class
and the <code>Horse</code> class:</p>
<pre>
<span class="operator">{</span> <span class="keyword">package</span> <span class="variable">Animal</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> speak </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">print</span> <span class="string">"a $class goes "</span><span class="operator">,</span> <span class="variable">$class</span><span class="operator">-></span><span class="variable">sound</span><span class="operator">,</span> <span class="string">"!\n"</span>
<span class="operator">}</span>
<span class="operator">}</span>
<span class="operator">{</span> <span class="keyword">package</span> <span class="variable">Horse</span><span class="operator">;</span>
<span class="variable">@ISA</span> <span class="operator">=</span> <span class="string">qw(Animal)</span><span class="operator">;</span>
<span class="keyword">sub</span><span class="variable"> sound </span><span class="operator">{</span> <span class="string">"neigh"</span> <span class="operator">}</span>
<span class="operator">}</span>
</pre>
<p>This lets us invoke <code>Horse->speak</code> to ripple upward to
<code>Animal::speak</code>, calling back to <code>Horse::sound</code> to get the specific
sound, and the output of:</p>
<pre>
a Horse goes neigh!</pre>
<p>But all of our Horse objects would have to be absolutely identical.
If I add a subroutine, all horses automatically share it. That's
great for making horses the same, but how do we capture the
distinctions about an individual horse? For example, suppose I want
to give my first horse a name. There's got to be a way to keep its
name separate from the other horses.</p>
<p>We can do that by drawing a new distinction, called an "instance".
An "instance" is generally created by a class. In Perl, any reference
can be an instance, so let's start with the simplest reference
that can hold a horse's name: a scalar reference.</p>
<pre>
<span class="keyword">my</span> <span class="variable">$name</span> <span class="operator">=</span> <span class="string">"Mr. Ed"</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$talking</span> <span class="operator">=</span> <span class="operator">\</span><span class="variable">$name</span><span class="operator">;</span>
</pre>
<p>So now <code>$talking</code> is a reference to what will be the instance-specific
data (the name). The final step in turning this into a real instance
is with a special operator called <a href="../../lib/Pod/perlfunc.html#item_bless"><code>bless</code></a>:</p>
<pre>
<span class="keyword">bless</span> <span class="variable">$talking</span><span class="operator">,</span> <span class="variable">Horse</span><span class="operator">;</span>
</pre>
<p>This operator stores information about the package named <code>Horse</code> into
the thing pointed at by the reference. At this point, we say
<code>$talking</code> is an instance of <code>Horse</code>. That is, it's a specific
horse. The reference is otherwise unchanged, and can still be used
with traditional dereferencing operators.</p>
<p>
</p>
<h2><a name="invoking_an_instance_method">Invoking an instance method</a></h2>
<p>The method arrow can be used on instances, as well as names of
packages (classes). So, let's get the sound that <code>$talking</code> makes:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$noise</span> <span class="operator">=</span> <span class="variable">$talking</span><span class="operator">-></span><span class="variable">sound</span><span class="operator">;</span>
</pre>
<p>To invoke <code>sound</code>, Perl first notes that <code>$talking</code> is a blessed
reference (and thus an instance). It then constructs an argument
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?