1N/A=head1 NAME
1N/A
1N/Aperlobj - Perl objects
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AFirst you need to understand what references are in Perl.
1N/ASee L<perlref> for that. Second, if you still find the following
1N/Areference work too complicated, a tutorial on object-oriented programming
1N/Ain Perl can be found in L<perltoot> and L<perltooc>.
1N/A
1N/AIf you're still with us, then
1N/Ahere are three very simple definitions that you should find reassuring.
1N/A
1N/A=over 4
1N/A
1N/A=item 1.
1N/A
1N/AAn object is simply a reference that happens to know which class it
1N/Abelongs to.
1N/A
1N/A=item 2.
1N/A
1N/AA class is simply a package that happens to provide methods to deal
1N/Awith object references.
1N/A
1N/A=item 3.
1N/A
1N/AA method is simply a subroutine that expects an object reference (or
1N/Aa package name, for class methods) as the first argument.
1N/A
1N/A=back
1N/A
1N/AWe'll cover these points now in more depth.
1N/A
1N/A=head2 An Object is Simply a Reference
1N/A
1N/AUnlike say C++, Perl doesn't provide any special syntax for
1N/Aconstructors. A constructor is merely a subroutine that returns a
1N/Areference to something "blessed" into a class, generally the
1N/Aclass that the subroutine is defined in. Here is a typical
1N/Aconstructor:
1N/A
1N/A package Critter;
1N/A sub new { bless {} }
1N/A
1N/AThat word C<new> isn't special. You could have written
1N/Aa construct this way, too:
1N/A
1N/A package Critter;
1N/A sub spawn { bless {} }
1N/A
1N/AThis might even be preferable, because the C++ programmers won't
1N/Abe tricked into thinking that C<new> works in Perl as it does in C++.
1N/AIt doesn't. We recommend that you name your constructors whatever
1N/Amakes sense in the context of the problem you're solving. For example,
1N/Aconstructors in the Tk extension to Perl are named after the widgets
1N/Athey create.
1N/A
1N/AOne thing that's different about Perl constructors compared with those in
1N/AC++ is that in Perl, they have to allocate their own memory. (The other
1N/Athings is that they don't automatically call overridden base-class
1N/Aconstructors.) The C<{}> allocates an anonymous hash containing no
1N/Akey/value pairs, and returns it The bless() takes that reference and
1N/Atells the object it references that it's now a Critter, and returns
1N/Athe reference. This is for convenience, because the referenced object
1N/Aitself knows that it has been blessed, and the reference to it could
1N/Ahave been returned directly, like this:
1N/A
1N/A sub new {
1N/A my $self = {};
1N/A bless $self;
1N/A return $self;
1N/A }
1N/A
1N/AYou often see such a thing in more complicated constructors
1N/Athat wish to call methods in the class as part of the construction:
1N/A
1N/A sub new {
1N/A my $self = {};
1N/A bless $self;
1N/A $self->initialize();
1N/A return $self;
1N/A }
1N/A
1N/AIf you care about inheritance (and you should; see
1N/AL<perlmodlib/"Modules: Creation, Use, and Abuse">),
1N/Athen you want to use the two-arg form of bless
1N/Aso that your constructors may be inherited:
1N/A
1N/A sub new {
1N/A my $class = shift;
1N/A my $self = {};
1N/A bless $self, $class;
1N/A $self->initialize();
1N/A return $self;
1N/A }
1N/A
1N/AOr if you expect people to call not just C<< CLASS->new() >> but also
1N/AC<< $obj->new() >>, then use something like the following. (Note that using
1N/Athis to call new() on an instance does not automatically perform any
1N/Acopying. If you want a shallow or deep copy of an object, you'll have to
1N/Aspecifically allow for that.) The initialize() method used will be of
1N/Awhatever $class we blessed the object into:
1N/A
1N/A sub new {
1N/A my $this = shift;
1N/A my $class = ref($this) || $this;
1N/A my $self = {};
1N/A bless $self, $class;
1N/A $self->initialize();
1N/A return $self;
1N/A }
1N/A
1N/AWithin the class package, the methods will typically deal with the
1N/Areference as an ordinary reference. Outside the class package,
1N/Athe reference is generally treated as an opaque value that may
1N/Abe accessed only through the class's methods.
1N/A
1N/AAlthough a constructor can in theory re-bless a referenced object
1N/Acurrently belonging to another class, this is almost certainly going
1N/Ato get you into trouble. The new class is responsible for all
1N/Acleanup later. The previous blessing is forgotten, as an object
1N/Amay belong to only one class at a time. (Although of course it's
1N/Afree to inherit methods from many classes.) If you find yourself
1N/Ahaving to do this, the parent class is probably misbehaving, though.
1N/A
1N/AA clarification: Perl objects are blessed. References are not. Objects
1N/Aknow which package they belong to. References do not. The bless()
1N/Afunction uses the reference to find the object. Consider
1N/Athe following example:
1N/A
1N/A $a = {};
1N/A $b = $a;
1N/A bless $a, BLAH;
1N/A print "\$b is a ", ref($b), "\n";
1N/A
1N/AThis reports $b as being a BLAH, so obviously bless()
1N/Aoperated on the object and not on the reference.
1N/A
1N/A=head2 A Class is Simply a Package
1N/A
1N/AUnlike say C++, Perl doesn't provide any special syntax for class
1N/Adefinitions. You use a package as a class by putting method
1N/Adefinitions into the class.
1N/A
1N/AThere is a special array within each package called @ISA, which says
1N/Awhere else to look for a method if you can't find it in the current
1N/Apackage. This is how Perl implements inheritance. Each element of the
1N/A@ISA array is just the name of another package that happens to be a
1N/Aclass package. The classes are searched (depth first) for missing
1N/Amethods in the order that they occur in @ISA. The classes accessible
1N/Athrough @ISA are known as base classes of the current class.
1N/A
1N/AAll classes implicitly inherit from class C<UNIVERSAL> as their
1N/Alast base class. Several commonly used methods are automatically
1N/Asupplied in the UNIVERSAL class; see L<"Default UNIVERSAL methods"> for
1N/Amore details.
1N/A
1N/AIf a missing method is found in a base class, it is cached
1N/Ain the current class for efficiency. Changing @ISA or defining new
1N/Asubroutines invalidates the cache and causes Perl to do the lookup again.
1N/A
1N/AIf neither the current class, its named base classes, nor the UNIVERSAL
1N/Aclass contains the requested method, these three places are searched
1N/Aall over again, this time looking for a method named AUTOLOAD(). If an
1N/AAUTOLOAD is found, this method is called on behalf of the missing method,
1N/Asetting the package global $AUTOLOAD to be the fully qualified name of
1N/Athe method that was intended to be called.
1N/A
1N/AIf none of that works, Perl finally gives up and complains.
1N/A
1N/AIf you want to stop the AUTOLOAD inheritance say simply
1N/A
1N/A sub AUTOLOAD;
1N/A
1N/Aand the call will die using the name of the sub being called.
1N/A
1N/APerl classes do method inheritance only. Data inheritance is left up
1N/Ato the class itself. By and large, this is not a problem in Perl,
1N/Abecause most classes model the attributes of their object using an
1N/Aanonymous hash, which serves as its own little namespace to be carved up
1N/Aby the various classes that might want to do something with the object.
1N/AThe only problem with this is that you can't sure that you aren't using
1N/Aa piece of the hash that isn't already used. A reasonable workaround
1N/Ais to prepend your fieldname in the hash with the package name.
1N/A
1N/A sub bump {
1N/A my $self = shift;
1N/A $self->{ __PACKAGE__ . ".count"}++;
1N/A }
1N/A
1N/A=head2 A Method is Simply a Subroutine
1N/A
1N/AUnlike say C++, Perl doesn't provide any special syntax for method
1N/Adefinition. (It does provide a little syntax for method invocation
1N/Athough. More on that later.) A method expects its first argument
1N/Ato be the object (reference) or package (string) it is being invoked
1N/Aon. There are two ways of calling methods, which we'll call class
1N/Amethods and instance methods.
1N/A
1N/AA class method expects a class name as the first argument. It
1N/Aprovides functionality for the class as a whole, not for any
1N/Aindividual object belonging to the class. Constructors are often
1N/Aclass methods, but see L<perltoot> and L<perltooc> for alternatives.
1N/AMany class methods simply ignore their first argument, because they
1N/Aalready know what package they're in and don't care what package
1N/Athey were invoked via. (These aren't necessarily the same, because
1N/Aclass methods follow the inheritance tree just like ordinary instance
1N/Amethods.) Another typical use for class methods is to look up an
1N/Aobject by name:
1N/A
1N/A sub find {
1N/A my ($class, $name) = @_;
1N/A $objtable{$name};
1N/A }
1N/A
1N/AAn instance method expects an object reference as its first argument.
1N/ATypically it shifts the first argument into a "self" or "this" variable,
1N/Aand then uses that as an ordinary reference.
1N/A
1N/A sub display {
1N/A my $self = shift;
1N/A my @keys = @_ ? @_ : sort keys %$self;
1N/A foreach $key (@keys) {
1N/A print "\t$key => $self->{$key}\n";
1N/A }
1N/A }
1N/A
1N/A=head2 Method Invocation
1N/A
1N/AFor various historical and other reasons, Perl offers two equivalent
1N/Aways to write a method call. The simpler and more common way is to use
1N/Athe arrow notation:
1N/A
1N/A my $fred = Critter->find("Fred");
1N/A $fred->display("Height", "Weight");
1N/A
1N/AYou should already be familiar with the use of the C<< -> >> operator with
1N/Areferences. In fact, since C<$fred> above is a reference to an object,
1N/Ayou could think of the method call as just another form of
1N/Adereferencing.
1N/A
1N/AWhatever is on the left side of the arrow, whether a reference or a
1N/Aclass name, is passed to the method subroutine as its first argument.
1N/ASo the above code is mostly equivalent to:
1N/A
1N/A my $fred = Critter::find("Critter", "Fred");
1N/A Critter::display($fred, "Height", "Weight");
1N/A
1N/AHow does Perl know which package the subroutine is in? By looking at
1N/Athe left side of the arrow, which must be either a package name or a
1N/Areference to an object, i.e. something that has been blessed to a
1N/Apackage. Either way, that's the package where Perl starts looking. If
1N/Athat package has no subroutine with that name, Perl starts looking for
1N/Ait in any base classes of that package, and so on.
1N/A
1N/AIf you need to, you I<can> force Perl to start looking in some other package:
1N/A
1N/A my $barney = MyCritter->Critter::find("Barney");
1N/A $barney->Critter::display("Height", "Weight");
1N/A
1N/AHere C<MyCritter> is presumably a subclass of C<Critter> that defines
1N/Aits own versions of find() and display(). We haven't specified what
1N/Athose methods do, but that doesn't matter above since we've forced Perl
1N/Ato start looking for the subroutines in C<Critter>.
1N/A
1N/AAs a special case of the above, you may use the C<SUPER> pseudo-class to
1N/Atell Perl to start looking for the method in the packages named in the
1N/Acurrent class's C<@ISA> list.
1N/A
1N/A package MyCritter;
1N/A use base 'Critter'; # sets @MyCritter::ISA = ('Critter');
1N/A
1N/A sub display {
1N/A my ($self, @args) = @_;
1N/A $self->SUPER::display("Name", @args);
1N/A }
1N/A
1N/AIt is important to note that C<SUPER> refers to the superclass(es) of the
1N/AI<current package> and not to the superclass(es) of the object. Also, the
1N/AC<SUPER> pseudo-class can only currently be used as a modifier to a method
1N/Aname, but not in any of the other ways that class names are normally used,
1N/Aeg:
1N/A
1N/A something->SUPER::method(...); # OK
1N/A SUPER::method(...); # WRONG
1N/A SUPER->method(...); # WRONG
1N/A
1N/AInstead of a class name or an object reference, you can also use any
1N/Aexpression that returns either of those on the left side of the arrow.
1N/ASo the following statement is valid:
1N/A
1N/A Critter->find("Fred")->display("Height", "Weight");
1N/A
1N/Aand so is the following:
1N/A
1N/A my $fred = (reverse "rettirC")->find(reverse "derF");
1N/A
1N/A=head2 Indirect Object Syntax
1N/A
1N/AThe other way to invoke a method is by using the so-called "indirect
1N/Aobject" notation. This syntax was available in Perl 4 long before
1N/Aobjects were introduced, and is still used with filehandles like this:
1N/A
1N/A print STDERR "help!!!\n";
1N/A
1N/AThe same syntax can be used to call either object or class methods.
1N/A
1N/A my $fred = find Critter "Fred";
1N/A display $fred "Height", "Weight";
1N/A
1N/ANotice that there is no comma between the object or class name and the
1N/Aparameters. This is how Perl can tell you want an indirect method call
1N/Ainstead of an ordinary subroutine call.
1N/A
1N/ABut what if there are no arguments? In that case, Perl must guess what
1N/Ayou want. Even worse, it must make that guess I<at compile time>.
1N/AUsually Perl gets it right, but when it doesn't you get a function
1N/Acall compiled as a method, or vice versa. This can introduce subtle bugs
1N/Athat are hard to detect.
1N/A
1N/AFor example, a call to a method C<new> in indirect notation -- as C++
1N/Aprogrammers are wont to make -- can be miscompiled into a subroutine
1N/Acall if there's already a C<new> function in scope. You'd end up
1N/Acalling the current package's C<new> as a subroutine, rather than the
1N/Adesired class's method. The compiler tries to cheat by remembering
1N/Abareword C<require>s, but the grief when it messes up just isn't worth the
1N/Ayears of debugging it will take you to track down such subtle bugs.
1N/A
1N/AThere is another problem with this syntax: the indirect object is
1N/Alimited to a name, a scalar variable, or a block, because it would have
1N/Ato do too much lookahead otherwise, just like any other postfix
1N/Adereference in the language. (These are the same quirky rules as are
1N/Aused for the filehandle slot in functions like C<print> and C<printf>.)
1N/AThis can lead to horribly confusing precedence problems, as in these
1N/Anext two lines:
1N/A
1N/A move $obj->{FIELD}; # probably wrong!
1N/A move $ary[$i]; # probably wrong!
1N/A
1N/AThose actually parse as the very surprising:
1N/A
1N/A $obj->move->{FIELD}; # Well, lookee here
1N/A $ary->move([$i]); # Didn't expect this one, eh?
1N/A
1N/ARather than what you might have expected:
1N/A
1N/A $obj->{FIELD}->move(); # You should be so lucky.
1N/A $ary[$i]->move; # Yeah, sure.
1N/A
1N/ATo get the correct behavior with indirect object syntax, you would have
1N/Ato use a block around the indirect object:
1N/A
1N/A move {$obj->{FIELD}};
1N/A move {$ary[$i]};
1N/A
1N/AEven then, you still have the same potential problem if there happens to
1N/Abe a function named C<move> in the current package. B<The C<< -> >>
1N/Anotation suffers from neither of these disturbing ambiguities, so we
1N/Arecommend you use it exclusively.> However, you may still end up having
1N/Ato read code using the indirect object notation, so it's important to be
1N/Afamiliar with it.
1N/A
1N/A=head2 Default UNIVERSAL methods
1N/A
1N/AThe C<UNIVERSAL> package automatically contains the following methods that
1N/Aare inherited by all other classes:
1N/A
1N/A=over 4
1N/A
1N/A=item isa(CLASS)
1N/A
1N/AC<isa> returns I<true> if its object is blessed into a subclass of C<CLASS>
1N/A
1N/AYou can also call C<UNIVERSAL::isa> as a subroutine with two arguments.
1N/AThe first does not need to be an object or even a reference. This
1N/Aallows you to check what a reference points to, or whether
1N/Asomething is a reference of a given type. Example
1N/A
1N/A if(UNIVERSAL::isa($ref, 'ARRAY')) {
1N/A #...
1N/A }
1N/A
1N/ATo determine if a reference is a blessed object, you can write
1N/A
1N/A print "It's an object\n" if UNIVERSAL::isa($val, 'UNIVERSAL');
1N/A
1N/A=item can(METHOD)
1N/A
1N/AC<can> checks to see if its object has a method called C<METHOD>,
1N/Aif it does then a reference to the sub is returned, if it does not then
1N/AI<undef> is returned.
1N/A
1N/AC<UNIVERSAL::can> can also be called as a subroutine with two arguments.
1N/AIt'll always return I<undef> if its first argument isn't an object or a
1N/Aclass name. So here's another way to check if a reference is a
1N/Ablessed object
1N/A
1N/A print "It's still an object\n" if UNIVERSAL::can($val, 'can');
1N/A
1N/AYou can also use the C<blessed> function of Scalar::Util:
1N/A
1N/A use Scalar::Util 'blessed';
1N/A
1N/A my $blessing = blessed $suspected_object;
1N/A
1N/AC<blessed> returns the name of the package the argument has been
1N/Ablessed into, or C<undef>.
1N/A
1N/A=item VERSION( [NEED] )
1N/A
1N/AC<VERSION> returns the version number of the class (package). If the
1N/ANEED argument is given then it will check that the current version (as
1N/Adefined by the $VERSION variable in the given package) not less than
1N/ANEED; it will die if this is not the case. This method is normally
1N/Acalled as a class method. This method is called automatically by the
1N/AC<VERSION> form of C<use>.
1N/A
1N/A use A 1.2 qw(some imported subs);
1N/A # implies:
1N/A A->VERSION(1.2);
1N/A
1N/A=back
1N/A
1N/AB<NOTE:> C<can> directly uses Perl's internal code for method lookup, and
1N/AC<isa> uses a very similar method and cache-ing strategy. This may cause
1N/Astrange effects if the Perl code dynamically changes @ISA in any package.
1N/A
1N/AYou may add other methods to the UNIVERSAL class via Perl or XS code.
1N/AYou do not need to C<use UNIVERSAL> to make these methods
1N/Aavailable to your program (and you should not do so).
1N/A
1N/A=head2 Destructors
1N/A
1N/AWhen the last reference to an object goes away, the object is
1N/Aautomatically destroyed. (This may even be after you exit, if you've
1N/Astored references in global variables.) If you want to capture control
1N/Ajust before the object is freed, you may define a DESTROY method in
1N/Ayour class. It will automatically be called at the appropriate moment,
1N/Aand you can do any extra cleanup you need to do. Perl passes a reference
1N/Ato the object under destruction as the first (and only) argument. Beware
1N/Athat the reference is a read-only value, and cannot be modified by
1N/Amanipulating C<$_[0]> within the destructor. The object itself (i.e.
1N/Athe thingy the reference points to, namely C<${$_[0]}>, C<@{$_[0]}>,
1N/AC<%{$_[0]}> etc.) is not similarly constrained.
1N/A
1N/AIf you arrange to re-bless the reference before the destructor returns,
1N/Aperl will again call the DESTROY method for the re-blessed object after
1N/Athe current one returns. This can be used for clean delegation of
1N/Aobject destruction, or for ensuring that destructors in the base classes
1N/Aof your choosing get called. Explicitly calling DESTROY is also possible,
1N/Abut is usually never needed.
1N/A
1N/ADo not confuse the previous discussion with how objects I<CONTAINED> in the current
1N/Aone are destroyed. Such objects will be freed and destroyed automatically
1N/Awhen the current object is freed, provided no other references to them exist
1N/Aelsewhere.
1N/A
1N/A=head2 Summary
1N/A
1N/AThat's about all there is to it. Now you need just to go off and buy a
1N/Abook about object-oriented design methodology, and bang your forehead
1N/Awith it for the next six months or so.
1N/A
1N/A=head2 Two-Phased Garbage Collection
1N/A
1N/AFor most purposes, Perl uses a fast and simple, reference-based
1N/Agarbage collection system. That means there's an extra
1N/Adereference going on at some level, so if you haven't built
1N/Ayour Perl executable using your C compiler's C<-O> flag, performance
1N/Awill suffer. If you I<have> built Perl with C<cc -O>, then this
1N/Aprobably won't matter.
1N/A
1N/AA more serious concern is that unreachable memory with a non-zero
1N/Areference count will not normally get freed. Therefore, this is a bad
1N/Aidea:
1N/A
1N/A {
1N/A my $a;
1N/A $a = \$a;
1N/A }
1N/A
1N/AEven thought $a I<should> go away, it can't. When building recursive data
1N/Astructures, you'll have to break the self-reference yourself explicitly
1N/Aif you don't care to leak. For example, here's a self-referential
1N/Anode such as one might use in a sophisticated tree structure:
1N/A
1N/A sub new_node {
1N/A my $class = shift;
1N/A my $node = {};
1N/A $node->{LEFT} = $node->{RIGHT} = $node;
1N/A $node->{DATA} = [ @_ ];
1N/A return bless $node => $class;
1N/A }
1N/A
1N/AIf you create nodes like that, they (currently) won't go away unless you
1N/Abreak their self reference yourself. (In other words, this is not to be
1N/Aconstrued as a feature, and you shouldn't depend on it.)
1N/A
1N/AAlmost.
1N/A
1N/AWhen an interpreter thread finally shuts down (usually when your program
1N/Aexits), then a rather costly but complete mark-and-sweep style of garbage
1N/Acollection is performed, and everything allocated by that thread gets
1N/Adestroyed. This is essential to support Perl as an embedded or a
1N/Amultithreadable language. For example, this program demonstrates Perl's
1N/Atwo-phased garbage collection:
1N/A
1N/A #!/usr/bin/perl
1N/A package Subtle;
1N/A
1N/A sub new {
1N/A my $test;
1N/A $test = \$test;
1N/A warn "CREATING " . \$test;
1N/A return bless \$test;
1N/A }
1N/A
1N/A sub DESTROY {
1N/A my $self = shift;
1N/A warn "DESTROYING $self";
1N/A }
1N/A
1N/A package main;
1N/A
1N/A warn "starting program";
1N/A {
1N/A my $a = Subtle->new;
1N/A my $b = Subtle->new;
1N/A $$a = 0; # break selfref
1N/A warn "leaving block";
1N/A }
1N/A
1N/A warn "just exited block";
1N/A warn "time to die...";
1N/A exit;
1N/A
1N/AWhen run as F</foo/test>, the following output is produced:
1N/A
1N/A starting program at /foo/test line 18.
1N/A CREATING SCALAR(0x8e5b8) at /foo/test line 7.
1N/A CREATING SCALAR(0x8e57c) at /foo/test line 7.
1N/A leaving block at /foo/test line 23.
1N/A DESTROYING Subtle=SCALAR(0x8e5b8) at /foo/test line 13.
1N/A just exited block at /foo/test line 26.
1N/A time to die... at /foo/test line 27.
1N/A DESTROYING Subtle=SCALAR(0x8e57c) during global destruction.
1N/A
1N/ANotice that "global destruction" bit there? That's the thread
1N/Agarbage collector reaching the unreachable.
1N/A
1N/AObjects are always destructed, even when regular refs aren't. Objects
1N/Aare destructed in a separate pass before ordinary refs just to
1N/Aprevent object destructors from using refs that have been themselves
1N/Adestructed. Plain refs are only garbage-collected if the destruct level
1N/Ais greater than 0. You can test the higher levels of global destruction
1N/Aby setting the PERL_DESTRUCT_LEVEL environment variable, presuming
1N/AC<-DDEBUGGING> was enabled during perl build time.
1N/ASee L<perlhack/PERL_DESTRUCT_LEVEL> for more information.
1N/A
1N/AA more complete garbage collection strategy will be implemented
1N/Aat a future date.
1N/A
1N/AIn the meantime, the best solution is to create a non-recursive container
1N/Aclass that holds a pointer to the self-referential data structure.
1N/ADefine a DESTROY method for the containing object's class that manually
1N/Abreaks the circularities in the self-referential structure.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AA kinder, gentler tutorial on object-oriented programming in Perl can
1N/Abe found in L<perltoot>, L<perlboot> and L<perltooc>. You should
1N/Aalso check out L<perlbot> for other object tricks, traps, and tips, as
1N/Awell as L<perlmodlib> for some style guides on constructing both
1N/Amodules and classes.