perltooc.html
来自「perl教程」· HTML 代码 · 共 738 行 · 第 1/5 页
HTML
738 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perltooc - Tom's OO Tutorial for Class Data in Perl</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perltooc - Tom's OO Tutorial for Class Data in Perl</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#class_data_in_a_can">Class Data in a Can</a></li>
<li><a href="#class_data_as_package_variables">Class Data as Package Variables</a></li>
<ul>
<li><a href="#putting_all_your_eggs_in_one_basket">Putting All Your Eggs in One Basket</a></li>
<li><a href="#inheritance_concerns">Inheritance Concerns</a></li>
<li><a href="#the_eponymous_metaobject">The Eponymous Meta-Object</a></li>
<li><a href="#indirect_references_to_class_data">Indirect References to Class Data</a></li>
<li><a href="#monadic_classes">Monadic Classes</a></li>
<li><a href="#translucent_attributes">Translucent Attributes</a></li>
</ul>
<li><a href="#class_data_as_lexical_variables">Class Data as Lexical Variables</a></li>
<ul>
<li><a href="#privacy_and_responsibility">Privacy and Responsibility</a></li>
<li><a href="#filescoped_lexicals">File-Scoped Lexicals</a></li>
<li><a href="#more_inheritance_concerns">More Inheritance Concerns</a></li>
<li><a href="#locking_the_door_and_throwing_away_the_key">Locking the Door and Throwing Away the Key</a></li>
<li><a href="#translucency_revisited">Translucency Revisited</a></li>
</ul>
<li><a href="#notes">NOTES</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
<li><a href="#acknowledgements">ACKNOWLEDGEMENTS</a></li>
<li><a href="#history">HISTORY</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perltooc - Tom's OO Tutorial for Class Data in Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>When designing an object class, you are sometimes faced with the situation
of wanting common state shared by all objects of that class.
Such <em>class attributes</em> act somewhat like global variables for the entire
class, but unlike program-wide globals, class attributes have meaning only to
the class itself.</p>
<p>Here are a few examples where class attributes might come in handy:</p>
<ul>
<li>
<p>to keep a count of the objects you've created, or how many are
still extant.</p>
</li>
<li>
<p>to extract the name or file descriptor for a logfile used by a debugging
method.</p>
</li>
<li>
<p>to access collective data, like the total amount of cash dispensed by
all ATMs in a network in a given day.</p>
</li>
<li>
<p>to access the last object created by a class, or the most accessed object,
or to retrieve a list of all objects.</p>
</li>
</ul>
<p>Unlike a true global, class attributes should not be accessed directly.
Instead, their state should be inspected, and perhaps altered, only
through the mediated access of <em>class methods</em>. These class attributes
accessor methods are similar in spirit and function to accessors used
to manipulate the state of instance attributes on an object. They provide a
clear firewall between interface and implementation.</p>
<p>You should allow access to class attributes through either the class
name or any object of that class. If we assume that $an_object is of
type Some_Class, and the &Some_Class::population_count method accesses
class attributes, then these two invocations should both be possible,
and almost certainly equivalent.</p>
<pre>
Some_Class->population_count()
$an_object->population_count()</pre>
<p>The question is, where do you store the state which that method accesses?
Unlike more restrictive languages like C++, where these are called
static data members, Perl provides no syntactic mechanism to declare
class attributes, any more than it provides a syntactic mechanism to
declare instance attributes. Perl provides the developer with a broad
set of powerful but flexible features that can be uniquely crafted to
the particular demands of the situation.</p>
<p>A class in Perl is typically implemented in a module. A module consists
of two complementary feature sets: a package for interfacing with the
outside world, and a lexical file scope for privacy. Either of these
two mechanisms can be used to implement class attributes. That means you
get to decide whether to put your class attributes in package variables
or to put them in lexical variables.</p>
<p>And those aren't the only decisions to make. If you choose to use package
variables, you can make your class attribute accessor methods either ignorant
of inheritance or sensitive to it. If you choose lexical variables,
you can elect to permit access to them from anywhere in the entire file
scope, or you can limit direct data access exclusively to the methods
implementing those attributes.</p>
<p>
</p>
<hr />
<h1><a name="class_data_in_a_can">Class Data in a Can</a></h1>
<p>One of the easiest ways to solve a hard problem is to let someone else
do it for you! In this case, Class::Data::Inheritable (available on a
CPAN near you) offers a canned solution to the class data problem
using closures. So before you wade into this document, consider
having a look at that module.</p>
<p>
</p>
<hr />
<h1><a name="class_data_as_package_variables">Class Data as Package Variables</a></h1>
<p>Because a class in Perl is really just a package, using package variables
to hold class attributes is the most natural choice. This makes it simple
for each class to have its own class attributes. Let's say you have a class
called Some_Class that needs a couple of different attributes that you'd
like to be global to the entire class. The simplest thing to do is to
use package variables like $Some_Class::CData1 and $Some_Class::CData2
to hold these attributes. But we certainly don't want to encourage
outsiders to touch those data directly, so we provide methods
to mediate access.</p>
<p>In the accessor methods below, we'll for now just ignore the first
argument--that part to the left of the arrow on method invocation, which
is either a class name or an object reference.</p>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?