Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

815 lines
26 KiB

  1. =head1 NAME
  2. perlboot - Beginner's Object-Oriented Tutorial
  3. =head1 DESCRIPTION
  4. If you're not familiar with objects from other languages, some of the
  5. other Perl object documentation may be a little daunting, such as
  6. L<perlobj>, a basic reference in using objects, and L<perltoot>, which
  7. introduces readers to the peculiarities of Perl's object system in a
  8. tutorial way.
  9. So, let's take a different approach, presuming no prior object
  10. experience. It helps if you know about subroutines (L<perlsub>),
  11. references (L<perlref> et. seq.), and packages (L<perlmod>), so become
  12. familiar with those first if you haven't already.
  13. =head2 If we could talk to the animals...
  14. Let's let the animals talk for a moment:
  15. sub Cow::speak {
  16. print "a Cow goes moooo!\n";
  17. }
  18. sub Horse::speak {
  19. print "a Horse goes neigh!\n";
  20. }
  21. sub Sheep::speak {
  22. print "a Sheep goes baaaah!\n"
  23. }
  24. Cow::speak;
  25. Horse::speak;
  26. Sheep::speak;
  27. This results in:
  28. a Cow goes moooo!
  29. a Horse goes neigh!
  30. a Sheep goes baaaah!
  31. Nothing spectacular here. Simple subroutines, albeit from separate
  32. packages, and called using the full package name. So let's create
  33. an entire pasture:
  34. # Cow::speak, Horse::speak, Sheep::speak as before
  35. @pasture = qw(Cow Cow Horse Sheep Sheep);
  36. foreach $animal (@pasture) {
  37. &{$animal."::speak"};
  38. }
  39. This results in:
  40. a Cow goes moooo!
  41. a Cow goes moooo!
  42. a Horse goes neigh!
  43. a Sheep goes baaaah!
  44. a Sheep goes baaaah!
  45. Wow. That symbolic coderef de-referencing there is pretty nasty.
  46. We're counting on C<no strict subs> mode, certainly not recommended
  47. for larger programs. And why was that necessary? Because the name of
  48. the package seems to be inseparable from the name of the subroutine we
  49. want to invoke within that package.
  50. Or is it?
  51. =head2 Introducing the method invocation arrow
  52. For now, let's say that C<< Class->method >> invokes subroutine
  53. C<method> in package C<Class>. (Here, "Class" is used in its
  54. "category" meaning, not its "scholastic" meaning.) That's not
  55. completely accurate, but we'll do this one step at a time. Now let's
  56. use it like so:
  57. # Cow::speak, Horse::speak, Sheep::speak as before
  58. Cow->speak;
  59. Horse->speak;
  60. Sheep->speak;
  61. And once again, this results in:
  62. a Cow goes moooo!
  63. a Horse goes neigh!
  64. a Sheep goes baaaah!
  65. That's not fun yet. Same number of characters, all constant, no
  66. variables. But yet, the parts are separable now. Watch:
  67. $a = "Cow";
  68. $a->speak; # invokes Cow->speak
  69. Ahh! Now that the package name has been parted from the subroutine
  70. name, we can use a variable package name. And this time, we've got
  71. something that works even when C<use strict refs> is enabled.
  72. =head2 Invoking a barnyard
  73. Let's take that new arrow invocation and put it back in the barnyard
  74. example:
  75. sub Cow::speak {
  76. print "a Cow goes moooo!\n";
  77. }
  78. sub Horse::speak {
  79. print "a Horse goes neigh!\n";
  80. }
  81. sub Sheep::speak {
  82. print "a Sheep goes baaaah!\n"
  83. }
  84. @pasture = qw(Cow Cow Horse Sheep Sheep);
  85. foreach $animal (@pasture) {
  86. $animal->speak;
  87. }
  88. There! Now we have the animals all talking, and safely at that,
  89. without the use of symbolic coderefs.
  90. But look at all that common code. Each of the C<speak> routines has a
  91. similar structure: a C<print> operator and a string that contains
  92. common text, except for two of the words. It'd be nice if we could
  93. factor out the commonality, in case we decide later to change it all
  94. to C<says> instead of C<goes>.
  95. And we actually have a way of doing that without much fuss, but we
  96. have to hear a bit more about what the method invocation arrow is
  97. actually doing for us.
  98. =head2 The extra parameter of method invocation
  99. The invocation of:
  100. Class->method(@args)
  101. attempts to invoke subroutine C<Class::method> as:
  102. Class::method("Class", @args);
  103. (If the subroutine can't be found, "inheritance" kicks in, but we'll
  104. get to that later.) This means that we get the class name as the
  105. first parameter (the only parameter, if no arguments are given). So
  106. we can rewrite the C<Sheep> speaking subroutine as:
  107. sub Sheep::speak {
  108. my $class = shift;
  109. print "a $class goes baaaah!\n";
  110. }
  111. And the other two animals come out similarly:
  112. sub Cow::speak {
  113. my $class = shift;
  114. print "a $class goes moooo!\n";
  115. }
  116. sub Horse::speak {
  117. my $class = shift;
  118. print "a $class goes neigh!\n";
  119. }
  120. In each case, C<$class> will get the value appropriate for that
  121. subroutine. But once again, we have a lot of similar structure. Can
  122. we factor that out even further? Yes, by calling another method in
  123. the same class.
  124. =head2 Calling a second method to simplify things
  125. Let's call out from C<speak> to a helper method called C<sound>.
  126. This method provides the constant text for the sound itself.
  127. { package Cow;
  128. sub sound { "moooo" }
  129. sub speak {
  130. my $class = shift;
  131. print "a $class goes ", $class->sound, "!\n"
  132. }
  133. }
  134. Now, when we call C<< Cow->speak >>, we get a C<$class> of C<Cow> in
  135. C<speak>. This in turn selects the C<< Cow->sound >> method, which
  136. returns C<moooo>. But how different would this be for the C<Horse>?
  137. { package Horse;
  138. sub sound { "neigh" }
  139. sub speak {
  140. my $class = shift;
  141. print "a $class goes ", $class->sound, "!\n"
  142. }
  143. }
  144. Only the name of the package and the specific sound change. So can we
  145. somehow share the definition for C<speak> between the Cow and the
  146. Horse? Yes, with inheritance!
  147. =head2 Inheriting the windpipes
  148. We'll define a common subroutine package called C<Animal>, with the
  149. definition for C<speak>:
  150. { package Animal;
  151. sub speak {
  152. my $class = shift;
  153. print "a $class goes ", $class->sound, "!\n"
  154. }
  155. }
  156. Then, for each animal, we say it "inherits" from C<Animal>, along
  157. with the animal-specific sound:
  158. { package Cow;
  159. @ISA = qw(Animal);
  160. sub sound { "moooo" }
  161. }
  162. Note the added C<@ISA> array. We'll get to that in a minute.
  163. But what happens when we invoke C<< Cow->speak >> now?
  164. First, Perl constructs the argument list. In this case, it's just
  165. C<Cow>. Then Perl looks for C<Cow::speak>. But that's not there, so
  166. Perl checks for the inheritance array C<@Cow::ISA>. It's there,
  167. and contains the single name C<Animal>.
  168. Perl next checks for C<speak> inside C<Animal> instead, as in
  169. C<Animal::speak>. And that's found, so Perl invokes that subroutine
  170. with the already frozen argument list.
  171. Inside the C<Animal::speak> subroutine, C<$class> becomes C<Cow> (the
  172. first argument). So when we get to the step of invoking
  173. C<< $class->sound >>, it'll be looking for C<< Cow->sound >>, which
  174. gets it on the first try without looking at C<@ISA>. Success!
  175. =head2 A few notes about @ISA
  176. This magical C<@ISA> variable (pronounced "is a" not "ice-uh"), has
  177. declared that C<Cow> "is a" C<Animal>. Note that it's an array,
  178. not a simple single value, because on rare occasions, it makes sense
  179. to have more than one parent class searched for the missing methods.
  180. If C<Animal> also had an C<@ISA>, then we'd check there too. The
  181. search is recursive, depth-first, left-to-right in each C<@ISA>.
  182. Typically, each C<@ISA> has only one element (multiple elements means
  183. multiple inheritance and multiple headaches), so we get a nice tree of
  184. inheritance.
  185. When we turn on C<use strict>, we'll get complaints on C<@ISA>, since
  186. it's not a variable containing an explicit package name, nor is it a
  187. lexical ("my") variable. We can't make it a lexical variable though
  188. (it has to belong to the package to be found by the inheritance mechanism),
  189. so there's a couple of straightforward ways to handle that.
  190. The easiest is to just spell the package name out:
  191. @Cow::ISA = qw(Animal);
  192. Or allow it as an implicitly named package variable:
  193. package Cow;
  194. use vars qw(@ISA);
  195. @ISA = qw(Animal);
  196. If you're bringing in the class from outside, via an object-oriented
  197. module, you change:
  198. package Cow;
  199. use Animal;
  200. use vars qw(@ISA);
  201. @ISA = qw(Animal);
  202. into just:
  203. package Cow;
  204. use base qw(Animal);
  205. And that's pretty darn compact.
  206. =head2 Overriding the methods
  207. Let's add a mouse, which can barely be heard:
  208. # Animal package from before
  209. { package Mouse;
  210. @ISA = qw(Animal);
  211. sub sound { "squeak" }
  212. sub speak {
  213. my $class = shift;
  214. print "a $class goes ", $class->sound, "!\n";
  215. print "[but you can barely hear it!]\n";
  216. }
  217. }
  218. Mouse->speak;
  219. which results in:
  220. a Mouse goes squeak!
  221. [but you can barely hear it!]
  222. Here, C<Mouse> has its own speaking routine, so C<< Mouse->speak >>
  223. doesn't immediately invoke C<< Animal->speak >>. This is known as
  224. "overriding". In fact, we didn't even need to say that a C<Mouse> was
  225. an C<Animal> at all, since all of the methods needed for C<speak> are
  226. completely defined with C<Mouse>.
  227. But we've now duplicated some of the code from C<< Animal->speak >>,
  228. and this can once again be a maintenance headache. So, can we avoid
  229. that? Can we say somehow that a C<Mouse> does everything any other
  230. C<Animal> does, but add in the extra comment? Sure!
  231. First, we can invoke the C<Animal::speak> method directly:
  232. # Animal package from before
  233. { package Mouse;
  234. @ISA = qw(Animal);
  235. sub sound { "squeak" }
  236. sub speak {
  237. my $class = shift;
  238. Animal::speak($class);
  239. print "[but you can barely hear it!]\n";
  240. }
  241. }
  242. Note that we have to include the C<$class> parameter (almost surely
  243. the value of C<"Mouse">) as the first parameter to C<Animal::speak>,
  244. since we've stopped using the method arrow. Why did we stop? Well,
  245. if we invoke C<< Animal->speak >> there, the first parameter to the
  246. method will be C<"Animal"> not C<"Mouse">, and when time comes for it
  247. to call for the C<sound>, it won't have the right class to come back
  248. to this package.
  249. Invoking C<Animal::speak> directly is a mess, however. What if
  250. C<Animal::speak> didn't exist before, and was being inherited from a
  251. class mentioned in C<@Animal::ISA>? Because we are no longer using
  252. the method arrow, we get one and only one chance to hit the right
  253. subroutine.
  254. Also note that the C<Animal> classname is now hardwired into the
  255. subroutine selection. This is a mess if someone maintains the code,
  256. changing C<@ISA> for <Mouse> and didn't notice C<Animal> there in
  257. C<speak>. So, this is probably not the right way to go.
  258. =head2 Starting the search from a different place
  259. A better solution is to tell Perl to search from a higher place
  260. in the inheritance chain:
  261. # same Animal as before
  262. { package Mouse;
  263. # same @ISA, &sound as before
  264. sub speak {
  265. my $class = shift;
  266. $class->Animal::speak;
  267. print "[but you can barely hear it!]\n";
  268. }
  269. }
  270. Ahh. This works. Using this syntax, we start with C<Animal> to find
  271. C<speak>, and use all of C<Animal>'s inheritance chain if not found
  272. immediately. And yet the first parameter will be C<$class>, so the
  273. found C<speak> method will get C<Mouse> as its first entry, and
  274. eventually work its way back to C<Mouse::sound> for the details.
  275. But this isn't the best solution. We still have to keep the C<@ISA>
  276. and the initial search package coordinated. Worse, if C<Mouse> had
  277. multiple entries in C<@ISA>, we wouldn't necessarily know which one
  278. had actually defined C<speak>. So, is there an even better way?
  279. =head2 The SUPER way of doing things
  280. By changing the C<Animal> class to the C<SUPER> class in that
  281. invocation, we get a search of all of our super classes (classes
  282. listed in C<@ISA>) automatically:
  283. # same Animal as before
  284. { package Mouse;
  285. # same @ISA, &sound as before
  286. sub speak {
  287. my $class = shift;
  288. $class->SUPER::speak;
  289. print "[but you can barely hear it!]\n";
  290. }
  291. }
  292. So, C<SUPER::speak> means look in the current package's C<@ISA> for
  293. C<speak>, invoking the first one found.
  294. =head2 Where we're at so far...
  295. So far, we've seen the method arrow syntax:
  296. Class->method(@args);
  297. or the equivalent:
  298. $a = "Class";
  299. $a->method(@args);
  300. which constructs an argument list of:
  301. ("Class", @args)
  302. and attempts to invoke
  303. Class::method("Class", @Args);
  304. However, if C<Class::method> is not found, then C<@Class::ISA> is examined
  305. (recursively) to locate a package that does indeed contain C<method>,
  306. and that subroutine is invoked instead.
  307. Using this simple syntax, we have class methods, (multiple)
  308. inheritance, overriding, and extending. Using just what we've seen so
  309. far, we've been able to factor out common code, and provide a nice way
  310. to reuse implementations with variations. This is at the core of what
  311. objects provide, but objects also provide instance data, which we
  312. haven't even begun to cover.
  313. =head2 A horse is a horse, of course of course -- or is it?
  314. Let's start with the code for the C<Animal> class
  315. and the C<Horse> class:
  316. { package Animal;
  317. sub speak {
  318. my $class = shift;
  319. print "a $class goes ", $class->sound, "!\n"
  320. }
  321. }
  322. { package Horse;
  323. @ISA = qw(Animal);
  324. sub sound { "neigh" }
  325. }
  326. This lets us invoke C<< Horse->speak >> to ripple upward to
  327. C<Animal::speak>, calling back to C<Horse::sound> to get the specific
  328. sound, and the output of:
  329. a Horse goes neigh!
  330. But all of our Horse objects would have to be absolutely identical.
  331. If I add a subroutine, all horses automatically share it. That's
  332. great for making horses the same, but how do we capture the
  333. distinctions about an individual horse? For example, suppose I want
  334. to give my first horse a name. There's got to be a way to keep its
  335. name separate from the other horses.
  336. We can do that by drawing a new distinction, called an "instance".
  337. An "instance" is generally created by a class. In Perl, any reference
  338. can be an instance, so let's start with the simplest reference
  339. that can hold a horse's name: a scalar reference.
  340. my $name = "Mr. Ed";
  341. my $talking = \$name;
  342. So now C<$talking> is a reference to what will be the instance-specific
  343. data (the name). The final step in turning this into a real instance
  344. is with a special operator called C<bless>:
  345. bless $talking, Horse;
  346. This operator stores information about the package named C<Horse> into
  347. the thing pointed at by the reference. At this point, we say
  348. C<$talking> is an instance of C<Horse>. That is, it's a specific
  349. horse. The reference is otherwise unchanged, and can still be used
  350. with traditional dereferencing operators.
  351. =head2 Invoking an instance method
  352. The method arrow can be used on instances, as well as names of
  353. packages (classes). So, let's get the sound that C<$talking> makes:
  354. my $noise = $talking->sound;
  355. To invoke C<sound>, Perl first notes that C<$talking> is a blessed
  356. reference (and thus an instance). It then constructs an argument
  357. list, in this case from just C<($talking)>. (Later we'll see that
  358. arguments will take their place following the instance variable,
  359. just like with classes.)
  360. Now for the fun part: Perl takes the class in which the instance was
  361. blessed, in this case C<Horse>, and uses that to locate the subroutine
  362. to invoke the method. In this case, C<Horse::sound> is found directly
  363. (without using inheritance), yielding the final subroutine invocation:
  364. Horse::sound($talking)
  365. Note that the first parameter here is still the instance, not the name
  366. of the class as before. We'll get C<neigh> as the return value, and
  367. that'll end up as the C<$noise> variable above.
  368. If Horse::sound had not been found, we'd be wandering up the
  369. C<@Horse::ISA> list to try to find the method in one of the
  370. superclasses, just as for a class method. The only difference between
  371. a class method and an instance method is whether the first parameter
  372. is an instance (a blessed reference) or a class name (a string).
  373. =head2 Accessing the instance data
  374. Because we get the instance as the first parameter, we can now access
  375. the instance-specific data. In this case, let's add a way to get at
  376. the name:
  377. { package Horse;
  378. @ISA = qw(Animal);
  379. sub sound { "neigh" }
  380. sub name {
  381. my $self = shift;
  382. $$self;
  383. }
  384. }
  385. Now we call for the name:
  386. print $talking->name, " says ", $talking->sound, "\n";
  387. Inside C<Horse::name>, the C<@_> array contains just C<$talking>,
  388. which the C<shift> stores into C<$self>. (It's traditional to shift
  389. the first parameter off into a variable named C<$self> for instance
  390. methods, so stay with that unless you have strong reasons otherwise.)
  391. Then, C<$self> gets de-referenced as a scalar ref, yielding C<Mr. Ed>,
  392. and we're done with that. The result is:
  393. Mr. Ed says neigh.
  394. =head2 How to build a horse
  395. Of course, if we constructed all of our horses by hand, we'd most
  396. likely make mistakes from time to time. We're also violating one of
  397. the properties of object-oriented programming, in that the "inside
  398. guts" of a Horse are visible. That's good if you're a veterinarian,
  399. but not if you just like to own horses. So, let's let the Horse class
  400. build a new horse:
  401. { package Horse;
  402. @ISA = qw(Animal);
  403. sub sound { "neigh" }
  404. sub name {
  405. my $self = shift;
  406. $$self;
  407. }
  408. sub named {
  409. my $class = shift;
  410. my $name = shift;
  411. bless \$name, $class;
  412. }
  413. }
  414. Now with the new C<named> method, we can build a horse:
  415. my $talking = Horse->named("Mr. Ed");
  416. Notice we're back to a class method, so the two arguments to
  417. C<Horse::named> are C<Horse> and C<Mr. Ed>. The C<bless> operator
  418. not only blesses C<$name>, it also returns the reference to C<$name>,
  419. so that's fine as a return value. And that's how to build a horse.
  420. We've called the constructor C<named> here, so that it quickly denotes
  421. the constructor's argument as the name for this particular C<Horse>.
  422. You can use different constructors with different names for different
  423. ways of "giving birth" to the object (like maybe recording its
  424. pedigree or date of birth). However, you'll find that most people
  425. coming to Perl from more limited languages use a single constructor
  426. named C<new>, with various ways of interpreting the arguments to
  427. C<new>. Either style is fine, as long as you document your particular
  428. way of giving birth to an object. (And you I<were> going to do that,
  429. right?)
  430. =head2 Inheriting the constructor
  431. But was there anything specific to C<Horse> in that method? No. Therefore,
  432. it's also the same recipe for building anything else that inherited from
  433. C<Animal>, so let's put it there:
  434. { package Animal;
  435. sub speak {
  436. my $class = shift;
  437. print "a $class goes ", $class->sound, "!\n"
  438. }
  439. sub name {
  440. my $self = shift;
  441. $$self;
  442. }
  443. sub named {
  444. my $class = shift;
  445. my $name = shift;
  446. bless \$name, $class;
  447. }
  448. }
  449. { package Horse;
  450. @ISA = qw(Animal);
  451. sub sound { "neigh" }
  452. }
  453. Ahh, but what happens if we invoke C<speak> on an instance?
  454. my $talking = Horse->named("Mr. Ed");
  455. $talking->speak;
  456. We get a debugging value:
  457. a Horse=SCALAR(0xaca42ac) goes neigh!
  458. Why? Because the C<Animal::speak> routine is expecting a classname as
  459. its first parameter, not an instance. When the instance is passed in,
  460. we'll end up using a blessed scalar reference as a string, and that
  461. shows up as we saw it just now.
  462. =head2 Making a method work with either classes or instances
  463. All we need is for a method to detect if it is being called on a class
  464. or called on an instance. The most straightforward way is with the
  465. C<ref> operator. This returns a string (the classname) when used on a
  466. blessed reference, and C<undef> when used on a string (like a
  467. classname). Let's modify the C<name> method first to notice the change:
  468. sub name {
  469. my $either = shift;
  470. ref $either
  471. ? $$either # it's an instance, return name
  472. : "an unnamed $either"; # it's a class, return generic
  473. }
  474. Here, the C<?:> operator comes in handy to select either the
  475. dereference or a derived string. Now we can use this with either an
  476. instance or a class. Note that I've changed the first parameter
  477. holder to C<$either> to show that this is intended:
  478. my $talking = Horse->named("Mr. Ed");
  479. print Horse->name, "\n"; # prints "an unnamed Horse\n"
  480. print $talking->name, "\n"; # prints "Mr Ed.\n"
  481. and now we'll fix C<speak> to use this:
  482. sub speak {
  483. my $either = shift;
  484. print $either->name, " goes ", $either->sound, "\n";
  485. }
  486. And since C<sound> already worked with either a class or an instance,
  487. we're done!
  488. =head2 Adding parameters to a method
  489. Let's train our animals to eat:
  490. { package Animal;
  491. sub named {
  492. my $class = shift;
  493. my $name = shift;
  494. bless \$name, $class;
  495. }
  496. sub name {
  497. my $either = shift;
  498. ref $either
  499. ? $$either # it's an instance, return name
  500. : "an unnamed $either"; # it's a class, return generic
  501. }
  502. sub speak {
  503. my $either = shift;
  504. print $either->name, " goes ", $either->sound, "\n";
  505. }
  506. sub eat {
  507. my $either = shift;
  508. my $food = shift;
  509. print $either->name, " eats $food.\n";
  510. }
  511. }
  512. { package Horse;
  513. @ISA = qw(Animal);
  514. sub sound { "neigh" }
  515. }
  516. { package Sheep;
  517. @ISA = qw(Animal);
  518. sub sound { "baaaah" }
  519. }
  520. And now try it out:
  521. my $talking = Horse->named("Mr. Ed");
  522. $talking->eat("hay");
  523. Sheep->eat("grass");
  524. which prints:
  525. Mr. Ed eats hay.
  526. an unnamed Sheep eats grass.
  527. An instance method with parameters gets invoked with the instance,
  528. and then the list of parameters. So that first invocation is like:
  529. Animal::eat($talking, "hay");
  530. =head2 More interesting instances
  531. What if an instance needs more data? Most interesting instances are
  532. made of many items, each of which can in turn be a reference or even
  533. another object. The easiest way to store these is often in a hash.
  534. The keys of the hash serve as the names of parts of the object (often
  535. called "instance variables" or "member variables"), and the
  536. corresponding values are, well, the values.
  537. But how do we turn the horse into a hash? Recall that an object was
  538. any blessed reference. We can just as easily make it a blessed hash
  539. reference as a blessed scalar reference, as long as everything that
  540. looks at the reference is changed accordingly.
  541. Let's make a sheep that has a name and a color:
  542. my $bad = bless { Name => "Evil", Color => "black" }, Sheep;
  543. so C<< $bad->{Name} >> has C<Evil>, and C<< $bad->{Color} >> has
  544. C<black>. But we want to make C<< $bad->name >> access the name, and
  545. that's now messed up because it's expecting a scalar reference. Not
  546. to worry, because that's pretty easy to fix up:
  547. ## in Animal
  548. sub name {
  549. my $either = shift;
  550. ref $either ?
  551. $either->{Name} :
  552. "an unnamed $either";
  553. }
  554. And of course C<named> still builds a scalar sheep, so let's fix that
  555. as well:
  556. ## in Animal
  557. sub named {
  558. my $class = shift;
  559. my $name = shift;
  560. my $self = { Name => $name, Color => $class->default_color };
  561. bless $self, $class;
  562. }
  563. What's this C<default_color>? Well, if C<named> has only the name,
  564. we still need to set a color, so we'll have a class-specific initial color.
  565. For a sheep, we might define it as white:
  566. ## in Sheep
  567. sub default_color { "white" }
  568. And then to keep from having to define one for each additional class,
  569. we'll define a "backstop" method that serves as the "default default",
  570. directly in C<Animal>:
  571. ## in Animal
  572. sub default_color { "brown" }
  573. Now, because C<name> and C<named> were the only methods that
  574. referenced the "structure" of the object, the rest of the methods can
  575. remain the same, so C<speak> still works as before.
  576. =head2 A horse of a different color
  577. But having all our horses be brown would be boring. So let's add a
  578. method or two to get and set the color.
  579. ## in Animal
  580. sub color {
  581. $_[0]->{Color}
  582. }
  583. sub set_color {
  584. $_[0]->{Color} = $_[1];
  585. }
  586. Note the alternate way of accessing the arguments: C<$_[0]> is used
  587. in-place, rather than with a C<shift>. (This saves us a bit of time
  588. for something that may be invoked frequently.) And now we can fix
  589. that color for Mr. Ed:
  590. my $talking = Horse->named("Mr. Ed");
  591. $talking->set_color("black-and-white");
  592. print $talking->name, " is colored ", $talking->color, "\n";
  593. which results in:
  594. Mr. Ed is colored black-and-white
  595. =head2 Summary
  596. So, now we have class methods, constructors, instance methods,
  597. instance data, and even accessors. But that's still just the
  598. beginning of what Perl has to offer. We haven't even begun to talk
  599. about accessors that double as getters and setters, destructors,
  600. indirect object notation, subclasses that add instance data, per-class
  601. data, overloading, "isa" and "can" tests, C<UNIVERSAL> class, and so
  602. on. That's for the rest of the Perl documentation to cover.
  603. Hopefully, this gets you started, though.
  604. =head1 SEE ALSO
  605. For more information, see L<perlobj> (for all the gritty details about
  606. Perl objects, now that you've seen the basics), L<perltoot> (the
  607. tutorial for those who already know objects), L<perltootc> (dealing
  608. with class data), L<perlbot> (for some more tricks), and books such as
  609. Damian Conway's excellent I<Object Oriented Perl>.
  610. Some modules which might prove interesting are Class::Accessor,
  611. Class::Class, Class::Contract, Class::Data::Inheritable,
  612. Class::MethodMaker and Tie::SecureHash
  613. =head1 COPYRIGHT
  614. Copyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge
  615. Consulting Services, Inc. Permission is hereby granted to distribute
  616. this document intact with the Perl distribution, and in accordance
  617. with the licenses of the Perl distribution; derived documents must
  618. include this copyright notice intact.
  619. Portions of this text have been derived from Perl Training materials
  620. originally appearing in the I<Packages, References, Objects, and
  621. Modules> course taught by instructors for Stonehenge Consulting
  622. Services, Inc. and used with permission.
  623. Portions of this text have been derived from materials originally
  624. appearing in I<Linux Magazine> and used with permission.