📄 ch14_05.htm
字号:
<html><head><title>A Subtle Untying Trap (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="A Subtle Untying Trap"><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="ch14_04.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch14_01.htm">Chapter 14: Tied Variables</a></td><td align="right" valign="top" width="172"><a href="ch14_06.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">14.5. A Subtle Untying Trap</h2><p><a name="INDEX-2759"></a><a name="INDEX-2760"></a><a name="INDEX-2761"></a>If you intend to make use of the object returned from<tt class="literal">tie</tt> or <tt class="literal">tied</tt>, and the classdefines a destructor, there is a subtle trap you must guard against.Consider this (admittedly contrived) example of a class that uses afile to log all values assigned to a scalar:<blockquote><pre class="programlisting">package Remember;sub TIESCALAR { my $class = shift; my $filename = shift; open(my $handle, ">", $filename) or die "Cannot open $filename: $!\n"; print $handle "The Start\n"; bless {FH => $handle, VALUE => 0}, $class;}sub FETCH { my $self = shift; return $self->{VALUE};}sub STORE { my $self = shift; my $value = shift; my $handle = $self->{FH}; print $handle "$value\n"; $self->{VALUE} = $value;}sub DESTROY { my $self = shift; my $handle = $self->{FH}; print $handle "The End\n"; close $handle;}1;</pre></blockquote>Here is an example that makes use of our <tt class="literal">Remember</tt> class:<blockquote><pre class="programlisting">use strict;use Remember;my $fred;$x = tie $fred, "Remember", "camel.log";$fred = 1;$fred = 4;$fred = 5;untie $fred;system "cat camel.log";</pre></blockquote>This is the output when it is executed:<blockquote><pre class="programlisting">The Start145The End</pre></blockquote>So far, so good. Let's add an extra method to the <tt class="literal">Remember</tt> class thatallows comments in the file--say, something like this:<blockquote><pre class="programlisting">sub comment { my $self = shift; my $message = shift; print { $self->{FH} } $handle $message, "\n";}</pre></blockquote>And here is the previous example, modified to use the <tt class="literal">comment</tt> method:<blockquote><pre class="programlisting">use strict;use Remember;my ($fred, $x);$x = tie $fred, "Remember", "camel.log";$fred = 1;$fred = 4;comment $x "changing...";$fred = 5;untie $fred;system "cat camel.log";</pre></blockquote><a name="INDEX-2762"></a>Now the file will be empty, which probably wasn't what youintended. Here's why. Tying a variable associates it with theobject returned by the constructor. This object normally has only onereference: the one hidden behind the tied variable itself. Calling"<tt class="literal">untie</tt>" breaks the association and eliminates thatreference. Since there are no remaining references to the object, the<tt class="literal">DESTROY</tt> method is triggered.<a name="INDEX-2763"></a><a name="INDEX-2764"></a></p><p>However, in the example above we stored a second reference to theobject tied to <tt class="literal">$x</tt>. That means that after the<tt class="literal">untie</tt> there will still be a valid reference to theobject. <tt class="literal">DESTROY</tt> won't get triggered, and the filewon't get flushed and closed. That's why there was no output: thefilehandle's buffer was still in memory. It won't hit the disk untilthe program exits.</p><p><a name="INDEX-2765"></a>To detect this, you could use the <tt class="userinput"><b>-w</b></tt>command-line flag, or include the <tt class="literal">use warnings"untie"</tt> pragma in the current lexical scope.Either technique would identify a call to <tt class="literal">untie</tt>while there were still references to the tied object remaining. Ifso, Perl prints this warning:<blockquote><pre class="programlisting">untie attempted while 1 inner references still exist</pre></blockquote>To get the program to work properly and silence the warning, eliminateany extra references to the tied object <em class="emphasis">before</em>calling <tt class="literal">untie</tt>. You can do that explicitly:<blockquote><pre class="programlisting">undef $x;untie $fred;</pre></blockquote>Often though you can solve the problem simply by making sure yourvariables go out of scope at the appropriate time.</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="ch14_04.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="ch14_06.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">14.4. Tying Filehandles</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">14.6. Tie Modules on CPAN</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 © 2001</a> O'Reilly & 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 + -