📄 ch31_03.htm
字号:
<html><head><title>use base (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="use base"><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="ch31_02.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch31_01.htm">Chapter 31: Pragmatic Modules</a></td><td align="right" valign="top" width="172"><a href="ch31_04.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">31.3. use base</h2><blockquote><pre class="programlisting">use base qw(Mother Father);</pre></blockquote><p><a name="INDEX-5419"></a>This pragma lets a programmer conveniently declare a derived classbased upon the listed parent classes. The declaration above isroughly equivalent to:<blockquote><pre class="programlisting">BEGIN { require Mother; require Father; push @ISA, qw(Mother Father);}</pre></blockquote>The <tt class="literal">use base</tt> pragma takes care of any <tt class="literal">require</tt> needed. When the<tt class="literal">strict 'vars'</tt> pragma is in scope, <tt class="literal">use base</tt> lets you (in effect)assign to <tt class="literal">@ISA</tt> without first having to declare <tt class="literal">our @ISA</tt>. (Since the<tt class="literal">use base</tt> pragma happens at compile time, it's best to avoid diddling<tt class="literal">@ISA</tt> on your own at run time.)<a name="INDEX-5420"></a></p><p>But beyond this, <tt class="literal">use base</tt> has another property. If any named baseclass makes use of the fields facility described under <tt class="literal">use fields</tt>later in this chapter, then the pragma initializes the package'sspecial field attributes from the base class. (Multiple inheritance offield classes is <em class="emphasis">not</em> supported. The <tt class="literal">use base</tt> pragma raises anexception if more than one named base class has fields.)</p><p>Any base class not yet loaded will be loaded automatically via<tt class="literal">require</tt>. However, whether to <tt class="literal">require</tt> a base class package isdetermined not by the customary inspection of <tt class="literal">%INC</tt>, but by the absenceof a global <tt class="literal">$VERSION</tt> in the base package. This hack keeps Perl fromrepeatedly trying (and failing) to load a base class that isn't in itsown requirable file (because, for example, it's loaded as part of someother module's file). If <tt class="literal">$VERSION</tt> is not detected after successfullyloading a file, <tt class="literal">use base</tt> will define <tt class="literal">$VERSION</tt> in the base package,setting it to the string "<tt class="literal">-1, defined by base.pm</tt>".</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="ch31_02.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="ch31_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.2. use autouse</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">31.4. use blib</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 + -