📄 ch08_43.htm
字号:
<html><head><title>Class::Struct (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_42.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_44.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.43. Class::Struct</h2><p><a name="INDEX-1326" />Formerly named<a name="INDEX-1327" />Class::Template; exports a singlefunction, <tt class="literal">struct</tt>. <tt class="literal">struct</tt> takesa list of element names and types, and optionally a class name, andcreates a Perl class that implements a struct-like data structure. Italso creates a constructor method, <tt class="literal">new</tt>, forcreating objects of the class (so a class created this way must notitself define a <tt class="literal">new</tt> method).</p><p>Each element in the struct data has an accessor method, which is usedto assign to the element and to fetch its value. The default accessorcan be overridden by declaring a sub of the same name in the package.Each element's type can be scalar, array, hash, orclass.</p><a name="INDEX-1328" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>struct</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>struct (<em class="replaceable">paramlist</em>)</pre><p><a name="INDEX-1328" />Creates a class, with object methods foreach element of the class. The parameter list<em class="replaceable"><tt>paramlist</tt></em> can take one of three forms:</p><blockquote><pre class="code">struct( CLASS_NAME => [ ELEMENT_LIST ]); # Object is array-basedstruct( CLASS_NAME => { ELEMENT_LIST }); # Object is hash-basedstruct( ELEMENT_LIST ); # Class name is current, and package name # object is array-based</pre></blockquote><p>The array-based element lists are faster and smaller, but thehash-based list is more flexible. The class that is created can be asubclass of the UNIVERSAL class, but not of any other class.</p><h4 class="refsect1">Element list</h4><p>The items in the ELEMENT_LIST are of the form: </p><blockquote><pre class="code">NAME => TYPE, ...</pre></blockquote><p>in which each <tt class="literal">NAME=>TYPE</tt> pair declares oneelement of the <tt class="literal">struct</tt>. Each element name isdefined as an accessor method, unless a method is explicitly definedwith that name. (In that case, a warning is issued if the<em class="emphasis">-w</em> flag is set.)</p><h4 class="refsect1">Element types and accessor methods</h4><p>There are four possible element types, each represented by a string.Each string may start with an asterisk (<tt class="literal">*</tt>),indicating that a reference to the element will be returned. The typeof an element determines the accessor method provided. The followinglist shows the element types, the strings that represent them, andthe accessor:</p><dl><dt><i><em class="emphasis">array (</em><tt class="literal">@</tt> <em class="emphasis">or</em> <tt class="literal">*@</tt>)</i></dt><dd>The element is an array, initialized to <tt class="literal">( )</tt>. Withno argument, the accessor returns a reference to theelement's whole array. With one or two arguments,the first argument is an index specifying one element of the array;the second argument, if present, is the value that will be assignedto that array element.</p></dd><dt><i><em class="emphasis">class (</em><tt class="literal">Class_Name</tt> <em class="emphasis">or</em> <tt class="literal">*Class_Name</tt>)</i></dt><dd>The element's value must be a reference blessed tothe named class or to one of its subclasses. The element isinitialized to the result of calling the <tt class="literal">new</tt>constructor of the named class. The accessor'sargument, if any, is the value that will be assigned to the element.The accessor <tt class="literal">croak</tt> s if it's notan appropriate object reference.</p></dd><dt><i><em class="emphasis">hash (</em><tt class="literal">%</tt> <em class="emphasis">or</em> <tt class="literal">*%</tt>)</i></dt><dd>The element is a hash, initialized to <tt class="literal">( )</tt>. With noargument, the accessor returns a reference to theelement's whole hash. With one or two arguments, thefirst argument is a key specifying one element of the hash; thesecond argument, if present, is the value to be assigned to that hashelement.</p></dd><dt><i><em class="emphasis">scalar (</em><tt class="literal">$</tt> <em class="emphasis">or</em> <tt class="literal">*$</tt>)</i></dt><dd>The element is a scalar, initialized to <tt class="literal">undef</tt>. Theaccessor's argument, if any, is assigned to theelement.</p></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_42.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_44.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.42. Class::ISA</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.44. Config</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -