This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl 5.000
[perl5.git] / pod / perlobj.pod
CommitLineData
a0d0e21e
LW
1=head1 NAME
2
3perlobj - Perl objects
4
5=head1 DESCRIPTION
6
7First of all, you need to understand what references are in Perl. See
8L<perlref> for that.
9
10Here are three very simple definitions that you should find reassuring.
11
12=over 4
13
14=item 1.
15
16An object is simply a reference that happens to know which class it
17belongs to.
18
19=item 2.
20
21A class is simply a package that happens to provide methods to deal
22with object references.
23
24=item 3.
25
26A method is simply a subroutine that expects an object reference (or
27a package name, for static methods) as the first argument.
28
29=back
30
31We'll cover these points now in more depth.
32
33=head2 An Object is Simply a Reference
34
35Unlike say C++, Perl doesn't provide any special syntax for
36constructors. A constructor is merely a subroutine that returns a
37reference that has been "blessed" into a class, generally the
38class that the subroutine is defined in. Here is a typical
39constructor:
40
41 package Critter;
42 sub new { bless {} }
43
44The C<{}> constructs a reference to an anonymous hash containing no
45key/value pairs. The bless() takes that reference and tells the object
46it references that it's now a Critter, and returns the reference.
47This is for convenience, since the referenced object itself knows that
48it has been blessed, and its reference to it could have been returned
49directly, like this:
50
51 sub new {
52 my $self = {};
53 bless $self;
54 return $self;
55 }
56
57In fact, you often see such a thing in more complicated constructors
58that wish to call methods in the class as part of the construction:
59
60 sub new {
61 my $self = {}
62 bless $self;
63 $self->initialize();
64 $self;
65 }
66
67Within the class package, the methods will typically deal with the
68reference as an ordinary reference. Outside the class package,
69the reference is generally treated as an opaque value that may
70only be accessed through the class's methods.
71
72A constructor may rebless a referenced object currently belonging to
73another class, but then the new class is responsible for all cleanup
74later. The previous blessing is forgotten, as an object may only
75belong to one class at a time. (Although of course it's free to
76inherit methods from many classes.)
77
78A clarification: Perl objects are blessed. References are not. Objects
79know which package they belong to. References do not. The bless()
80function simply uses the reference in order to find the object. Consider
81the following example:
82
83 $a = {};
84 $b = $a;
85 bless $a, BLAH;
86 print "\$b is a ", ref($b), "\n";
87
88This reports $b as being a BLAH, so obviously bless()
89operated on the object and not on the reference.
90
91=head2 A Class is Simply a Package
92
93Unlike say C++, Perl doesn't provide any special syntax for class
94definitions. You just use a package as a class by putting method
95definitions into the class.
96
97There is a special array within each package called @ISA which says
98where else to look for a method if you can't find it in the current
99package. This is how Perl implements inheritance. Each element of the
100@ISA array is just the name of another package that happens to be a
101class package. The classes are searched (depth first) for missing
102methods in the order that they occur in @ISA. The classes accessible
103through @ISA are known as base classes of the current class.
104
105If a missing method is found in one of the base classes, it is cached
106in the current class for efficiency. Changing @ISA or defining new
107subroutines invalidates the cache and causes Perl to do the lookup again.
108
109If a method isn't found, but an AUTOLOAD routine is found, then
110that is called on behalf of the missing method.
111
112If neither a method nor an AUTOLOAD routine is found in @ISA, then one
113last try is made for the method (or an AUTOLOAD routine) in a class
114called UNIVERSAL. If that doesn't work, Perl finally gives up and
115complains.
116
117Perl classes only do method inheritance. Data inheritance is left
118up to the class itself. By and large, this is not a problem in Perl,
119because most classes model the attributes of their object using
120an anonymous hash, which serves as its own little namespace to be
121carved up by the various classes that might want to do something
122with the object.
123
124=head2 A Method is Simply a Subroutine
125
126Unlike say C++, Perl doesn't provide any special syntax for method
127definition. (It does provide a little syntax for method invocation
128though. More on that later.) A method expects its first argument
129to be the object or package it is being invoked on. There are just two
130types of methods, which we'll call static and virtual, in honor of
131the two C++ method types they most closely resemble.
132
133A static method expects a class name as the first argument. It
134provides functionality for the class as a whole, not for any individual
135object belonging to the class. Constructors are typically static
136methods. Many static methods simply ignore their first argument, since
137they already know what package they're in, and don't care what package
138they were invoked via. (These aren't necessarily the same, since
139static methods follow the inheritance tree just like ordinary virtual
140methods.) Another typical use for static methods is to look up an
141object by name:
142
143 sub find {
144 my ($class, $name) = @_;
145 $objtable{$name};
146 }
147
148A virtual method expects an object reference as its first argument.
149Typically it shifts the first argument into a "self" or "this" variable,
150and then uses that as an ordinary reference.
151
152 sub display {
153 my $self = shift;
154 my @keys = @_ ? @_ : sort keys %$self;
155 foreach $key (@keys) {
156 print "\t$key => $self->{$key}\n";
157 }
158 }
159
160=head2 Method Invocation
161
162There are two ways to invoke a method, one of which you're already
163familiar with, and the other of which will look familiar. Perl 4
164already had an "indirect object" syntax that you use when you say
165
166 print STDERR "help!!!\n";
167
168This same syntax can be used to call either static or virtual methods.
169We'll use the two methods defined above, the static method to lookup
170an object reference and the virtual method to print out its attributes.
171
172 $fred = find Critter "Fred";
173 display $fred 'Height', 'Weight';
174
175These could be combined into one statement by using a BLOCK in the
176indirect object slot:
177
178 display {find Critter "Fred"} 'Height', 'Weight';
179
180For C++ fans, there's also a syntax using -> notation that does exactly
181the same thing. The parentheses are required if there are any arguments.
182
183 $fred = Critter->find("Fred");
184 $fred->display('Height', 'Weight');
185
186or in one statement,
187
188 Critter->find("Fred")->display('Height', 'Weight');
189
190There are times when one syntax is more readable, and times when the
191other syntax is more readable. The indirect object syntax is less
192cluttered, but it has the same ambiguity as ordinary list operators.
193Indirect object method calls are parsed using the same rule as list
194operators: "If it looks like a function, it is a function". (Presuming
195for the moment that you think two words in a row can look like a
196function name. C++ programmers seem to think so with some regularity,
197especially when the first word is "new".) Thus, the parens of
198
199 new Critter ('Barney', 1.5, 70)
200
201are assumed to surround ALL the arguments of the method call, regardless
202of what comes after. Saying
203
204 new Critter ('Bam' x 2), 1.4, 45
205
206would be equivalent to
207
208 Critter->new('Bam' x 2), 1.4, 45
209
210which is unlikely to do what you want.
211
212There are times when you wish to specify which class's method to use.
213In this case, you can call your method as an ordinary subroutine
214call, being sure to pass the requisite first argument explicitly:
215
216 $fred = MyCritter::find("Critter", "Fred");
217 MyCritter::display($fred, 'Height', 'Weight');
218
219Note however, that this does not do any inheritance. If you merely
220wish to specify that Perl should I<START> looking for a method in a
221particular package, use an ordinary method call, but qualify the method
222name with the package like this:
223
224 $fred = Critter->MyCritter::find("Fred");
225 $fred->MyCritter::display('Height', 'Weight');
226
227=head2 Destructors
228
229When the last reference to an object goes away, the object is
230automatically destroyed. (This may even be after you exit, if you've
231stored references in global variables.) If you want to capture control
232just before the object is freed, you may define a DESTROY method in
233your class. It will automatically be called at the appropriate moment,
234and you can do any extra cleanup you need to do.
235
236Perl doesn't do nested destruction for you. If your constructor
237reblessed a reference from one of your base classes, your DESTROY may
238need to call DESTROY for any base classes that need it. But this only
239applies to reblessed objects--an object reference that is merely
240I<CONTAINED> in the current object will be freed and destroyed
241automatically when the current object is freed.
242
243=head2 Summary
244
245That's about all there is to it. Now you just need to go off and buy a
246book about object-oriented design methodology, and bang your forehead
247with it for the next six months or so.
248
249=head1 SEE ALSO
250
251You should also check out L<perlbot> for other object tricks, traps, and tips.