⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12_06.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
字号:
<html><head><title>Instance Destructors (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Instance Destructors"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch12_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch12_01.htm">Chapter 12: Objects</a></td><td align="right" valign="top" width="172"><a href="ch12_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">12.6. Instance Destructors</h2><p><a name="INDEX-2502"></a><a name="INDEX-2503"></a>As with any other referent in Perl, when the last reference to anobject goes away, its memory is implicitly recycled.  With an object,you have the opportunity to capture control just as this is about tohappen by defining a <tt class="literal">DESTROY</tt> subroutine in the class's package.This method is triggered automatically at the appropriate moment, with theabout-to-be-recycled object as its only argument.</p><p><a name="INDEX-2504"></a>Destructors are rarely needed in Perl, because memory management ishandled automatically for you.  Some objects, though, may have stateoutside the memory system that you'd like to attend to, such asfilehandles or database connections.<blockquote><pre class="programlisting">package MailNotify;sub DESTROY {    my $self = shift;    my $fh   = $self-&gt;{mailhandle};    my $id   = $self-&gt;{name};    print $fh "\n$id is signing off at " . localtime() . "\n";    close $fh;  # close pipe to mailer}</pre></blockquote><a name="INDEX-2505"></a></p><p>Just as Perl uses only a single method to construct an object evenwhen the constructor's class inherits from one or more other classes,Perl also uses only one <tt class="literal">DESTROY</tt> method per objectdestroyed regardless of inheritance.  In other words, Perl does not dohierarchical destruction for you.  If your class overrides asuperclass's destructor, then your <tt class="literal">DESTROY</tt> methodmay need to invoke the <tt class="literal">DESTROY</tt> method for anyapplicable base classes:<blockquote><pre class="programlisting">sub DESTROY {    my $self = shift;    # check for an overridden destructor...    $self-&gt;SUPER::DESTROY if $self-&gt;can("SUPER::DESTROY");    # now do your own thing before or after}</pre></blockquote>This applies only to inherited classes; an object thatis simply <em class="emphasis">contained</em> within the currentobject--as, for example, one value in a larger hash--will be freed anddestroyed automatically.  This is one reason why containership viamere aggregation (sometimes called a "has-a" relationship) is oftencleaner and clearer than inheritance (an "is-a" relationship).  Inother words, often you really only need to store one object insideanother directly instead of through inheritance, which can addunnecessary complexity.  Sometimes when users reach for multipleinheritance, single inheritance will suffice.<a name="INDEX-2506"></a></p><p>Explicitly calling <tt class="literal">DESTROY</tt> is possible but seldomneeded.  It might even be harmful since running the destructor morethan once on the same object could prove unpleasant.</p><h3 class="sect2">12.6.1. Garbage Collection with DESTROY Methods</h3><p><a name="INDEX-2507"></a><a name="INDEX-2508"></a><a name="INDEX-2509"></a><a name="INDEX-2510"></a><a name="INDEX-2511"></a>As described in the section <a href="ch08_05.htm#ch08-sect-garbage">Section 12.5.2, "Garbage Collection, Circular References, and Weak References"</a> in <a href="ch08_01.htm">Chapter 8, "References"</a>, a variable that refersto itself (or multiple variables that refer to one another indirectly)will not be freed until the program (or embedded interpreter) is aboutto exit.  If you want to reclaim the memory any earlier, youusually have to explicitly break the reference or weaken it usingthe <tt class="literal">WeakRef</tt> module on CPAN.</p><p><a name="INDEX-2512"></a><a name="INDEX-2513"></a><a name="INDEX-2514"></a>With objects, an alternative solution is to create a container classthat holds a pointer to the self-referential data structure.  Definea <tt class="literal">DESTROY</tt> method for the containing object's class that manuallybreaks the circularities in the self-referential structure.  Youcan find an example of this in <em class="citetitle">Chapter 13</em> of the <em class="citetitle">Perl Cookbook</em>in the recipe 13.13, "Coping with Circular Data Structures".</p><p><a name="INDEX-2515"></a><a name="INDEX-2516"></a><a name="INDEX-2517"></a>When an interpreter shuts down, all its objects are destroyed, which isimportant for multithreaded or embedded Perl applications.  Objects arealways destroyed in a separate pass before ordinary references.  This is toprevent <tt class="literal">DESTROY</tt> methods from using references that have themselvesbeen destroyed.  (And also because plain references are onlygarbage-collected in embedded interpreters, since exiting a process isa very <em class="emphasis">fast</em> way of reclaiming references.  But exiting won't run theobject destructors, so Perl does that first.)</p><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch12_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch12_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">12.5. Class Inheritance</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">12.7. Managing Instance Data</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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