This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Show that blessed comes from Scalar::Util in code example
[perl5.git] / pod / perlobj.pod
CommitLineData
af36000c
DR
1=encoding utf8
2
3=for comment
4Consistent formatting of this file is achieved with:
5 perl ./Porting/podtidy pod/perlobj.pod
6
a0d0e21e 7=head1 NAME
d74e8afc 8X<object> X<OOP>
a0d0e21e 9
af36000c 10perlobj - Perl object reference
a0d0e21e
LW
11
12=head1 DESCRIPTION
13
af36000c
DR
14This document provides a reference for Perl's object orientation
15features. If you're looking for an introduction to object-oriented
16programming in Perl, please see L<perlootut>.
17
18In order to understand Perl objects, you first need to understand
19references in Perl. See L<perlref> for details.
a0d0e21e 20
8d26c1a8
DR
21This document describes all of Perl's object-oriented (OO) features
22from the ground up. If you're just looking to write some
23object-oriented code of your own, you are probably better served by
24using one of the object systems from CPAN described in L<perlootut>.
af36000c
DR
25
26If you're looking to write your own object system, or you need to
27maintain code which implements objects from scratch then this document
28will help you understand exactly how Perl does object orientation.
29
30There are a few basic principles which define object oriented Perl:
a0d0e21e
LW
31
32=over 4
33
34=item 1.
35
7168b251
DC
36An object is simply a data structure that knows to which class it
37belongs.
a0d0e21e
LW
38
39=item 2.
40
af36000c
DR
41A class is simply a package. A class provides methods that expect to
42operate on objects.
a0d0e21e
LW
43
44=item 3.
45
7168b251
DC
46A method is simply a subroutine that expects a reference to an object
47(or a package name, for class methods) as the first argument.
a0d0e21e
LW
48
49=back
50
af36000c 51Let's look at each of these principles in depth.
a0d0e21e 52
7168b251 53=head2 An Object is Simply a Data Structure
d74e8afc 54X<object> X<bless> X<constructor> X<new>
a0d0e21e 55
af36000c 56Unlike many other languages which support object orientation, Perl does
7168b251
DC
57not provide any special syntax for constructing an object. Objects are
58merely Perl data structures (hashes, arrays, scalars, filehandles,
59etc.) that have been explicitly associated with a particular class.
60
61That explicit association is created by the built-in C<bless> function,
62which is typically used within the I<constructor> subroutine of the
63class.
a0d0e21e 64
af36000c 65Here is a simple constructor:
a0d0e21e 66
af36000c 67 package File;
5a964f20 68
af36000c
DR
69 sub new {
70 my $class = shift;
a0d0e21e 71
af36000c
DR
72 return bless {}, $class;
73 }
5a964f20 74
af36000c
DR
75The name C<new> isn't special. We could name our constructor something
76else:
5a964f20 77
7168b251
DC
78 package File;
79
af36000c
DR
80 sub load {
81 my $class = shift;
82
83 return bless {}, $class;
84 }
85
7168b251
DC
86The modern convention for OO modules is to always use C<new> as the
87name for the constructor, but there is no requirement to do so. Any
88subroutine that blesses a data structure into a class is a valid
89constructor in Perl.
af36000c 90
7168b251
DC
91In the previous examples, the C<{}> code creates a reference to an
92empty anonymous hash. The C<bless> function then takes that reference
93and associates the hash with the class in C<$class>. In the simplest
94case, the C<$class> variable will end up containing the string "File".
af36000c 95
7168b251
DC
96We can also use a variable to store a reference to the data structure
97that is being blessed as our object:
af36000c
DR
98
99 sub new {
100 my $class = shift;
101
102 my $self = {};
103 bless $self, $class;
104
105 return $self;
106 }
107
7168b251
DC
108Once we've blessed the hash referred to by C<$self> we can start
109calling methods on it. This is useful if you want to put object
110initialization in its own separate method:
af36000c
DR
111
112 sub new {
113 my $class = shift;
114
115 my $self = {};
116 bless $self, $class;
117
118 $self->_initialize();
119
120 return $self;
121 }
ed850460 122
7168b251
DC
123Since the object is also a hash, you can treat it as one, using it to
124store data associated with the object. Typically, code inside the class
125can treat the hash as an accessible data structure, while code outside
126the class should always treat the object as opaque. This is called
af36000c
DR
127B<encapsulation>. Encapsulation means that the user of an object does
128not have to know how it is implemented. The user simply calls
129documented methods on the object.
ed850460 130
7168b251
DC
131Note, however, that (unlike most other OO languages) Perl does not
132ensure or enforce encapsulation in any way. If you want objects to
133actually I<be> opaque you need to arrange for that yourself. This can
134be done in a varierty of ways, including using L<"Inside-Out objects">
135or modules from CPAN.
136
8d26c1a8
DR
137=head3 Objects Are Blessed; Variables Are Not
138
139When we bless something, we are not blessing the variable which
7168b251
DC
140contains a reference to that thing, nor are we blessing the reference
141that the variable stores; we are blessing the thing that the variable
142refers to (sometimes known as the I<referent>). This is best
143demonstrated with this code:
8d26c1a8 144
bcaf8a4b
DR
145 use Scalar::Util 'blessed';
146
8d26c1a8
DR
147 my $foo = {};
148 my $bar = $foo;
149
150 bless $foo, 'Class';
7168b251 151 print blessed( $bar ); # prints "Class"
8d26c1a8 152
7168b251
DC
153 $bar = "some other value";
154 print blessed( $bar ); # prints undef
155
156When we call C<bless> on a variable, we are actually blessing the
157underlying data structure that the variable refers to. We are not
158blessing the reference itself, nor the variable that contains that
159reference. That's why the second call to C<blessed( $bar )> returns
160false. At that point C<$bar> is no longer storing a reference to an
161object.
162
163You will sometimes see older books or documentation mention "blessing a
164reference" or describe an object as a "blessed reference", but this is
165incorrect. It isn't the reference that is blessed as an object; it's
166the thing the reference refers to (i.e. the referent).
8d26c1a8 167
af36000c
DR
168=head2 A Class is Simply a Package
169X<class> X<package> X<@ISA> X<inheritance>
ed850460 170
af36000c
DR
171Perl does not provide any special syntax for class definitions. A
172package is simply a namespace containing variables and subroutines. The
7168b251
DC
173only difference is that in a class, the subroutines may expect a
174reference to an object or the name of a class as the first argument.
175This is purely a matter of convention, so a class may contain both
176methods and subroutines which I<don't> operate on an object or class.
af36000c 177
8d26c1a8
DR
178Each package contains a special array called C<@ISA>. The C<@ISA> array
179contains a list of that class's parent classes, if any. This array is
180examined when Perl does method resolution, which we will cover later.
af36000c
DR
181
182It is possible to manually set C<@ISA>, and you may see this in older
8d26c1a8
DR
183Perl code. Much older code also uses the L<base> pragma. For new code,
184we recommend that you use the L<parent> pragma to declare your parents.
185This pragma will take care of setting C<@ISA>. It will also load the
186parent classes and make sure that the package doesn't inherit from
187itself.
188
189However the parent classes are set, the package's C<@ISA> variable will
190contain a list of those parents.
af36000c
DR
191
192All classes inherit from the L<UNIVERSAL> class implicitly. The
193L<UNIVERSAL> class is implemented by the Perl core, and provides
194several default methods, such as C<isa()>, C<can()>, and C<VERSION()>.
8d26c1a8
DR
195The C<UNIVERSAL> class will I<never> appear in a package's C<@ISA>
196variable.
5a964f20 197
af36000c
DR
198Perl I<only> provides method inheritance as a built-in feature.
199Attribute inheritance is left up the class to implement. See the
8d26c1a8 200L</Writing Accessors> section for details.
a0d0e21e
LW
201
202=head2 A Method is Simply a Subroutine
d74e8afc 203X<method>
a0d0e21e 204
af36000c
DR
205Perl does not provide any special syntax for defining a method. A
206method is simply a regular subroutine, and is declared with C<sub>.
207What makes a method special is that it expects to receive either an
208object or a class name as its first argument.
209
210Perl I<does> provide special syntax for method invocation, the C<< ->
211>> operator. We will cover this in more detail later.
212
213Most methods you write will expect to operate on objects:
214
215 sub save {
216 my $self = shift;
217
7168b251
DC
218 open my $fh, '>', $self->path() or die $!;
219 print {$fh} $self->data() or die $!;
220 close $fh or die $!;
af36000c 221 }
a0d0e21e
LW
222
223=head2 Method Invocation
d74e8afc 224X<invocation> X<method> X<arrow> X<< -> >>
a0d0e21e 225
7168b251
DC
226Calling a method on an object is written as C<< $object->method >>.
227
228The left hand side of the method invocation (or arrow) operator is the
af36000c 229object (or class name), and the right hand side is the method name.
a0d0e21e 230
af36000c
DR
231 my $pod = File->new( 'perlobj.pod', $data );
232 $pod->save();
a0d0e21e 233
7168b251 234The C<< -> >> syntax is also used when dereferencing a reference. It
8d26c1a8 235looks like the same operator, but these are two different operations.
a0d0e21e 236
af36000c
DR
237When you call a method, the thing on the left side of the arrow is
238passed as the first argument to the method. That means when we call C<<
239Critter->new() >>, the C<new()> method receives the string C<"Critter">
240as its first argument. When we call C<< $fred->speak() >>, the C<$fred>
8d26c1a8
DR
241variable is passed as the first argument to C<speak()>.
242
243Just as with any Perl subroutine, all of the arguments passed in C<@_>
244are aliases to the original argument. This includes the object itself.
245If you assign directly to C<$_[0]> you will change the contents of the
7168b251
DC
246variable that holds the reference to the object. We recommend that you
247don't do this unless you know exactly what you're doing.
a0d0e21e 248
af36000c
DR
249Perl knows what package the method is in by looking at the left side of
250the arrow. If the left hand side is a package name, it looks for the
7168b251
DC
251method in that package. If the left hand side is an object, then Perl
252looks for the method in the package that the object has been blessed
253into.
a0d0e21e 254
af36000c
DR
255If the left hand side is neither a package name nor an object, then the
256method call will cause an error, but see the section on L</Method Call
257Variations> for more nuances.
a0d0e21e 258
af36000c
DR
259=head2 Inheritance
260X<inheritance>
a0d0e21e 261
af36000c
DR
262We already talked about the special C<@ISA> array and the L<parent>
263pragma.
a0d0e21e 264
af36000c 265When a class inherits from another class, any methods defined in the
7168b251 266parent class are available to the child class. If you attempt to call a
af36000c
DR
267method on an object that isn't defined in its own class, Perl will also
268look for that method in any parent classes it may have.
a0d0e21e 269
af36000c
DR
270 package File::MP3;
271 use parent 'File'; # sets @File::MP3::ISA = ('File');
272
273 my $mp3 = File::MP3->new( 'Andvari.mp3', $data );
274 $mp3->save();
275
276Since we didn't define a C<save()> method in the C<File::MP3> class,
277Perl will look at the C<File::MP3> class's parent classes to find the
8d26c1a8
DR
278C<save()> method. If Perl cannot find a C<save()> method anywhere in
279the inheritance hierarchy, it will die.
af36000c 280
7168b251
DC
281In this case, it finds a C<save()> method in the C<File> class. Note
282that the object passed to C<save()> in this case is still a
283C<File::MP3> object, even though the method is found in the C<File>
284class.
a0d0e21e 285
af36000c
DR
286We can override a parent's method in a child class. When we do so, we
287can still call the parent class's method with the C<SUPER>
288pseudo-class.
a0d0e21e 289
af36000c
DR
290 sub save {
291 my $self = shift;
a0d0e21e 292
af36000c
DR
293 say 'Prepare to rock';
294 $self->SUPER::save();
295 }
296
297The C<SUPER> modifier can I<only> be used for method calls. You can't
7168b251
DC
298use it for regular subroutine calls or class methods:
299
300 SUPER::save($thing); # FAIL: looks for save() sub in package SUPER
301
2a014a4b
KW
302 SUPER->save($thing); # FAIL: looks for save() method in class
303 # SUPER
7168b251 304
2a014a4b
KW
305 $thing->SUPER::save(); # Okay: looks for save() method in parent
306 # classes
af36000c 307
af36000c
DR
308
309=head3 How SUPER is Resolved
d74e8afc 310X<SUPER>
029f3b44 311
af36000c
DR
312The C<SUPER> pseudo-class is resolved from the package where the call
313is made. It is I<not> resolved based on the object's class. This is
7168b251
DC
314important, because it lets methods at different levels within a deep
315inheritance hierarchy each correctly call their respective parent
316methods.
af36000c
DR
317
318 package A;
319
320 sub new {
321 return bless {}, shift;
322 }
323
324 sub speak {
325 my $self = shift;
326
327 $self->SUPER::speak();
328
329 say 'A';
330 }
331
332 package B;
333
334 use parent 'A';
335
336 sub speak {
337 my $self = shift;
029f3b44 338
af36000c 339 $self->SUPER::speak();
a0d0e21e 340
af36000c
DR
341 say 'B';
342 }
a0d0e21e 343
af36000c 344 package C;
cb1a09d0 345
af36000c 346 use parent 'B';
cb1a09d0 347
af36000c
DR
348 sub speak {
349 my $self = shift;
d9693f5a 350
af36000c 351 $self->SUPER::speak();
e947c198 352
af36000c
DR
353 say 'C';
354 }
e947c198 355
af36000c
DR
356 my $c = C->new();
357 $c->speak();
e947c198 358
7168b251 359In this example, we will get the following output:
cb1a09d0 360
af36000c
DR
361 A
362 B
363 C
748a9306 364
af36000c
DR
365This demonstrates how C<SUPER> is resolved. Even though the object is
366blessed into the C<C> class, the C<speak()> method in the C<B> class
7168b251
DC
367can still call C<SUPER::speak()> and expect it to correctly look in the
368parent class of C<B> (i.e the class the method call is in), not in the
369parent class of C<C> (i.e. the class the object belongs to).
19799a22 370
7168b251
DC
371There are rare cases where this package-based resolution can be a
372problem. If you copy a subroutine from one package to another, C<SUPER>
af36000c 373resolution will be done based on the original package.
19799a22 374
af36000c
DR
375=head3 Multiple Inheritance
376X<multiple inheritance>
19799a22 377
af36000c 378Multiple inheritance often indicates a design problem, but Perl always
7168b251 379give you enough rope to hang yourself with if you really need to.
19799a22 380
af36000c
DR
381To declare multiple parents, you simply need to pass multiple class
382names to C<use parent>:
5d9f8747 383
af36000c 384 package MultiChild;
5d9f8747 385
af36000c 386 use parent 'Parent1', 'Parent2';
19799a22 387
af36000c
DR
388=head3 Method Resolution Order
389X<method resolution order> X<mro>
390
391Method resolution order only matters in the case of multiple
392inheritance. In the case of single inheritance, Perl simply looks up
393the inheritance chain to find a method:
394
395 Grandparent
7168b251 396 |
af36000c 397 Parent
7168b251 398 |
af36000c
DR
399 Child
400
401If we call a method on a C<Child> object and that method is not defined
7168b251
DC
402in the C<Child> class, Perl will look for that method in the C<Parent>
403class and then, if necessary, in the C<Grandparent> class.
af36000c
DR
404
405If Perl cannot find the method in any of these classes, it will die
406with an error message.
407
408When a class has multiple parents, the method lookup order becomes more
409complicated.
410
411By default, Perl does a depth-first left-to-right search for a method.
412That means it starts with the first parent in the C<@ISA> array, and
7168b251
DC
413then searches all of its parents, grandparents, etc. If it fails to
414find the method, it then goes to the next parent in the original
415class's C<@ISA> array and searches from there.
af36000c 416
7168b251
DC
417 SharedGreatGrandParent
418 / \
af36000c
DR
419 PaternalGrandparent MaternalGrandparent
420 \ /
421 Father Mother
422 \ /
423 Child
424
425So given the diagram above, Perl will search C<Child>, C<Father>,
7168b251
DC
426C<PaternalGrandparent>, C<SharedGreatGrandParent>, C<Mother>, and
427finally C<MaternalGrandparent> This is a problem because now we're
428looking in C<SharedGreatGrandParent> I<before> we've checked all its
429derived classes (i.e. before we tried C<Mother> and
430C<MaternalGrandparent>).
af36000c
DR
431
432It is possible to ask for a different method resolution order with the
433L<mro> pragma.
434
435 package Child;
436
437 use mro 'c3';
438 use parent 'Father', 'Mother';
439
440This pragma lets you switch to the "C3" resolution order. In simple
7168b251
DC
441terms, "C3" order ensures that parent classes are never searched before
442child classes, so Perl will now search: C<Child>, C<Father>,
443C<PaternalGrandparent>, C<Mother> C<MaternalGrandparent>, and finally
444C<SharedGreatGrandParent>. Note however that this is not
445"breadth-first" searching: All the C<Father> ancestors (except the
446common ancestor) are searched before any of the C<Mother> ancestors are
447considered.
af36000c
DR
448
449The C3 order also lets you call methods in sibling classes with the
450C<next> pseudo-class. See the L<mro> documentation for more details on
451this feature.
452
453=head3 Method Resolution Caching
454
455When Perl searches for a method, it caches the lookup so that future
456calls to the method do not need to search for it again. Changing a
457class's parent class or adding subroutines to a class will invalidate
458the cache for that class.
459
460The L<mro> pragma provides some functions for manipulating the method
461cache directly.
462
463=head2 Writing Constructors
464X<constructor>
465
466As we mentioned earlier, Perl provides no special constructor syntax.
467This means that a class must implement its own constructor. A
7168b251
DC
468constructor is simply a class method that returns a reference to a new
469object.
af36000c
DR
470
471The constructor can also accept additional parameters that define the
472object. Let's write a real constructor for the C<File> class we used
473earlier:
474
475 package File;
476
477 sub new {
478 my $class = shift;
479 my ( $path, $data ) = @_;
480
481 my $self = bless {
482 path => $path,
483 data => $data,
484 }, $class;
485
486 return $self;
487 }
488
489As you can see, we've stored the path and file data in the object
7168b251
DC
490itself. Remember, under the hood, this object is still just a hash.
491Later, we'll write accessors to manipulate this data.
af36000c
DR
492
493For our File::MP3 class, we can check to make sure that the path we're
494given ends with ".mp3":
495
496 package File::MP3;
497
498 sub new {
499 my $class = shift;
500 my ( $path, $data ) = @_;
501
502 die "You cannot create a File::MP3 without an mp3 extension\n"
503 unless $path =~ /\.mp3\z/;
504
505 return $class->SUPER::new(@_);
506 }
507
508This constructor lets its parent class do the actual object
509construction.
510
8d26c1a8
DR
511=head2 Attributes
512X<attribute>
513
514An attribute is a piece of data belonging to a particular object.
515Unlike most object-oriented languages, Perl provides no special syntax
516or support for declaring and manipulating attributes.
517
518Attributes are often stored in the object itself. For example, if the
7168b251
DC
519object is an anonymous hash, we can store the attribute values in the
520hash using the attribute name as the key.
8d26c1a8
DR
521
522While it's possible to refer directly to these hash keys outside of the
523class, it's considered a best practice to wrap all access to the
524attribute with accessor methods.
525
7168b251 526This has several advantages. Accessors make it easier to change the
8d26c1a8
DR
527implementation of an object later while still preserving the original
528API.
529
530An accessor lets you add additional code around attribute access. For
531example, you could apply a default to an attribute that wasn't set in
532the constructor, or you could validate that a new value for the
533attribute is acceptable.
534
535Finally, using accessors makes inheritance much simpler. Subclasses can
536use the accessors rather than having to know the inner details of the
537object.
538
539=head3 Writing Accessors
af36000c
DR
540X<accessor>
541
542As with constructors, Perl provides no special accessor declaration
543syntax, so classes must write them by hand. There are two common types
544of accessors, read-only and read-write.
545
546A simple read-only accessor simply gets the value of a single
547attribute:
548
549 sub path {
550 my $self = shift;
551
552 return $self->{path};
553 }
554
555A read-write accessor will allow the caller to set the value as well as
556get it:
557
558 sub path {
559 my $self = shift;
560
561 if (@_) {
562 $self->{path} = shift;
563 }
564
565 return $self->{path};
566 }
567
568=head2 An Aside About Smarter and Safer Code
569
570Our constructor and accessors are not very smart. They don't check that
571a C<$path> is defined, nor do they check that a C<$path> is a valid
572filesystem path.
573
574Doing these checks by hand can quickly become tedious. Writing a bunch
8d26c1a8 575of accessors by hand is also incredibly tedious. There are a lot of
af36000c
DR
576modules on CPAN that can help you write safer and more concise code,
577including the modules we recommend in L<perlootut>.
578
579=head2 Method Call Variations
580X<method>
19799a22 581
af36000c
DR
582Perl supports several other ways to call methods besides the typical
583C<< $object->method() >> pattern we've seen so far.
19799a22 584
af36000c 585=head3 Method Names as Strings
19799a22 586
af36000c
DR
587Perl lets you use a scalar variable containing a string as a method
588name:
19799a22 589
af36000c 590 my $file = File->new( $path, $data );
19799a22 591
af36000c
DR
592 my $method = 'save';
593 $file->$method();
19799a22 594
af36000c
DR
595This works exactly like calling C<< $file->save() >>. This can be very
596useful for writing dynamic code. For example, it allows you to pass a
597method name to be called as a parameter to another method.
5d9f8747 598
af36000c 599=head3 Class Names as Strings
748a9306 600
af36000c
DR
601Perl also lets you use a scalar containing a string as a class name:
602
603 my $class = 'File';
604
605 my $file = $class->new( $path, $data );
606
607Again, this allows for very dynamic code.
608
609=head3 Subroutine References as Methods
610
7168b251 611You can also use a subroutine reference as a method:
af36000c
DR
612
613 my $sub = sub {
614 my $self = shift;
615
616 $self->save();
617 };
618
619 $file->$sub();
620
621This is exactly equivalent to writing C<< $sub->($file) >>. You may see
622this idiom in the wild combined with a call to C<can>:
623
624 if ( my $meth = $object->can('foo') ) {
625 $object->$meth();
626 }
627
8d26c1a8
DR
628=head3 Deferencing Method Call
629
630Perl also lets you use a dereferenced scalar reference in a method
631call. That's a mouthful, so let's look at some code:
632
633 $file->${ \'save' };
634 $file->${ returns_scalar_ref() };
635 $file->${ \( returns_scalar() ) };
636
637This works if the dereference produces a string I<or> a subroutine
638reference.
639
640=head3 Method Calls on Filehandles
641
642Under the hood, Perl filehandles are instances of the C<IO::Handle> or
643C<IO::File> class. Once you have an open filehandle, you can call
7168b251 644methods on it. Additionally, you can call methods on the C<STDIN>,
8d26c1a8
DR
645C<STDOUT>, and C<STDERR> filehandles.
646
647 open my $fh, '>', 'path/to/file';
648 $fh->autoflush();
649 $fh->print('content');
650
651 STDOUT->autoflush();
652
af36000c
DR
653=head2 Invoking Class Methods
654X<invocation>
655
656Because Perl allows you to use barewords for package names and
657subroutine names, it can sometimes guess wrong about what you intend a
7168b251
DC
658bareword to be. For example, the construct C<< Class->new() >> can be
659interpreted as either C<< 'Class'->new() >> or C<< Class()->new() >>.
660In English, that second interpretation reads as "call a subroutine
661named Class(), then call new() as a method on the return value". If
662there is a subroutine named C<Class()> in the current namespace, Perl
663will always interpret C<Class->new()> as the second alterative: a call
664to C<new()> on the object returned by a call to C<Class()>
665
666You can force Perl to use the first interpretation (i.e. as a method
667call on the class named "Class") in two ways. First, you can append a
668C<::> to the class name:
af36000c
DR
669
670 Class::->new()
671
7168b251
DC
672Perl will always interpret this as a method call.
673
674Alternatively, you can quote the class name:
af36000c
DR
675
676 'Class'->new()
677
678Of course, if the class name is in a scalar Perl will do the right
679thing as well:
680
681 my $class = 'Class';
682 $class->new();
683
684=head3 Indirect Object Syntax
685X<indirect object>
686
687B<Outside of the file handle case, use of this syntax is discouraged,
688as it can confuse the Perl interpreter. See below for more details.>
689
690Perl suports another method invocation syntax called "indirect object"
8d26c1a8
DR
691notation. This syntax is called "indirect" because the method comes
692before the object it is being invoked on.
af36000c
DR
693
694This syntax can be used with any class or object method:
695
696 my $file = new File $path, $data;
697 save $file;
698
699We recommend that you avoid this syntax, for several reasons.
700
701First, it can be confusing to read. In the above example, it's not
702clear if C<save> is a method or simply a subroutine that expects a file
703object as its first argument.
704
705When used with class methods, the problem is even worse. Because Perl
706allows subroutine names to be written as barewords, Perl has to guess
707whether the bareword after the method is a class name or subroutine
708name. In other words, Perl can resolve the syntax as either C<<
709File->new( $path, $data ) >> B<or> C<< new( File( $path, $data ) ) >>.
710
711To parse this code, Perl uses a heuristic based on what package names
8d26c1a8
DR
712it has seen, what subroutines exist in the current package, what
713barewords it has previously seen, and other input. Needless to say,
714heuristics can produce very surprising results!
af36000c
DR
715
716Older documentation (and some CPAN modules) encouraged this syntax,
717particularly for constructors, so you may still find it in the wild.
718However, we encourage you to avoid using it in new code.
719
8d26c1a8
DR
720You can force Perl to interpret the bareword as a class name by
721appending "::" to it, like we saw earlier:
722
723 my $file = new File:: $path, $data;
724
af36000c
DR
725=head2 C<bless>, C<blessed>, and C<ref>
726
7168b251
DC
727As we saw earlier, an object is simply a data structure that has been
728blessed into a class via the C<bless> function. The C<bless> function
8d26c1a8 729can take either one or two arguments:
af36000c
DR
730
731 my $object = bless {}, $class;
732 my $object = bless {};
733
7168b251
DC
734In the first form, the anonymous hash is being blessed into the class
735in C<$class>. In the second form, the anonymous hash is blessed into
736the current package.
af36000c 737
7168b251
DC
738The second form is strongly discouraged, because it breaks the ability
739of a subclass to reuse the parent's constructor, but you may still run
af36000c
DR
740across it in existing code.
741
8d26c1a8
DR
742If you want to know whether a particular scalar refers to an object,
743you can use the C<blessed> function exported by L<Scalar::Util>, which
744is shipped with the Perl core.
af36000c
DR
745
746 use Scalar::Util 'blessed';
747
748 if ( blessed($thing) ) { ... }
749
7168b251 750If C<$thing> refers to an object, then this function returns the name
af36000c
DR
751of the package the object has been blessed into. Note that the example
752above will return false if C<$thing> has been blessed into a class
7168b251
DC
753named "0". If C<$thing> doesn't contain a reference to a blessed
754object, the C<blessed> function returns false (specifically: C<undef>).
af36000c 755
7168b251
DC
756Similarly, Perl's built-in C<ref> function treats a reference to a
757blessed object specially. If you call C<ref($thing)> and C<$thing>
758holds a reference to an object, it will return the name of the class
759that the object has been blessed into.
af36000c 760
7168b251
DC
761If you simply want to check that a variable contains an object
762reference, we recommend that you use C<defined blessed($object)>, since
763C<ref> returns true values for all references, not just objects.
af36000c
DR
764
765=head2 The UNIVERSAL Class
d74e8afc 766X<UNIVERSAL>
a2bdc9a5 767
af36000c 768All classes automatically inherit from the L<UNIVERSAL> class, which is
8d26c1a8 769built-in to the Perl core. This class provides a number of methods, all
af36000c
DR
770of which can be called on either a class or an object. You can also
771choose to override some of these methods in your class. If you do so,
8d26c1a8 772we recommend that you follow the built-in semantics described below.
a2bdc9a5
PP
773
774=over 4
775
af36000c 776=item isa($class)
d74e8afc 777X<isa>
a2bdc9a5 778
af36000c
DR
779The C<isa> method returns I<true> if the object is a member of the
780class in C<$class>, or a member of a subclass of C<$class>.
a2bdc9a5 781
af36000c 782=item DOES($role)
003db2bd 783X<DOES>
bcb8f0e8 784
af36000c
DR
785The C<DOES> method returns I<true> if its object claims to perform the
786role C<$role>. By default, this is equivalent to C<isa>. This method is
787provided for use by object system extensions that implement roles, like
788C<Moose> and C<Role::Tiny>.
bcb8f0e8 789
8d26c1a8
DR
790You can also override C<DOES> directly in your own classes.
791
af36000c 792=item can($method)
d74e8afc 793X<can>
a2bdc9a5 794
8d26c1a8
DR
795The C<can> method checks to see if the class or object it was called on
796has a method named C<$method>. This checks for the method in the class
797and all of its parents. If the method exists, then a reference to the
798subroutine is returned. If it does not then C<undef> is returned.
b32b0a5d 799
af36000c 800If your class responds to method calls via C<AUTOLOAD>, you may want to
8d26c1a8
DR
801overload C<can> to return a subroutine reference for methods which your
802C<AUTOLOAD> method handles.
af36000c
DR
803
804=item VERSION($need)
d74e8afc 805X<VERSION>
760ac839 806
af36000c
DR
807The C<VERSION> method returns the version number of the class
808(package).
809
810If the C<$need> argument is given then it will check that the current
811version (as defined by the $VERSION variable in the package) is greater
812than or equal to C<$need>; it will die if this is not the case. This
813method is called automatically by the C<VERSION> form of C<use>.
a2bdc9a5 814
003db2bd 815 use Package 1.2 qw(some imported subs);
71be2cbc 816 # implies:
003db2bd 817 Package->VERSION(1.2);
a2bdc9a5 818
af36000c
DR
819We recommend that you use this method to access another package's
820version, rather than looking directly at C<$Package::VERSION>. The
821package you are looking at could have overridden the C<VERSION> method.
822
823We also recommend using this method to check whether a module has a
824sufficient version. The internal implementation uses the L<version>
825module to make sure that different types of version numbers are
826compared correctly.
827
a2bdc9a5
PP
828=back
829
af36000c
DR
830=head2 AUTOLOAD
831X<AUTOLOAD>
832
833If you call a method that doesn't exist in a class, Perl will throw an
834error. However, if that class or any of its parent classes defined an
835C<AUTOLOAD> method, that method will be called instead.
836
837This is called as a regular method, and the caller will not know the
838difference. Whatever value your C<AUTOLOAD> method returns is given to
839the caller.
840
841The fully qualified method name that was called is available in the
842C<$AUTOLOAD> package global for your class. Since this is a global, if
995ab4ef
DR
843you want to refer to do it without a package name prefix under C<strict
844'vars'>, you need to declare it.
af36000c 845
00c07ea6
DR
846 # XXX - this is a terrible way to implement accessors, but it makes
847 # for a simple example.
af36000c
DR
848 our $AUTOLOAD;
849 sub AUTOLOAD {
850 my $self = shift;
851
7168b251
DC
852 # Remove qualifier from original method name...
853 my $called = $AUTOLOAD =~ s/.*:://r;
af36000c 854
7168b251 855 # Is there an attribute of that name?
af36000c
DR
856 die "No such attribute: $called"
857 unless exists $self->{$called};
858
7168b251 859 # If so, return it...
af36000c
DR
860 return $self->{$called};
861 }
862
8d26c1a8
DR
863 sub DESTROY { } # see below
864
af36000c
DR
865Without the C<our $AUTOLOAD> declaration, this code will not compile
866under the L<strict> pragma.
867
868As the comment says, this is not a good way to implement accessors.
869It's slow and too clever by far. See L<perlootut> for recommendations
870on OO coding in Perl.
871
872If your class does have an C<AUTOLOAD> method, we strongly recommend
873that you override C<can> in your class as well. Your overridden C<can>
874method should return a subroutine reference for any method that your
875C<AUTOLOAD> responds to.
876
54310121 877=head2 Destructors
d74e8afc 878X<destructor> X<DESTROY>
a0d0e21e 879
8d26c1a8
DR
880When the last reference to an object goes away, the object is
881destroyed. If you only have one reference to an object stored in a
882lexical scalar, the object is destroyed when that scalar goes out of
883scope. If you store the object in a package global, that object may not
884go out of scope until the program exits.
af36000c
DR
885
886If you want to do something when the object is destroyed, you can
8d26c1a8
DR
887define a C<DESTROY> method in your class. This method will always be
888called by Perl at the appropriate time, unless the method is empty.
af36000c
DR
889
890This is called just like any other method, with the object as the first
8d26c1a8
DR
891argument. It does not receive any additional arguments. However, the
892C<$_[0]> variable will be read-only in the destructor, so you cannot
893assign a value to it.
894
895If your C<DESTROY> method throws an error, this error will be ignored.
896It will not be sent to C<STDERR> and it will not cause the program to
897die. However, if your destructor is running inside an C<eval {}> block,
898then the error will change the value of C<$@>.
af36000c
DR
899
900Because C<DESTROY> methods can be called at any time, you should
901localize any global variables you might update in your C<DESTROY>. In
902particular, if you use C<eval {}> you should localize C<$@>, and if you
903use C<system> or backticks, you should localize C<$?>.
904
8d26c1a8
DR
905If you define an C<AUTOLOAD> in your class, then Perl will call your
906C<AUTOLOAD> to handle the C<DESTROY> method. You can prevent this by
7168b251
DC
907defining an empty C<DESTROY>, like we did in the autoloading example.
908You can also check the value of C<$AUTOLOAD> and return without doing
909anything when called to handle C<DESTROY>.
8d26c1a8
DR
910
911=head3 Global Destruction
af36000c 912
8d26c1a8
DR
913The order in which objects are destroyed during the global destruction
914before the program exits is unpredictable. This means that any objects
915contained by your object may already have been destroyed. You should
916check that a contained object is defined before calling a method on it:
af36000c
DR
917
918 sub DESTROY {
919 my $self = shift;
920
921 $self->{handle}->close() if $self->{handle};
922 }
923
924You can use the C<${^GLOBAL_PHASE}> variable to detect if you are
8d26c1a8 925currently in the global destruction phase:
af36000c
DR
926
927 sub DESTROY {
928 my $self = shift;
929
930 return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
931
932 $self->{handle}->close();
933 }
934
935Note that this variable was added in Perl 5.14.0. If you want to detect
8d26c1a8 936the global destruction phase on older versions of Perl, you can use the
af36000c
DR
937C<Devel::GlobalDestruction> module on CPAN.
938
8d26c1a8
DR
939If your C<DESTROY> method issues a warning during global destruction,
940the Perl interpreter will append the string " during global
941destruction" the warning.
942
943During global destruction, Perl will always garbage collect objects
995ab4ef
DR
944before unblessed references. See L<perlhacktips/PERL_DESTRUCT_LEVEL>
945for more information about global destruction.
8d26c1a8 946
7168b251 947=head2 Non-Hash Objects
af36000c 948
7168b251
DC
949All the examples so far have shown objects based on a blessed hash.
950However, it's possible to bless any type of data structure or referent,
951including scalars, globs, and subroutines. You may see this sort of
952thing when looking at code in the wild.
af36000c
DR
953
954Here's an example of a module as a blessed scalar:
955
956 package Time;
957
e011bc34
DR
958 use strict;
959 use warnings;
960
af36000c
DR
961 sub new {
962 my $class = shift;
963
964 my $time = time;
965 return bless \$time, $class;
966 }
967
968 sub epoch {
969 my $self = shift;
970 return ${ $self };
971 }
972
973 my $time = Time->new();
974 print $time->epoch();
975
976=head2 Inside-Out objects
977
e011bc34 978In the past, the Perl community experimented with a technique called
8d26c1a8
DR
979"inside-out objects". An inside-out object stores its data outside of
980the object's reference, indexed on a unique property of the object,
7168b251
DC
981such as its memory address, rather than in the object itself. This has
982the advantage of enforcing the encapsulation of object attributes,
983since their data is not stored in the object itself.
af36000c 984
e011bc34 985This technique was popular for a while (and was recommended in Damian
7168b251
DC
986Conway's I<Perl Best Practices>), but never achieved universal
987adoption. The L<Object::InsideOut> module on CPAN provides a
988comprehensive implementation of this technique, and you may see it or
989other inside-out modules in the wild.
af36000c 990
e011bc34
DR
991Here is a simple example of the technique, using the
992L<Hash::Util::FieldHash> core module. This module was added to the core
993to support inside-out object implementations.
994
7168b251 995 package Time;
e011bc34
DR
996
997 use strict;
998 use warnings;
999
1000 use Hash::Util::FieldHash 'fieldhash';
1001
7168b251 1002 fieldhash my %time_for;
e011bc34
DR
1003
1004 sub new {
1005 my $class = shift;
e011bc34 1006
7168b251
DC
1007 my $self = bless \( my $object ), $class;
1008
1009 $time_for{$self} = time;
1010
1011 return $self;
e011bc34
DR
1012 }
1013
1014 sub epoch {
1015 my $self = shift;
1016
7168b251 1017 return $time_for{$self};
e011bc34
DR
1018 }
1019
7168b251 1020 my $time = Time->new;
e011bc34
DR
1021 print $time->epoch;
1022
af36000c
DR
1023=head2 Pseudo-hashes
1024
1025The pseudo-hash feature was an experimental feature introduced in
1026earlier versions of Perl and removed in 5.10.0. A pseudo-hash is an
1027array reference which can be accessed using named keys like a hash. You
1028may run in to some code in the wild which uses it. See the L<fields>
1029pragma for more information.
5a964f20 1030
a0d0e21e
LW
1031=head1 SEE ALSO
1032
8257a158 1033A kinder, gentler tutorial on object-oriented programming in Perl can
af36000c
DR
1034be found in L<perlootut>. You should also check out L<perlmodlib> for
1035some style guides on constructing both modules and classes.
1036