This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Inline PI function
[perl5.git] / pod / perltoot.pod
CommitLineData
5f05dabc 1=head1 NAME
2
3perltoot - Tom's object-oriented tutorial for perl
4
5=head1 DESCRIPTION
6
7Object-oriented programming is a big seller these days. Some managers
8would rather have objects than sliced bread. Why is that? What's so
9special about an object? Just what I<is> an object anyway?
10
11An object is nothing but a way of tucking away complex behaviours into
12a neat little easy-to-use bundle. (This is what professors call
13abstraction.) Smart people who have nothing to do but sit around for
14weeks on end figuring out really hard problems make these nifty
15objects that even regular people can use. (This is what professors call
16software reuse.) Users (well, programmers) can play with this little
17bundle all they want, but they aren't to open it up and mess with the
18insides. Just like an expensive piece of hardware, the contract says
19that you void the warranty if you muck with the cover. So don't do that.
20
21The heart of objects is the class, a protected little private namespace
22full of data and functions. A class is a set of related routines that
23addresses some problem area. You can think of it as a user-defined type.
24The Perl package mechanism, also used for more traditional modules,
25is used for class modules as well. Objects "live" in a class, meaning
26that they belong to some package.
27
28More often than not, the class provides the user with little bundles.
29These bundles are objects. They know whose class they belong to,
30and how to behave. Users ask the class to do something, like "give
31me an object." Or they can ask one of these objects to do something.
32Asking a class to do something for you is calling a I<class method>.
33Asking an object to do something for you is calling an I<object method>.
34Asking either a class (usually) or an object (sometimes) to give you
35back an object is calling a I<constructor>, which is just a
36kind of method.
37
38That's all well and good, but how is an object different from any other
39Perl data type? Just what is an object I<really>; that is, what's its
40fundamental type? The answer to the first question is easy. An object
41is different from any other data type in Perl in one and only one way:
42you may dereference it using not merely string or numeric subscripts
43as with simple arrays and hashes, but with named subroutine calls.
44In a word, with I<methods>.
45
46The answer to the second question is that it's a reference, and not just
47any reference, mind you, but one whose referent has been I<bless>()ed
48into a particular class (read: package). What kind of reference? Well,
49the answer to that one is a bit less concrete. That's because in Perl
50the designer of the class can employ any sort of reference they'd like
51as the underlying intrinsic data type. It could be a scalar, an array,
52or a hash reference. It could even be a code reference. But because
53of its inherent flexibility, an object is usually a hash reference.
54
55=head1 Creating a Class
56
57Before you create a class, you need to decide what to name it. That's
58because the class (package) name governs the name of the file used to
59house it, just as with regular modules. Then, that class (package)
60should provide one or more ways to generate objects. Finally, it should
61provide mechanisms to allow users of its objects to indirectly manipulate
62these objects from a distance.
63
64For example, let's make a simple Person class module. It gets stored in
65the file Person.pm. If it were called a Happy::Person class, it would
66be stored in the file Happy/Person.pm, and its package would become
67Happy::Person instead of just Person. (On a personal computer not
68running Unix or Plan 9, but something like MacOS or VMS, the directory
69separator may be different, but the principle is the same.) Do not assume
70any formal relationship between modules based on their directory names.
71This is merely a grouping convenience, and has no effect on inheritance,
72variable accessibility, or anything else.
73
74For this module we aren't going to use Exporter, because we're
75a well-behaved class module that doesn't export anything at all.
76In order to manufacture objects, a class needs to have a I<constructor
77method>. A constructor gives you back not just a regular data type,
78but a brand-new object in that class. This magic is taken care of by
79the bless() function, whose sole purpose is to enable its referent to
80be used as an object. Remember: being an object really means nothing
81more than that methods may now be called against it.
82
83While a constructor may be named anything you'd like, most Perl
84programmers seem to like to call theirs new(). However, new() is not
85a reserved word, and a class is under no obligation to supply such.
86Some programmers have also been known to use a function with
87the same name as the class as the constructor.
88
89=head2 Object Representation
90
91By far the most common mechanism used in Perl to represent a Pascal
92record, a C struct, or a C++ class an anonymous hash. That's because a
93hash has an arbitrary number of data fields, each conveniently accessed by
94an arbitrary name of your own devising.
95
96If you were just doing a simple
97struct-like emulation, you would likely go about it something like this:
98
99 $rec = {
100 name => "Jason",
101 age => 23,
102 peers => [ "Norbert", "Rhys", "Phineas"],
103 };
104
105If you felt like it, you could add a bit of visual distinction
106by up-casing the hash keys:
107
108 $rec = {
109 NAME => "Jason",
110 AGE => 23,
111 PEERS => [ "Norbert", "Rhys", "Phineas"],
112 };
113
114And so you could get at C<$rec-E<gt>{NAME}> to find "Jason", or
115C<@{ $rec-E<gt>{PEERS} }> to get at "Norbert", "Rhys", and "Phineas".
116(Have you ever noticed how many 23-year-old programmers seem to
117be named "Jason" these days? :-)
118
119This same model is often used for classes, although it is not considered
120the pinnacle of programming propriety for folks from outside the
121class to come waltzing into an object, brazenly accessing its data
122members directly. Generally speaking, an object should be considered
123an opaque cookie that you use I<object methods> to access. Visually,
124methods look like you're dereffing a reference using a function name
125instead of brackets or braces.
126
127=head2 Class Interface
128
129Some languages provide a formal syntactic interface to a class's methods,
130but Perl does not. It relies on you to read the documentation of each
131class. If you try to call an undefined method on an object, Perl won't
132complain, but the program will trigger an exception while it's running.
133Likewise, if you call a method expecting a prime number as its argument
8d9aa5e0 134with a non-prime one instead, you can't expect the compiler to catch this.
5f05dabc 135(Well, you can expect it all you like, but it's not going to happen.)
136
137Let's suppose you have a well-educated user of your Person class,
138someone who has read the docs that explain the prescribed
139interface. Here's how they might use the Person class:
140
141 use Person;
142
143 $him = Person->new();
144 $him->name("Jason");
145 $him->age(23);
146 $him->peers( "Norbert", "Rhys", "Phineas" );
147
148 push @All_Recs, $him; # save object in array for later
149
150 printf "%s is %d years old.\n", $him->name, $him->age;
151 print "His peers are: ", join(", ", $him->peers), "\n";
152
153 printf "Last rec's name is %s\n", $All_Recs[-1]->name;
154
155As you can see, the user of the class doesn't know (or at least, has no
156business paying attention to the fact) that the object has one particular
157implementation or another. The interface to the class and its objects
158is exclusively via methods, and that's all the user of the class should
159ever play with.
160
161=head2 Constructors and Instance Methods
162
163Still, I<someone> has to know what's in the object. And that someone is
164the class. It implements methods that the programmer uses to access
165the object. Here's how to implement the Person class using the standard
166hash-ref-as-an-object idiom. We'll make a class method called new() to
167act as the constructor, and three object methods called name(), age(), and
168peers() to get at per-object data hidden away in our anonymous hash.
169
170 package Person;
171 use strict;
172
173 ##################################################
174 ## the object constructor (simplistic version) ##
175 ##################################################
176 sub new {
177 my $self = {};
178 $self->{NAME} = undef;
179 $self->{AGE} = undef;
180 $self->{PEERS} = [];
181 bless($self); # but see below
182 return $self;
183 }
184
185 ##############################################
186 ## methods to access per-object data ##
187 ## ##
188 ## With args, they set the value. Without ##
189 ## any, they only retrieve it/them. ##
190 ##############################################
191
192 sub name {
193 my $self = shift;
194 if (@_) { $self->{NAME} = shift }
195 return $self->{NAME};
196 }
197
198 sub age {
199 my $self = shift;
200 if (@_) { $self->{AGE} = shift }
201 return $self->{AGE};
202 }
203
204 sub peers {
205 my $self = shift;
206 if (@_) { @{ $self->{PEERS} } = @_ }
207 return @{ $self->{PEERS} };
208 }
209
210 1; # so the require or use succeeds
211
212We've created three methods to access an object's data, name(), age(),
213and peers(). These are all substantially similar. If called with an
214argument, they set the appropriate field; otherwise they return the
215value held by that field, meaning the value of that hash key.
216
217=head2 Planning for the Future: Better Constructors
218
219Even though at this point you may not even know what it means, someday
220you're going to worry about inheritance. (You can safely ignore this
221for now and worry about it later if you'd like.) To ensure that this
222all works out smoothly, you must use the double-argument form of bless().
223The second argument is the class into which the referent will be blessed.
224By not assuming our own class as the default second argument and instead
225using the class passed into us, we make our constructor inheritable.
226
227While we're at it, let's make our constructor a bit more flexible.
228Rather than being uniquely a class method, we'll set it up so that
229it can be called as either a class method I<or> an object
230method. That way you can say:
231
232 $me = Person->new();
233 $him = $me->new();
234
235To do this, all we have to do is check whether what was passed in
236was a reference or not. If so, we were invoked as an object method,
237and we need to extract the package (class) using the ref() function.
238If not, we just use the string passed in as the package name
239for blessing our referent.
240
241 sub new {
242 my $proto = shift;
243 my $class = ref($proto) || $proto;
244 my $self = {};
245 $self->{NAME} = undef;
246 $self->{AGE} = undef;
247 $self->{PEERS} = [];
248 bless ($self, $class);
249 return $self;
250 }
251
252That's about all there is for constructors. These methods bring objects
253to life, returning neat little opaque bundles to the user to be used in
254subsequent method calls.
255
256=head2 Destructors
257
258Every story has a beginning and an end. The beginning of the object's
259story is its constructor, explicitly called when the object comes into
260existence. But the ending of its story is the I<destructor>, a method
261implicitly called when an object leaves this life. Any per-object
262clean-up code is placed in the destructor, which must (in Perl) be called
263DESTROY.
264
265If constructors can have arbitrary names, then why not destructors?
266Because while a constructor is explicitly called, a destructor is not.
267Destruction happens automatically via Perl's garbage collection (GC)
268system, which is a quick but somewhat lazy reference-based GC system.
269To know what to call, Perl insists that the destructor be named DESTROY.
270
271Why is DESTROY in all caps? Perl on occasion uses purely upper-case
272function names as a convention to indicate that the function will
273be automatically called by Perl in some way. Others that are called
274implicitly include BEGIN, END, AUTOLOAD, plus all methods used by
275tied objects, described in L<perltie>.
276
277In really good object-oriented programming languages, the user doesn't
278care when the destructor is called. It just happens when it's supposed
279to. In low-level languages without any GC at all, there's no way to
280depend on this happening at the right time, so the programmer must
281explicitly call the destructor to clean up memory and state, crossing
282their fingers that it's the right time to do so. Unlike C++, an
283object destructor is nearly never needed in Perl, and even when it is,
284explicit invocation is uncalled for. In the case of our Person class,
285we don't need a destructor because Perl takes care of simple matters
286like memory deallocation.
287
288The only situation where Perl's reference-based GC won't work is
289when there's a circularity in the data structure, such as:
290
291 $this->{WHATEVER} = $this;
292
293In that case, you must delete the self-reference manually if you expect
294your program not to leak memory. While admittedly error-prone, this is
295the best we can do right now. Nonetheless, rest assured that when your
296program is finished, its objects' destructors are all duly called.
297So you are guaranteed that an object I<eventually> gets properly
298destroyed, except in the unique case of a program that never exits.
299(If you're running Perl embedded in another application, this full GC
300pass happens a bit more frequently--whenever a thread shuts down.)
301
302=head2 Other Object Methods
303
304The methods we've talked about so far have either been constructors or
305else simple "data methods", interfaces to data stored in the object.
306These are a bit like an object's data members in the C++ world, except
307that strangers don't access them as data. Instead, they should only
308access the object's data indirectly via its methods. This is an
309important rule: in Perl, access to an object's data should I<only>
310be made through methods.
311
312Perl doesn't impose restrictions on who gets to use which methods.
313The public-versus-private distinction is by convention, not syntax.
8d9aa5e0
TC
314(Well, unless you use the Alias module described below in
315L</"Data Members as Variables">.) Occasionally you'll see method names beginning or ending
5f05dabc 316with an underscore or two. This marking is a convention indicating
317that the methods are private to that class alone and sometimes to its
318closest acquaintances, its immediate subclasses. But this distinction
319is not enforced by Perl itself. It's up to the programmer to behave.
320
321There's no reason to limit methods to those that simply access data.
322Methods can do anything at all. The key point is that they're invoked
323against an object or a class. Let's say we'd like object methods that
324do more than fetch or set one particular field.
325
326 sub exclaim {
327 my $self = shift;
328 return sprintf "Hi, I'm %s, age %d, working with %s",
329 $self->{NAME}, $self->{AGE}, join(", ", $self->{PEERS});
330 }
331
332Or maybe even one like this:
333
334 sub happy_birthday {
335 my $self = shift;
336 return ++$self->{AGE};
337 }
338
339Some might argue that one should go at these this way:
340
341 sub exclaim {
342 my $self = shift;
343 return sprintf "Hi, I'm %s, age %d, working with %s",
344 $self->name, $self->age, join(", ", $self->peers);
345 }
346
347 sub happy_birthday {
348 my $self = shift;
349 return $self->age( $self->age() + 1 );
350 }
351
352But since these methods are all executing in the class itself, this
353may not be critical. There are trade-offs to be made. Using direct
354hash access is faster (about an order of magnitude faster, in fact), and
355it's more convenient when you want to interpolate in strings. But using
356methods (the external interface) internally shields not just the users of
357your class but even you yourself from changes in your data representation.
358
359=head1 Class Data
360
361What about "class data", data items common to each object in a class?
362What would you want that for? Well, in your Person class, you might
363like to keep track of the total people alive. How do you implement that?
364
365You I<could> make it a global variable called $Person::Census. But about
366only reason you'd do that would be if you I<wanted> people to be able to
367get at your class data directly. They could just say $Person::Census
368and play around with it. Maybe this is ok in your design scheme.
369You might even conceivably want to make it an exported variable. To be
370exportable, a variable must be a (package) global. If this were a
371traditional module rather than an object-oriented one, you might do that.
372
373While this approach is expected in most traditional modules, it's
374generally considered rather poor form in most object modules. In an
375object module, you should set up a protective veil to separate interface
376from implementation. So provide a class method to access class data
377just as you provide object methods to access object data.
378
379So, you I<could> still keep $Census as a package global and rely upon
380others to honor the contract of the module and therefore not play around
381with its implementation. You could even be supertricky and make $Census a
382tied object as described in L<perltie>, thereby intercepting all accesses.
383
384But more often than not, you just want to make your class data a
385file-scoped lexical. To do so, simply put this at the top of the file:
386
387 my $Census = 0;
388
389Even though the scope of a my() normally expires when the block in which
390it was declared is done (in this case the whole file being required or
391used), Perl's deep binding of lexical variables guarantees that the
392variable will not be deallocated, remaining accessible to functions
393declared within that scope. This doesn't work with global variables
394given temporary values via local(), though.
395
396Irrespective of whether you leave $Census a package global or make
397it instead a file-scoped lexical, you should make these
398changes to your Person::new() constructor:
399
400 sub new {
401 my $proto = shift;
402 my $class = ref($proto) || $proto;
403 my $self = {};
404 $Census++;
405 $self->{NAME} = undef;
406 $self->{AGE} = undef;
407 $self->{PEERS} = [];
408 bless ($self, $class);
409 return $self;
410 }
411
412 sub population {
413 return $Census;
414 }
415
416Now that we've done this, we certainly do need a destructor so that
417when Person is destroyed, the $Census goes down. Here's how
418this could be done:
419
420 sub DESTROY { --$Census }
421
422Notice how there's no memory to deallocate in the destructor? That's
423something that Perl takes care of for you all by itself.
424
425=head2 Accessing Class Data
426
427It turns out that this is not really a good way to go about handling
428class data. A good scalable rule is that I<you must never reference class
429data directly from an object method>. Otherwise you aren't building a
430scalable, inheritable class. The object must be the rendezvous point
431for all operations, especially from an object method. The globals
432(class data) would in some sense be in the "wrong" package in your
433derived classes. In Perl, methods execute in the context of the class
434they were defined in, I<not> that of the object that triggered them.
435Therefore, namespace visibility of package globals in methods is unrelated
436to inheritance.
437
438Got that? Maybe not. Ok, let's say that some other class "borrowed"
439(well, inherited) the DESTROY method as it was defined above. When those
8d9aa5e0 440objects are destroyed, the original $Census variable will be altered,
5f05dabc 441not the one in the new class's package namespace. Perhaps this is what
442you want, but probably it isn't.
443
444Here's how to fix this. We'll store a reference to the data in the
445value accessed by the hash key "_CENSUS". Why the underscore? Well,
446mostly because an initial underscore already conveys strong feelings
447of magicalness to a C programmer. It's really just a mnemonic device
448to remind ourselves that this field is special and not to be used as
449a public data member in the same way that NAME, AGE, and PEERS are.
450(Because we've been developing this code under the strict pragma, prior
a6006777 451to perl version 5.004 we'll have to quote the field name.)
5f05dabc 452
453 sub new {
454 my $proto = shift;
455 my $class = ref($proto) || $proto;
456 my $self = {};
457 $self->{NAME} = undef;
458 $self->{AGE} = undef;
459 $self->{PEERS} = [];
460 # "private" data
461 $self->{"_CENSUS"} = \$Census;
462 bless ($self, $class);
463 ++ ${ $self->{"_CENSUS"} };
464 return $self;
465 }
466
467 sub population {
468 my $self = shift;
469 if (ref $self) {
470 return ${ $self->{"_CENSUS"} };
471 } else {
472 return $Census;
473 }
474 }
475
476 sub DESTROY {
477 my $self = shift;
478 -- ${ $self->{"_CENSUS"} };
479 }
480
481=head2 Debugging Methods
482
483It's common for a class to have a debugging mechanism. For example,
484you might want to see when objects are created or destroyed. To do that,
485add a debugging variable as a file-scoped lexical. For this, we'll pull
486in the standard Carp module to emit our warnings and fatal messages.
487That way messages will come out with the caller's filename and
488line number instead of our own; if we wanted them to be from our own
489perspective, we'd just use die() and warn() directly instead of croak()
490and carp() respectively.
491
492 use Carp;
493 my $Debugging = 0;
494
495Now add a new class method to access the variable.
496
497 sub debug {
498 my $class = shift;
499 if (ref $class) { confess "Class method called as object method" }
500 unless (@_ == 1) { confess "usage: CLASSNAME->debug(level)" }
501 $Debugging = shift;
502 }
503
504Now fix up DESTROY to murmur a bit as the moribund object expires:
505
506 sub DESTROY {
507 my $self = shift;
508 if ($Debugging) { carp "Destroying $self " . $self->name }
509 -- ${ $self->{"_CENSUS"} };
510 }
511
512One could conceivably make a per-object debug state. That
513way you could call both of these:
514
515 Person->debug(1); # entire class
516 $him->debug(1); # just this object
517
518To do so, we need our debugging method to be a "bimodal" one, one that
519works on both classes I<and> objects. Therefore, adjust the debug()
520and DESTROY methods as follows:
521
522 sub debug {
523 my $self = shift;
524 confess "usage: thing->debug(level)" unless @_ == 1;
525 my $level = shift;
526 if (ref($self)) {
527 $self->{"_DEBUG"} = $level; # just myself
528 } else {
529 $Debugging = $level; # whole class
530 }
531 }
532
533 sub DESTROY {
534 my $self = shift;
535 if ($Debugging || $self->{"_DEBUG"}) {
536 carp "Destroying $self " . $self->name;
537 }
538 -- ${ $self->{"_CENSUS"} };
539 }
540
8d9aa5e0
TC
541What happens if a derived class (which we'll call Employee) inherits
542methods from this Person base class? Then C<Employee-E<gt>debug()>, when called
543as a class method, manipulates $Person::Debugging not $Employee::Debugging.
5f05dabc 544
545=head2 Class Destructors
546
547The object destructor handles the death of each distinct object. But sometimes
548you want a bit of cleanup when the entire class is shut down, which
549currently only happens when the program exits. To make such a
550I<class destructor>, create a function in that class's package named
551END. This works just like the END function in traditional modules,
552meaning that it gets called whenever your program exits unless it execs
553or dies of an uncaught signal. For example,
554
555 sub END {
556 if ($Debugging) {
557 print "All persons are going away now.\n";
558 }
559 }
560
561When the program exits, all the class destructors (END functions) are
562be called in the opposite order that they were loaded in (LIFO order).
563
564=head2 Documenting the Interface
565
566And there you have it: we've just shown you the I<implementation> of this
567Person class. Its I<interface> would be its documentation. Usually this
568means putting it in pod ("plain old documentation") format right there
569in the same file. In our Person example, we would place the following
570docs anywhere in the Person.pm file. Even though it looks mostly like
571code, it's not. It's embedded documentation such as would be used by
572the pod2man, pod2html, or pod2text programs. The Perl compiler ignores
573pods entirely, just as the translators ignore code. Here's an example of
574some pods describing the informal interface:
575
576 =head1 NAME
577
578 Person - class to implement people
579
580 =head1 SYNOPSIS
581
582 use Person;
583
584 #################
585 # class methods #
586 #################
587 $ob = Person->new;
588 $count = Person->population;
589
590 #######################
591 # object data methods #
592 #######################
593
594 ### get versions ###
595 $who = $ob->name;
596 $years = $ob->age;
597 @pals = $ob->peers;
598
599 ### set versions ###
600 $ob->name("Jason");
601 $ob->age(23);
602 $ob->peers( "Norbert", "Rhys", "Phineas" );
603
604 ########################
605 # other object methods #
606 ########################
607
608 $phrase = $ob->exclaim;
609 $ob->happy_birthday;
610
611 =head1 DESCRIPTION
612
613 The Person class implements dah dee dah dee dah....
614
615That's all there is to the matter of interface versus implementation.
616A programmer who opens up the module and plays around with all the private
617little shiny bits that were safely locked up behind the interface contract
618has voided the warranty, and you shouldn't worry about their fate.
619
620=head1 Aggregation
621
622Suppose you later want to change the class to implement better names.
623Perhaps you'd like to support both given names (called Christian names,
624irrespective of one's religion) and family names (called surnames), plus
625nicknames and titles. If users of your Person class have been properly
626accessing it through its documented interface, then you can easily change
627the underlying implementation. If they haven't, then they lose and
628it's their fault for breaking the contract and voiding their warranty.
629
630To do this, we'll make another class, this one called Fullname. What's
631the Fullname class look like? To answer that question, you have to
632first figure out how you want to use it. How about we use it this way:
633
634 $him = Person->new();
635 $him->fullname->title("St");
636 $him->fullname->christian("Thomas");
637 $him->fullname->surname("Aquinas");
638 $him->fullname->nickname("Tommy");
639 printf "His normal name is %s\n", $him->name;
640 printf "But his real name is %s\n", $him->fullname->as_string;
641
642Ok. To do this, we'll change Person::new() so that it supports
643a full name field this way:
644
645 sub new {
646 my $proto = shift;
647 my $class = ref($proto) || $proto;
648 my $self = {};
649 $self->{FULLNAME} = Fullname->new();
650 $self->{AGE} = undef;
651 $self->{PEERS} = [];
652 $self->{"_CENSUS"} = \$Census;
653 bless ($self, $class);
654 ++ ${ $self->{"_CENSUS"} };
655 return $self;
656 }
657
658 sub fullname {
659 my $self = shift;
660 return $self->{FULLNAME};
661 }
662
663Then to support old code, define Person::name() this way:
664
665 sub name {
666 my $self = shift;
667 return $self->{FULLNAME}->nickname(@_)
668 || $self->{FULLNAME}->christian(@_);
669 }
670
671Here's the Fullname class. We'll use the same technique
672of using a hash reference to hold data fields, and methods
673by the appropriate name to access them:
674
675 package Fullname;
676 use strict;
677
678 sub new {
679 my $proto = shift;
680 my $class = ref($proto) || $proto;
681 my $self = {
682 TITLE => undef,
683 CHRISTIAN => undef,
684 SURNAME => undef,
685 NICK => undef,
686 };
687 bless ($self, $class);
688 return $self;
689 }
690
691 sub christian {
692 my $self = shift;
693 if (@_) { $self->{CHRISTIAN} = shift }
694 return $self->{CHRISTIAN};
695 }
696
697 sub surname {
698 my $self = shift;
699 if (@_) { $self->{SURNAME} = shift }
700 return $self->{SURNAME};
701 }
702
703 sub nickname {
704 my $self = shift;
705 if (@_) { $self->{NICK} = shift }
706 return $self->{NICK};
707 }
708
709 sub title {
710 my $self = shift;
711 if (@_) { $self->{TITLE} = shift }
712 return $self->{TITLE};
713 }
714
715 sub as_string {
716 my $self = shift;
717 my $name = join(" ", @$self{'CHRISTIAN', 'SURNAME'});
718 if ($self->{TITLE}) {
719 $name = $self->{TITLE} . " " . $name;
720 }
721 return $name;
722 }
723
724 1;
725
726Finally, here's the test program:
727
728 #!/usr/bin/perl -w
729 use strict;
730 use Person;
731 sub END { show_census() }
732
733 sub show_census () {
734 printf "Current population: %d\n", Person->population;
735 }
736
737 Person->debug(1);
738
739 show_census();
740
741 my $him = Person->new();
742
743 $him->fullname->christian("Thomas");
744 $him->fullname->surname("Aquinas");
745 $him->fullname->nickname("Tommy");
746 $him->fullname->title("St");
747 $him->age(1);
748
749 printf "%s is really %s.\n", $him->name, $him->fullname;
750 printf "%s's age: %d.\n", $him->name, $him->age;
751 $him->happy_birthday;
752 printf "%s's age: %d.\n", $him->name, $him->age;
753
754 show_census();
755
756=head1 Inheritance
757
758Object-oriented programming systems all support some notion of
759inheritance. Inheritance means allowing one class to piggy-back on
760top of another one so you don't have to write the same code again and
761again. It's about software reuse, and therefore related to Laziness,
762the principal virtue of a programmer. (The import/export mechanisms in
763traditional modules are also a form of code reuse, but a simpler one than
764the true inheritance that you find in object modules.)
765
766Sometimes the syntax of inheritance is built into the core of the
767language, and sometimes it's not. Perl has no special syntax for
768specifying the class (or classes) to inherit from. Instead, it's all
769strictly in the semantics. Each package can have a variable called @ISA,
770which governs (method) inheritance. If you try to call a method on an
771object or class, and that method is not found in that object's package,
772Perl then looks to @ISA for other packages to go looking through in
773search of the missing method.
774
775Like the special per-package variables recognized by Exporter (such as
776@EXPORT, @EXPORT_OK, @EXPORT_FAIL, %EXPORT_TAGS, and $VERSION), the @ISA
777array I<must> be a package-scoped global and not a file-scoped lexical
778created via my(). Most classes have just one item in their @ISA array.
779In this case, we have what's called "single inheritance", or SI for short.
780
781Consider this class:
782
783 package Employee;
784 use Person;
785 @ISA = ("Person");
786 1;
787
788Not a lot to it, eh? All it's doing so far is loading in another
789class and stating that this one will inherit methods from that
790other class if need be. We have given it none of its own methods.
791We rely upon an Employee to behave just like a Person.
792
793Setting up an empty class like this is called the "empty subclass test";
794that is, making a derived class that does nothing but inherit from a
795base class. If the original base class has been designed properly,
796then the new derived class can be used as a drop-in replacement for the
797old one. This means you should be able to write a program like this:
798
799 use Employee
800 my $empl = Employee->new();
801 $empl->name("Jason");
802 $empl->age(23);
803 printf "%s is age %d.\n", $empl->name, $empl->age;
804
805By proper design, we mean always using the two-argument form of bless(),
806avoiding direct access of global data, and not exporting anything. If you
807look back at the Person::new() function we defined above, we were careful
808to do that. There's a bit of package data used in the constructor,
809but the reference to this is stored on the object itself and all other
810methods access package data via that reference, so we should be ok.
811
812What do we mean by the Person::new() function -- isn't that actually
813a method? Well, in principle, yes. A method is just a function that
814expects as its first argument a class name (package) or object
815(blessed reference). Person::new() is the function that both the
816C<Person-E<gt>new()> method and the C<Employee-E<gt>new()> method end
817up calling. Understand that while a method call looks a lot like a
818function call, they aren't really quite the same, and if you treat them
819as the same, you'll very soon be left with nothing but broken programs.
820First, the actual underlying calling conventions are different: method
821calls get an extra argument. Second, function calls don't do inheritance,
822but methods do.
823
824 Method Call Resulting Function Call
825 ----------- ------------------------
826 Person->new() Person::new("Person")
827 Employee->new() Person::new("Employee")
828
829So don't use function calls when you mean to call a method.
830
831If an employee is just a Person, that's not all too very interesting.
832So let's add some other methods. We'll give our employee
833data fields to access their salary, their employee ID, and their
834start date.
835
836If you're getting a little tired of creating all these nearly identical
837methods just to get at the object's data, do not despair. Later,
838we'll describe several different convenience mechanisms for shortening
839this up. Meanwhile, here's the straight-forward way:
840
841 sub salary {
842 my $self = shift;
843 if (@_) { $self->{SALARY} = shift }
844 return $self->{SALARY};
845 }
846
847 sub id_number {
848 my $self = shift;
849 if (@_) { $self->{ID} = shift }
850 return $self->{ID};
851 }
852
853 sub start_date {
854 my $self = shift;
855 if (@_) { $self->{START_DATE} = shift }
856 return $self->{START_DATE};
857 }
858
859=head2 Overridden Methods
860
861What happens when both a derived class and its base class have the same
862method defined? Well, then you get the derived class's version of that
863method. For example, let's say that we want the peers() method called on
864an employee to act a bit differently. Instead of just returning the list
865of peer names, let's return slightly different strings. So doing this:
866
867 $empl->peers("Peter", "Paul", "Mary");
868 printf "His peers are: %s\n", join(", ", $empl->peers);
869
870will produce:
871
872 His peers are: PEON=PETER, PEON=PAUL, PEON=MARY
873
874To do this, merely add this definition into the Employee.pm file:
875
876 sub peers {
877 my $self = shift;
878 if (@_) { @{ $self->{PEERS} } = @_ }
879 return map { "PEON=\U$_" } @{ $self->{PEERS} };
880 }
881
882There, we've just demonstrated the high-falutin' concept known in certain
883circles as I<polymorphism>. We've taken on the form and behaviour of
884an existing object, and then we've altered it to suit our own purposes.
885This is a form of Laziness. (Getting polymorphed is also what happens
886when the wizard decides you'd look better as a frog.)
887
888Every now and then you'll want to have a method call trigger both its
889derived class (also know as "subclass") version as well as its base class
890(also known as "superclass") version. In practice, constructors and
891destructors are likely to want to do this, and it probably also makes
892sense in the debug() method we showed previously.
893
894To do this, add this to Employee.pm:
895
896 use Carp;
897 my $Debugging = 0;
898
899 sub debug {
900 my $self = shift;
901 confess "usage: thing->debug(level)" unless @_ == 1;
902 my $level = shift;
903 if (ref($self)) {
904 $self->{"_DEBUG"} = $level;
905 } else {
906 $Debugging = $level; # whole class
907 }
908 Person::debug($self, $Debugging); # don't really do this
909 }
910
911As you see, we turn around and call the Person package's debug() function.
912But this is far too fragile for good design. What if Person doesn't
913have a debug() function, but is inheriting I<its> debug() method
914from elsewhere? It would have been slightly better to say
915
916 Person->debug($Debugging);
917
918But even that's got too much hard-coded. It's somewhat better to say
919
920 $self->Person::debug($Debugging);
921
922Which is a funny way to say to start looking for a debug() method up
923in Person. This strategy is more often seen on overridden object methods
924than on overridden class methods.
925
926There is still something a bit off here. We've hard-coded our
927superclass's name. This in particular is bad if you change which classes
928you inherit from, or add others. Fortunately, the pseudoclass SUPER
929comes to the rescue here.
930
71be2cbc 931 $self->SUPER::debug($Debugging);
5f05dabc 932
933This way it starts looking in my class's @ISA. This only makes sense
934from I<within> a method call, though. Don't try to access anything
935in SUPER:: from anywhere else, because it doesn't exist outside
936an overridden method call.
937
938Things are getting a bit complicated here. Have we done anything
939we shouldn't? As before, one way to test whether we're designing
940a decent class is via the empty subclass test. Since we already have
941an Employee class that we're trying to check, we'd better get a new
942empty subclass that can derive from Employee. Here's one:
943
944 package Boss;
945 use Employee; # :-)
946 @ISA = qw(Employee);
947
948And here's the test program:
949
950 #!/usr/bin/perl -w
951 use strict;
952 use Boss;
953 Boss->debug(1);
954
955 my $boss = Boss->new();
956
957 $boss->fullname->title("Don");
958 $boss->fullname->surname("Pichon Alvarez");
959 $boss->fullname->christian("Federico Jesus");
960 $boss->fullname->nickname("Fred");
961
962 $boss->age(47);
963 $boss->peers("Frank", "Felipe", "Faust");
964
965 printf "%s is age %d.\n", $boss->fullname, $boss->age;
966 printf "His peers are: %s\n", join(", ", $boss->peers);
967
968Running it, we see that we're still ok. If you'd like to dump out your
969object in a nice format, somewhat like the way the 'x' command works in
970the debugger, you could use the Data::Dumper module from CPAN this way:
971
972 use Data::Dumper;
973 print "Here's the boss:\n";
974 print Dumper($boss);
975
976Which shows us something like this:
977
978 Here's the boss:
979 $VAR1 = bless( {
980 _CENSUS => \1,
981 FULLNAME => bless( {
982 TITLE => 'Don',
983 SURNAME => 'Pichon Alvarez',
984 NICK => 'Fred',
985 CHRISTIAN => 'Federico Jesus'
986 }, 'Fullname' ),
987 AGE => 47,
988 PEERS => [
989 'Frank',
990 'Felipe',
991 'Faust'
992 ]
993 }, 'Boss' );
994
995Hm.... something's missing there. What about the salary, start date,
996and ID fields? Well, we never set them to anything, even undef, so they
997don't show up in the hash's keys. The Employee class has no new() method
998of its own, and the new() method in Person doesn't know about Employees.
999(Nor should it: proper OO design dictates that a subclass be allowed to
1000know about its immediate superclass, but never vice-versa.) So let's
1001fix up Employee::new() this way:
1002
1003 sub new {
1004 my $proto = shift;
1005 my $class = ref($proto) || $proto;
1006 my $self = $class->SUPER::new();
1007 $self->{SALARY} = undef;
1008 $self->{ID} = undef;
1009 $self->{START_DATE} = undef;
1010 bless ($self, $class); # reconsecrate
1011 return $self;
1012 }
1013
1014Now if you dump out an Employee or Boss object, you'll find
1015that new fields show up there now.
1016
1017=head2 Multiple Inheritance
1018
1019Ok, at the risk of confusing beginners and annoying OO gurus, it's
1020time to confess that Perl's object system includes that controversial
1021notion known as multiple inheritance, or MI for short. All this means
1022is that rather than having just one parent class who in turn might
1023itself have a parent class, etc., that you can directly inherit from
1024two or more parents. It's true that some uses of MI can get you into
1025trouble, although hopefully not quite so much trouble with Perl as with
1026dubiously-OO languages like C++.
1027
1028The way it works is actually pretty simple: just put more than one package
1029name in your @ISA array. When it comes time for Perl to go finding
1030methods for your object, it looks at each of these packages in order.
1031Well, kinda. It's actually a fully recursive, depth-first order.
1032Consider a bunch of @ISA arrays like this:
1033
1034 @First::ISA = qw( Alpha );
1035 @Second::ISA = qw( Beta );
1036 @Third::ISA = qw( First Second );
1037
1038If you have an object of class Third:
1039
1040 my $ob = Third->new();
1041 $ob->spin();
1042
1043How do we find a spin() method (or a new() method for that matter)?
1044Because the search is depth-first, classes will be looked up
1045in the following order: Third, First, Alpha, Second, and Beta.
1046
1047In practice, few class modules have been seen that actually
1048make use of MI. One nearly always chooses simple containership of
1049one class within another over MI. That's why our Person
1050object I<contained> a Fullname object. That doesn't mean
1051it I<was> one.
1052
1053However, there is one particular area where MI in Perl is rampant:
1054borrowing another class's class methods. This is rather common,
1055especially with some bundled "objectless" classes,
1056like Exporter, DynaLoader, AutoLoader, and SelfLoader. These classes
1057do not provide constructors; they exist only so you may inherit their
1058class methods. (It's not entirely clear why inheritance was done
1059here rather than traditional module importation.)
1060
1061For example, here is the POSIX module's @ISA:
1062
1063 package POSIX;
1064 @ISA = qw(Exporter DynaLoader);
1065
1066The POSIX module isn't really an object module, but then,
1067neither are Exporter or DynaLoader. They're just lending their
1068classes' behaviours to POSIX.
1069
1070Why don't people use MI for object methods much? One reason is that
1071it can have complicated side-effects. For one thing, your inheritance
1072graph (no longer a tree) might converge back to the same base class.
1073Although Perl guards against recursive inheritance, merely having parents
1074who are related to each other via a common ancestor, incestuous though
1075it sounds, is not forbidden. What if in our Third class shown above we
1076wanted its new() method to also call both overridden constructors in its
1077two parent classes? The SUPER notation would only find the first one.
1078Also, what about if the Alpha and Beta classes both had a common ancestor,
1079like Nought? If you kept climbing up the inheritance tree calling
1080overridden methods, you'd end up calling Nought::new() twice,
1081which might well be a bad idea.
1082
1083=head2 UNIVERSAL: The Root of All Objects
1084
1085Wouldn't it be convenient if all objects were rooted at some ultimate
1086base class? That way you could give every object common methods without
1087having to go and add it to each and every @ISA. Well, it turns out that
1088you can. You don't see it, but Perl tacitly and irrevocably assumes
1089that there's an extra element at the end of @ISA: the class UNIVERSAL.
a6006777 1090In version 5.003, there were no predefined methods there, but you could put
5f05dabc 1091whatever you felt like into it.
1092
a6006777 1093However, as of version 5.004 (or some subversive releases, like 5.003_08),
5f05dabc 1094UNIVERSAL has some methods in it already. These are built-in to your Perl
1095binary, so they don't take any extra time to load. Predefined methods
1096include isa(), can(), and VERSION(). isa() tells you whether an object or
1097class "is" another one without having to traverse the hierarchy yourself:
1098
1099 $has_io = $fd->isa("IO::Handle");
1100 $itza_handle = IO::Socket->isa("IO::Handle");
1101
1102The can() method, called against that object or class, reports back
1103whether its string argument is a callable method name in that class.
1104In fact, it gives you back a function reference to that method:
1105
1106 $his_print_method = $obj->can('as_string');
1107
1108Finally, the VERSION method checks whether the class (or the object's
1109class) has a package global called $VERSION that's high enough, as in:
1110
1111 Some_Module->VERSION(3.0);
1112 $his_vers = $ob->VERSION();
1113
1114However, we don't usually call VERSION ourselves. (Remember that an all
1115upper-case function name is a Perl convention that indicates that the
1116function will be automatically used by Perl in some way.) In this case,
1117it happens when you say
1118
1119 use Some_Module 3.0;
1120
8d9aa5e0 1121If you wanted to add version checking to your Person class explained
5f05dabc 1122above, just add this to Person.pm:
1123
1124 use vars qw($VERSION);
1125 $VERSION = '1.1';
1126
1127and then in Employee.pm could you can say
1128
1129 use Employee 1.1;
1130
1131And it would make sure that you have at least that version number or
1132higher available. This is not the same as loading in that exact version
1133number. No mechanism currently exists for concurrent installation of
1134multiple versions of a module. Lamentably.
1135
1136=head1 Alternate Object Representations
1137
1138Nothing requires objects to be implemented as hash references. An object
1139can be any sort of reference so long as its referent has been suitably
1140blessed. That means scalar, array, and code references are also fair
1141game.
1142
1143A scalar would work if the object has only one datum to hold. An array
1144would work for most cases, but makes inheritance a bit dodgy because
1145you have to invent new indices for the derived classes.
1146
1147=head2 Arrays as Objects
1148
1149If the user of your class honors the contract and sticks to the advertised
1150interface, then you can change its underlying interface if you feel
1151like it. Here's another implementation that conforms to the same
1152interface specification. This time we'll use an array reference
1153instead of a hash reference to represent the object.
1154
1155 package Person;
1156 use strict;
1157
1158 my($NAME, $AGE, $PEERS) = ( 0 .. 2 );
1159
1160 ############################################
1161 ## the object constructor (array version) ##
1162 ############################################
1163 sub new {
1164 my $self = [];
1165 $self->[$NAME] = undef; # this is unnecessary
1166 $self->[$AGE] = undef; # as it this
1167 $self->[$PEERS] = []; # but this isn't, really
1168 bless($self);
1169 return $self;
1170 }
1171
1172 sub name {
1173 my $self = shift;
1174 if (@_) { $self->[$NAME] = shift }
1175 return $self->[$NAME];
1176 }
1177
1178 sub age {
1179 my $self = shift;
1180 if (@_) { $self->[$AGE] = shift }
1181 return $self->[$AGE];
1182 }
1183
1184 sub peers {
1185 my $self = shift;
1186 if (@_) { @{ $self->[$PEERS] } = @_ }
1187 return @{ $self->[$PEERS] };
1188 }
1189
1190 1; # so the require or use succeeds
1191
8d9aa5e0
TC
1192You might guess that the array access would be a lot faster than the
1193hash access, but they're actually comparable. The array is a I<little>
5f05dabc 1194bit faster, but not more than ten or fifteen percent, even when you
1195replace the variables above like $AGE with literal numbers, like 1.
1196A bigger difference between the two approaches can be found in memory use.
1197A hash representation takes up more memory than an array representation
8d9aa5e0 1198because you have to allocate memory for the keys as well as for the values.
a6006777 1199However, it really isn't that bad, especially since as of version 5.004,
5f05dabc 1200memory is only allocated once for a given hash key, no matter how many
1201hashes have that key. It's expected that sometime in the future, even
1202these differences will fade into obscurity as more efficient underlying
1203representations are devised.
1204
1205Still, the tiny edge in speed (and somewhat larger one in memory)
1206is enough to make some programmers choose an array representation
1207for simple classes. There's still a little problem with
1208scalability, though, because later in life when you feel
1209like creating subclasses, you'll find that hashes just work
1210out better.
1211
1212=head2 Closures as Objects
1213
1214Using a code reference to represent an object offers some fascinating
1215possibilities. We can create a new anonymous function (closure) who
1216alone in all the world can see the object's data. This is because we
1217put the data into an anonymous hash that's lexically visible only to
1218the closure we create, bless, and return as the object. This object's
1219methods turn around and call the closure as a regular subroutine call,
1220passing it the field we want to affect. (Yes,
1221the double-function call is slow, but if you wanted fast, you wouldn't
1222be using objects at all, eh? :-)
1223
1224Use would be similar to before:
1225
1226 use Person;
1227 $him = Person->new();
1228 $him->name("Jason");
1229 $him->age(23);
1230 $him->peers( [ "Norbert", "Rhys", "Phineas" ] );
1231 printf "%s is %d years old.\n", $him->name, $him->age;
1232 print "His peers are: ", join(", ", @{$him->peers}), "\n";
1233
1234but the implementation would be radically, perhaps even sublimely
1235different:
1236
1237 package Person;
1238
1239 sub new {
1240 my $that = shift;
1241 my $class = ref($that) || $that;
1242 my $self = {
1243 NAME => undef,
1244 AGE => undef,
1245 PEERS => [],
1246 };
1247 my $closure = sub {
1248 my $field = shift;
1249 if (@_) { $self->{$field} = shift }
1250 return $self->{$field};
1251 };
1252 bless($closure, $class);
1253 return $closure;
1254 }
1255
1256 sub name { &{ $_[0] }("NAME", @_[ 1 .. $#_ ] ) }
1257 sub age { &{ $_[0] }("AGE", @_[ 1 .. $#_ ] ) }
1258 sub peers { &{ $_[0] }("PEERS", @_[ 1 .. $#_ ] ) }
1259
1260 1;
1261
1262Because this object is hidden behind a code reference, it's probably a bit
1263mysterious to those whose background is more firmly rooted in standard
1264procedural or object-based programming languages than in functional
1265programming languages whence closures derive. The object
1266created and returned by the new() method is itself not a data reference
1267as we've seen before. It's an anonymous code reference that has within
1268it access to a specific version (lexical binding and instantiation)
1269of the object's data, which are stored in the private variable $self.
1270Although this is the same function each time, it contains a different
1271version of $self.
1272
1273When a method like C<$him-E<gt>name("Jason")> is called, its implicit
8d9aa5e0 1274zeroth argument is the invoking object--just as it is with all method
5f05dabc 1275calls. But in this case, it's our code reference (something like a
1276function pointer in C++, but with deep binding of lexical variables).
1277There's not a lot to be done with a code reference beyond calling it, so
1278that's just what we do when we say C<&{$_[0]}>. This is just a regular
1279function call, not a method call. The initial argument is the string
1280"NAME", and any remaining arguments are whatever had been passed to the
1281method itself.
1282
1283Once we're executing inside the closure that had been created in new(),
1284the $self hash reference suddenly becomes visible. The closure grabs
1285its first argument ("NAME" in this case because that's what the name()
1286method passed it), and uses that string to subscript into the private
1287hash hidden in its unique version of $self.
1288
1289Nothing under the sun will allow anyone outside the executing method to
1290be able to get at this hidden data. Well, nearly nothing. You I<could>
1291single step through the program using the debugger and find out the
1292pieces while you're in the method, but everyone else is out of luck.
1293
1294There, if that doesn't excite the Scheme folks, then I just don't know
1295what will. Translation of this technique into C++, Java, or any other
1296braindead-static language is left as a futile exercise for aficionados
1297of those camps.
1298
1299You could even add a bit of nosiness via the caller() function and
1300make the closure refuse to operate unless called via its own package.
1301This would no doubt satisfy certain fastidious concerns of programming
1302police and related puritans.
1303
1304If you were wondering when Hubris, the third principle virtue of a
1305programmer, would come into play, here you have it. (More seriously,
1306Hubris is just the pride in craftsmanship that comes from having written
1307a sound bit of well-designed code.)
1308
1309=head1 AUTOLOAD: Proxy Methods
1310
1311Autoloading is a way to intercept calls to undefined methods. An autoload
1312routine may choose to create a new function on the fly, either loaded
1313from disk or perhaps just eval()ed right there. This define-on-the-fly
1314strategy is why it's called autoloading.
1315
1316But that's only one possible approach. Another one is to just
1317have the autoloaded method itself directly provide the
1318requested service. When used in this way, you may think
1319of autoloaded methods as "proxy" methods.
1320
1321When Perl tries to call an undefined function in a particular package
1322and that function is not defined, it looks for a function in
1323that same package called AUTOLOAD. If one exists, it's called
1324with the same arguments as the original function would have had.
1325The fully-qualified name of the function is stored in that package's
1326global variable $AUTOLOAD. Once called, the function can do anything
1327it would like, including defining a new function by the right name, and
1328then doing a really fancy kind of C<goto> right to it, erasing itself
1329from the call stack.
1330
1331What does this have to do with objects? After all, we keep talking about
1332functions, not methods. Well, since a method is just a function with
1333an extra argument and some fancier semantics about where it's found,
1334we can use autoloading for methods, too. Perl doesn't start looking
1335for an AUTOLOAD method until it has exhausted the recursive hunt up
1336through @ISA, though. Some programmers have even been known to define
1337a UNIVERSAL::AUTOLOAD method to trap unresolved method calls to any
1338kind of object.
1339
1340=head2 Autoloaded Data Methods
1341
1342You probably began to get a little suspicious about the duplicated
1343code way back earlier when we first showed you the Person class, and
1344then later the Employee class. Each method used to access the
1345hash fields looked virtually identical. This should have tickled
1346that great programming virtue, Impatience, but for the time,
1347we let Laziness win out, and so did nothing. Proxy methods can cure
1348this.
1349
1350Instead of writing a new function every time we want a new data field,
1351we'll use the autoload mechanism to generate (actually, mimic) methods on
1352the fly. To verify that we're accessing a valid member, we will check
1353against an C<_permitted> (pronounced "under-permitted") field, which
1354is a reference to a file-scoped lexical (like a C file static) hash of permitted fields in this record
1355called %fields. Why the underscore? For the same reason as the _CENSUS
1356field we once used: as a marker that means "for internal use only".
1357
1358Here's what the module initialization code and class
1359constructor will look like when taking this approach:
1360
1361 package Person;
1362 use Carp;
1363 use vars qw($AUTOLOAD); # it's a package global
1364
1365 my %fields = (
1366 name => undef,
1367 age => undef,
1368 peers => undef,
1369 );
1370
1371 sub new {
1372 my $that = shift;
1373 my $class = ref($that) || $that;
1374 my $self = {
1375 _permitted => \%fields,
1376 %fields,
1377 };
1378 bless $self, $class;
1379 return $self;
1380 }
1381
1382If we wanted our record to have default values, we could fill those in
1383where current we have C<undef> in the %fields hash.
1384
1385Notice how we saved a reference to our class data on the object itself?
1386Remember that it's important to access class data through the object
1387itself instead of having any method reference %fields directly, or else
1388you won't have a decent inheritance.
1389
1390The real magic, though, is going to reside in our proxy method, which
1391will handle all calls to undefined methods for objects of class Person
1392(or subclasses of Person). It has to be called AUTOLOAD. Again, it's
1393all caps because it's called for us implicitly by Perl itself, not by
1394a user directly.
1395
1396 sub AUTOLOAD {
1397 my $self = shift;
1398 my $type = ref($self)
1399 or croak "$self is not an object";
1400
1401 my $name = $AUTOLOAD;
1402 $name =~ s/.*://; # strip fully-qualified portion
1403
1404 unless (exists $self->{_permitted}->{$name} ) {
1405 croak "Can't access `$name' field in class $type";
1406 }
1407
1408 if (@_) {
1409 return $self->{$name} = shift;
1410 } else {
1411 return $self->{$name};
1412 }
1413 }
1414
1415Pretty nifty, eh? All we have to do to add new data fields
1416is modify %fields. No new functions need be written.
1417
1418I could have avoided the C<_permitted> field entirely, but I
1419wanted to demonstrate how to store a reference to class data on the
1420object so you wouldn't have to access that class data
1421directly from an object method.
1422
1423=head2 Inherited Autoloaded Data Methods
1424
1425But what about inheritance? Can we define our Employee
1426class similarly? Yes, so long as we're careful enough.
1427
1428Here's how to be careful:
1429
1430 package Employee;
1431 use Person;
1432 use strict;
1433 use vars qw(@ISA);
1434 @ISA = qw(Person);
1435
1436 my %fields = (
1437 id => undef,
1438 salary => undef,
1439 );
1440
1441 sub new {
1442 my $that = shift;
1443 my $class = ref($that) || $that;
1444 my $self = bless $that->SUPER::new(), $class;
1445 my($element);
1446 foreach $element (keys %fields) {
1447 $self->{_permitted}->{$element} = $fields{$element};
1448 }
1449 @{$self}{keys %fields} = values %fields;
1450 return $self;
1451 }
1452
1453Once we've done this, we don't even need to have an
1454AUTOLOAD function in the Employee package, because
1455we'll grab Person's version of that via inheritance,
1456and it will all work out just fine.
1457
1458=head1 Metaclassical Tools
1459
1460Even though proxy methods can provide a more convenient approach to making
1461more struct-like classes than tediously coding up data methods as
1462functions, it still leaves a bit to be desired. For one thing, it means
1463you have to handle bogus calls that you don't mean to trap via your proxy.
1464It also means you have to be quite careful when dealing with inheritance,
1465as detailed above.
1466
1467Perl programmers have responded to this by creating several different
1468class construction classes. These metaclasses are classes
1469that create other classes. A couple worth looking at are
1470Class::Template and Alias. These and other related metaclasses can be
1471found in the modules directory on CPAN.
1472
1473=head2 Class::Template
1474
1475One of the older ones is Class::Template. In fact, its syntax and
1476interface were sketched out long before perl5 even solidified into a
8d9aa5e0
TC
1477real thing. What it does is provide you a way to "declare" a class
1478as having objects whose fields are of a specific type. The function
1479that does this is called, not surprisingly enough, struct(). Because
1480structures or records are not base types in Perl, each time you want to
1481create a class to provide a record-like data object, you yourself have
1482to define a new() method, plus separate data-access methods for each of
1483that record's fields. You'll quickly become bored with this process.
1484The Class::Template::struct() function alleviates this tedium.
5f05dabc 1485
1486Here's a simple example of using it:
1487
1488 use Class::Template qw(struct);
1489 use Jobbie; # user-defined; see below
1490
1491 struct 'Fred' => {
1492 one => '$',
1493 many => '@',
1494 profession => Jobbie, # calls Jobbie->new()
1495 };
1496
1497 $ob = Fred->new;
1498 $ob->one("hmmmm");
1499
1500 $ob->many(0, "here");
1501 $ob->many(1, "you");
1502 $ob->many(2, "go");
1503 print "Just set: ", $ob->many(2), "\n";
1504
1505 $ob->profession->salary(10_000);
1506
1507You can declare types in the struct to be basic Perl types, or
1508user-defined types (classes). User types will be initialized by calling
1509that class's new() method.
1510
1511Here's a real-world example of using struct generation. Let's say you
1512wanted to override Perl's idea of gethostbyname() and gethostbyaddr() so
1513that they would return objects that acted like C structures. We don't
1514care about high-falutin' OO gunk. All we want is for these objects to
1515act like structs in the C sense.
1516
1517 use Socket;
1518 use Net::hostent;
1519 $h = gethostbyname("perl.com"); # object return
1520 printf "perl.com's real name is %s, address %s\n",
1521 $h->name, inet_ntoa($h->addr);
1522
1523Here's how to do this using the Class::Template module.
1524The crux is going to be this call:
1525
1526 struct 'Net::hostent' => [ # note bracket
1527 name => '$',
1528 aliases => '@',
1529 addrtype => '$',
1530 'length' => '$',
1531 addr_list => '@',
1532 ];
1533
1534Which creates object methods of those names and types.
1535It even creates a new() method for us.
1536
1537We could also have implemented our object this way:
1538
1539 struct 'Net::hostent' => { # note brace
1540 name => '$',
1541 aliases => '@',
1542 addrtype => '$',
1543 'length' => '$',
1544 addr_list => '@',
1545 };
1546
1547and then Class::Template would have used an anonymous hash as the object
1548type, instead of an anonymous array. The array is faster and smaller,
1549but the hash works out better if you eventually want to do inheritance.
1550Since for this struct-like object we aren't planning on inheritance,
1551this time we'll opt for better speed and size over better flexibility.
1552
1553Here's the whole implementation:
1554
1555 package Net::hostent;
1556 use strict;
1557
1558 BEGIN {
1559 use Exporter ();
1560 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
1561 @ISA = qw(Exporter);
1562 @EXPORT = qw(gethostbyname gethostbyaddr gethost);
1563 @EXPORT_OK = qw(
1564 $h_name @h_aliases
1565 $h_addrtype $h_length
1566 @h_addr_list $h_addr
1567 );
1568 %EXPORT_TAGS = ( FIELDS => [ @EXPORT_OK, @EXPORT ] );
1569 }
1570 use vars @EXPORT_OK;
1571
1572 use Class::Template qw(struct);
1573 struct 'Net::hostent' => [
1574 name => '$',
1575 aliases => '@',
1576 addrtype => '$',
1577 'length' => '$',
1578 addr_list => '@',
1579 ];
1580
1581 sub addr { shift->addr_list->[0] }
1582
1583 sub populate (@) {
1584 return unless @_;
1585 my $hob = new(); # Class::Template made this!
1586 $h_name = $hob->[0] = $_[0];
1587 @h_aliases = @{ $hob->[1] } = split ' ', $_[1];
1588 $h_addrtype = $hob->[2] = $_[2];
1589 $h_length = $hob->[3] = $_[3];
1590 $h_addr = $_[4];
1591 @h_addr_list = @{ $hob->[4] } = @_[ (4 .. $#_) ];
1592 return $hob;
1593 }
1594
1595 sub gethostbyname ($) { populate(CORE::gethostbyname(shift)) }
1596
1597 sub gethostbyaddr ($;$) {
1598 my ($addr, $addrtype);
1599 $addr = shift;
1600 require Socket unless @_;
1601 $addrtype = @_ ? shift : Socket::AF_INET();
1602 populate(CORE::gethostbyaddr($addr, $addrtype))
1603 }
1604
1605 sub gethost($) {
1606 if ($_[0] =~ /^\d+(?:\.\d+(?:\.\d+(?:\.\d+)?)?)?$/) {
1607 require Socket;
1608 &gethostbyaddr(Socket::inet_aton(shift));
1609 } else {
1610 &gethostbyname;
1611 }
1612 }
1613
1614 1;
1615
1616We've snuck in quite a fair bit of other concepts besides just dynamic
1617class creation, like overriding core functions, import/export bits,
1618function prototyping, and short-cut function call via C<&whatever>.
1619These all mostly make sense from the perspective of a traditional module,
1620but as you can see, we can also use them in an object module.
1621
1622You can look at other object-based, struct-like overrides of core
1623functions in the 5.004 release of Perl in File::stat, Net::hostent,
1624Net::netent, Net::protoent, Net::servent, Time::gmtime, Time::localtime,
1625User::grent, and User::pwent. These modules have a final component
1626that's all lower-case, by convention reserved for compiler pragmas,
1627because they affect the compilation and change a built-in function.
1628They also have the type names that a C programmer would most expect.
1629
1630=head2 Data Members as Variables
1631
1632If you're used to C++ objects, then you're accustomed to being able to
1633get at an object's data members as simple variables from within a method.
1634The Alias module provides for this, as well as a good bit more, such
1635as the possibility of private methods that the object can call but folks
1636outside the class cannot.
1637
1638Here's an example of creating a Person using the Alias module.
1639When you update these magical instance variables, you automatically
1640update value fields in the hash. Convenient, eh?
1641
1642 package Person;
1643
1644 # this is the same as before...
1645 sub new {
1646 my $that = shift;
1647 my $class = ref($that) || $that;
1648 my $self = {
1649 NAME => undef,
1650 AGE => undef,
1651 PEERS => [],
1652 };
1653 bless($self, $class);
1654 return $self;
1655 }
1656
1657 use Alias qw(attr);
1658 use vars qw($NAME $AGE $PEERS);
1659
1660 sub name {
1661 my $self = attr shift;
1662 if (@_) { $NAME = shift; }
1663 return $NAME;
1664 }
1665
1666 sub age {
1667 my $self = attr shift;
1668 if (@_) { $AGE = shift; }
1669 return $AGE;
1670 }
1671
1672 sub peers {
1673 my $self = attr shift;
1674 if (@_) { @PEERS = @_; }
1675 return @PEERS;
1676 }
1677
1678 sub exclaim {
1679 my $self = attr shift;
1680 return sprintf "Hi, I'm %s, age %d, working with %s",
1681 $NAME, $AGE, join(", ", @PEERS);
1682 }
1683
1684 sub happy_birthday {
1685 my $self = attr shift;
1686 return ++$AGE;
1687 }
1688
1689The need for the C<use vars> declaration is because what Alias does
1690is play with package globals with the same name as the fields. To use
1691globals while C<use strict> is in effect, you have to pre-declare them.
1692These package variables are localized to the block enclosing the attr()
1693call just as if you'd used a local() on them. However, that means that
1694they're still considered global variables with temporary values, just
1695as with any other local().
1696
1697It would be nice to combine Alias with
1698something like Class::Template or Class::MethodMaker.
1699
1700=head2 NOTES
1701
1702=head2 Object Terminology
1703
1704In the various OO literature, it seems that a lot of different words
1705are used to describe only a few different concepts. If you're not
1706already an object programmer, then you don't need to worry about all
1707these fancy words. But if you are, then you might like to know how to
1708get at the same concepts in Perl.
1709
1710For example, it's common to call an object an I<instance> of a class
1711and to call those objects' methods I<instance methods>. Data fields
1712peculiar to each object are often called I<instance data> or I<object
1713attributes>, and data fields common to all members of that class are
1714I<class data>, I<class attributes>, or I<static data members>.
1715
1716Also, I<base class>, I<generic class>, and I<superclass> all describe
1717the same notion, whereas I<derived class>, I<specific class>, and
1718I<subclass> describe the other related one.
1719
1720C++ programmers have I<static methods> and I<virtual methods>,
1721but Perl only has I<class methods> and I<object methods>.
1722Actually, Perl only has methods. Whether a method gets used
1723as a class or object method is by usage only. You could accidentally
1724call a class method (one expecting a string argument) on an
1725object (one expecting a reference), or vice versa.
1726
8d9aa5e0 1727Z<>From the C++ perspective, all methods in Perl are virtual.
5f05dabc 1728This, by the way, is why they are never checked for function
1729prototypes in the argument list as regular built-in and user-defined
1730functions can be.
1731
1732Because a class is itself something of an object, Perl's classes can be
1733taken as describing both a "class as meta-object" (also called I<object
1734factory>) philosophy and the "class as type definition" (I<declaring>
1735behaviour, not I<defining> mechanism) idea. C++ supports the latter
1736notion, but not the former.
1737
1738=head1 SEE ALSO
1739
1740The following man pages will doubtless provide more
1741background for this one:
1742L<perlmod>,
1743L<perlref>,
1744L<perlobj>,
1745L<perlbot>,
1746L<perltie>,
1747and
1748L<overload>.
1749
1750=head1 COPYRIGHT
1751
1752I I<really> hate to have to say this, but recent unpleasant
1753experiences have mandated its inclusion:
1754
1755 Copyright 1996 Tom Christiansen. All Rights Reserved.
1756
1757This work derives in part from the second edition of I<Programming Perl>.
1758Although destined for release as a man page with the standard Perl
1759distribution, it is not public domain (nor is any of Perl and its docset:
1760publishers beware). It's expected to someday make its way into a revision
1761of the Camel Book. While it is copyright by me with all rights reserved,
1762permission is granted to freely distribute verbatim copies of this
1763document provided that no modifications outside of formatting be made,
1764and that this notice remain intact. You are permitted and encouraged to
1765use its code and derivatives thereof in your own source code for fun or
1766for profit as you see fit. But so help me, if in six months I find some
1767book out there with a hacked-up version of this material in it claiming to
1768be written by someone else, I'll tell all the world that you're a jerk.
1769Furthermore, your lawyer will meet my lawyer (or O'Reilly's) over lunch
1770to arrange for you to receive your just deserts. Count on it.
1771
1772=head2 Acknowledgments
1773
1774Thanks to
1775Larry Wall,
1776Roderick Schertler,
1777Gurusamy Sarathy,
1778Dean Roehrich,
1779Raphael Manfredi,
1780Brent Halsey,
1781Greg Bacon,
1782Brad Appleton,
1783and many others for their helpful comments.