📄 ch32_06.htm
字号:
<html><head><title>Class::Struct (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="Class::Struct"><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="ch32_05.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch32_01.htm">Chapter 32: Standard Modules</a></td><td align="right" valign="top" width="172"><a href="ch32_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">32.6. Class::Struct</h2><a name="INDEX-5809"></a><p><blockquote><pre class="programlisting">use Class::Struct;struct Manager => { # Creates a Manager->new() constructor. name => '$', # Now name() method accesses a scalar value. salary => '$', # And so does salary(). started => '$', # And so does started().};struct Shoppe => { # Creates a Shoppe->new() constructor. owner => '$', # Now owner() method accesses a scalar. addrs => '@', # And addrs() method accesses an array. stock => '%', # And stock() method accesses a hash. boss => 'Manager', # Initializes with Manager->new().};$store = Shoppe->new();$store->owner('Abdul Alhazred');$store->addrs(0, 'Miskatonic University');$store->addrs(1, 'Innsmouth, Mass.');$store->stock("books", 208);$store->stock("charms", 3);$store->stock("potions", "none");$store->boss->name('Prof L. P. Haitch');$store->boss->salary('madness');$store->boss->started(scalar localtime);</pre></blockquote>The <tt class="literal">Class::Struct</tt> module provides a way to"declare" a class as having objects whose fields are of a specifictype. The function that does this is called<tt class="literal">struct</tt>. Because structures or records are not basetypes in Perl, each time you want to create a class to provide arecord-like data object, you have to define a constructor method alongwith accessor methods for each data field, sometimes called "wrapper"methods. The <tt class="literal">Class::Struct</tt> module's<tt class="literal">struct</tt> function alleviates this tedium by creatinga class for you on the fly. You just tell it what data members shouldexist and their types. The function creates a constructor methodnamed <tt class="literal">new</tt> in the package specified by the firstargument, plus an attribute accessor method for each member, asspecified by the second argument, which should be a hash reference.</p><p>Field types are specified as either a built-in type using thecustomary "<tt class="literal">$</tt>", "<tt class="literal">@</tt>","<tt class="literal">%</tt>", and "<tt class="literal">&</tt>" symbols, or asanother class using the class name. The type of each field will beenforced when you try to set the value.</p><p><a name="INDEX-5810"></a><a name="INDEX-5811"></a>Many standard modules use <tt class="literal">Class::Struct</tt> to createtheir objects and accessors, including <tt class="literal">Net::hostent</tt>and <tt class="literal">User::pwent</tt>, whose source you can look at as amodel. See also the CPAN modules <tt class="literal">Tie::SecureHash</tt>and <tt class="literal">Class::Multimethods</tt> for more elaborateapproaches to autogeneration of classes and accessor methods. See thesection "Managing Instance Data" in <a href="ch12_01.htm">Chapter 12, "Objects"</a>.</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="ch32_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="ch32_07.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">32.5. CGI::Carp</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">32.7. Config</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 + -