=head1 DESCRIPTION
-First of all, you need to understand what references are in Perl.
+First you need to understand what references are in Perl.
See L<perlref> for that. Second, if you still find the following
reference work too complicated, a tutorial on object-oriented programming
in Perl can be found in L<perltoot> and L<perltootc>.
package Critter;
sub spawn { bless {} }
-In fact, this might even be preferable, because the C++ programmers won't
+This might even be preferable, because the C++ programmers won't
be tricked into thinking that C<new> works in Perl as it does in C++.
It doesn't. We recommend that you name your constructors whatever
makes sense in the context of the problem you're solving. For example,
return $self;
}
-In fact, you often see such a thing in more complicated constructors
+You often see such a thing in more complicated constructors
that wish to call methods in the class as part of the construction:
sub new {
return $self;
}
-Or if you expect people to call not just C<CLASS-E<gt>new()> but also
-C<$obj-E<gt>new()>, then use something like this. The initialize()
+Or if you expect people to call not just C<< CLASS->new() >> but also
+C<< $obj->new() >>, then use something like this. The initialize()
method used will be of whatever $class we blessed the
object into:
the reference is generally treated as an opaque value that may
be accessed only through the class's methods.
-Although a a constructor can in theory re-bless a referenced object
+Although a constructor can in theory re-bless a referenced object
currently belonging to another class, this is almost certainly going
to get you into trouble. The new class is responsible for all
cleanup later. The previous blessing is forgotten, as an object
supplied in the UNIVERSAL class; see L<"Default UNIVERSAL methods"> for
more details.
-If a missing method is found in one of the base classes, it is cached
+If a missing method is found in a base class, it is cached
in the current class for efficiency. Changing @ISA or defining new
subroutines invalidates the cache and causes Perl to do the lookup again.
If none of that works, Perl finally gives up and complains.
+If you want to stop the AUTOLOAD inheritance say simply
+
+ sub AUTOLOAD;
+
+and the call will die using the name of the sub being called.
+
Perl classes do method inheritance only. Data inheritance is left up
to the class itself. By and large, this is not a problem in Perl,
because most classes model the attributes of their object using an
display {find Critter "Fred"} 'Height', 'Weight';
-For C++ fans, there's also a syntax using -E<gt> notation that does exactly
+For C++ fans, there's also a syntax using -> notation that does exactly
the same thing. The parentheses are required if there are any arguments.
$fred = Critter->find("Fred");
L<"WARNING"> below.
There are times when you wish to specify which class's method to use.
-In this case, you can call your method as an ordinary subroutine
+Here you can call your method as an ordinary subroutine
call, being sure to pass the requisite first argument explicitly:
$fred = MyCritter::find("Critter", "Fred");
MyCritter::display($fred, 'Height', 'Weight');
-Note however, that this does not do any inheritance. If you wish
+Unlike method calls, function calls don't consider inheritance. If you wish
merely to specify that Perl should I<START> looking for a method in a
particular package, use an ordinary method call, but qualify the method
name with the package like this:
Those actually parse as the very surprising:
$obj->move->{FIELD}; # Well, lookee here
- $ary->move->[$i]; # Didn't expect this one, eh?
+ $ary->move([$i]); # Didn't expect this one, eh?
Rather than what you might have expected:
$obj->{FIELD}->move(); # You should be so lucky.
$ary[$i]->move; # Yeah, sure.
-The left side of ``-E<gt>'' is not so limited, because it's an infix operator,
+The left side of ``->'' is not so limited, because it's an infix operator,
not a postfix operator.
As if that weren't bad enough, think about this: Perl must guess I<at
as a subroutine, rather than the desired class's method. The compiler
tries to cheat by remembering bareword C<require>s, but the grief if it
messes up just isn't worth the years of debugging it would likely take
-you to to track such subtle bugs down.
+you to track such subtle bugs down.
-The infix arrow notation using ``C<-E<gt>>'' doesn't suffer from either
+The infix arrow notation using ``C<< -> >>'' doesn't suffer from either
of these disturbing ambiguities, so we recommend you use it exclusively.
=head2 Default UNIVERSAL methods
strange effects if the Perl code dynamically changes @ISA in any package.
You may add other methods to the UNIVERSAL class via Perl or XS code.
-You do not need to C<use UNIVERSAL> in order to make these methods
+You do not need to C<use UNIVERSAL> to make these methods
available to your program. This is necessary only if you wish to
have C<isa> available as a plain subroutine in the current package.
of your choosing get called. Explicitly calling DESTROY is also possible,
but is usually never needed.
-Do not confuse the foregoing with how objects I<CONTAINED> in the current
+Do not confuse the previous discussion with how objects I<CONTAINED> in the current
one are destroyed. Such objects will be freed and destroyed automatically
when the current object is freed, provided no other references to them exist
elsewhere.
=head2 Two-Phased Garbage Collection
-For most purposes, Perl uses a fast and simple reference-based
-garbage collection system. For this reason, there's an extra
+For most purposes, Perl uses a fast and simple, reference-based
+garbage collection system. That means there's an extra
dereference going on at some level, so if you haven't built
your Perl executable using your C compiler's C<-O> flag, performance
will suffer. If you I<have> built Perl with C<cc -O>, then this
Notice that "global destruction" bit there? That's the thread
garbage collector reaching the unreachable.
-Objects are always destructed, even when regular refs aren't and in fact
-are destructed in a separate pass before ordinary refs just to try to
+Objects are always destructed, even when regular refs aren't. Objects
+are destructed in a separate pass before ordinary refs just to
prevent object destructors from using refs that have been themselves
destructed. Plain refs are only garbage-collected if the destruct level
is greater than 0. You can test the higher levels of global destruction