class. If you try to call an undefined method on an object, Perl won't
complain, but the program will trigger an exception while it's running.
Likewise, if you call a method expecting a prime number as its argument
-with an even one instead, you can't expect the compiler to catch this.
+with a non-prime one instead, you can't expect the compiler to catch this.
(Well, you can expect it all you like, but it's not going to happen.)
Let's suppose you have a well-educated user of your Person class,
system, which is a quick but somewhat lazy reference-based GC system.
To know what to call, Perl insists that the destructor be named DESTROY.
-Why is DESTROY in all caps? Perl on occasion uses purely upper-case
+Why is DESTROY in all caps? Perl on occasion uses purely uppercase
function names as a convention to indicate that the function will
be automatically called by Perl in some way. Others that are called
implicitly include BEGIN, END, AUTOLOAD, plus all methods used by
Perl doesn't impose restrictions on who gets to use which methods.
The public-versus-private distinction is by convention, not syntax.
-(Well, unless you use the Alias module described below in L</"Data Members
-as Variables">.) Occasionally you'll see method names beginning or ending
+(Well, unless you use the Alias module described below in
+L</"Data Members as Variables">.) Occasionally you'll see method names beginning or ending
with an underscore or two. This marking is a convention indicating
that the methods are private to that class alone and sometimes to its
closest acquaintances, its immediate subclasses. But this distinction
Got that? Maybe not. Ok, let's say that some other class "borrowed"
(well, inherited) the DESTROY method as it was defined above. When those
-objects are destructed, the original $Census variable will be altered,
+objects are destroyed, the original $Census variable will be altered,
not the one in the new class's package namespace. Perhaps this is what
you want, but probably it isn't.
to remind ourselves that this field is special and not to be used as
a public data member in the same way that NAME, AGE, and PEERS are.
(Because we've been developing this code under the strict pragma, prior
-to 5.004 we'll have to quote the field name.)
+to perl version 5.004 we'll have to quote the field name.)
sub new {
my $proto = shift;
-- ${ $self->{"_CENSUS"} };
}
-What happens if a derived class (which we'll all C<Employee>) inherits
-methods from this person one? Then C<Employee->debug()> when called
-as a class method manipulates $Person::Debugging not $Employee::Debugging.
+What happens if a derived class (which we'll call Employee) inherits
+methods from this Person base class? Then C<Employee-E<gt>debug()>, when called
+as a class method, manipulates $Person::Debugging not $Employee::Debugging.
=head2 Class Destructors
when the wizard decides you'd look better as a frog.)
Every now and then you'll want to have a method call trigger both its
-derived class (also know as "subclass") version as well as its base class
+derived class (also known as "subclass") version as well as its base class
(also known as "superclass") version. In practice, constructors and
destructors are likely to want to do this, and it probably also makes
sense in the debug() method we showed previously.
having to go and add it to each and every @ISA. Well, it turns out that
you can. You don't see it, but Perl tacitly and irrevocably assumes
that there's an extra element at the end of @ISA: the class UNIVERSAL.
-In 5.003, there were no predefined methods there, but you could put
+In version 5.003, there were no predefined methods there, but you could put
whatever you felt like into it.
-However, as of 5.004 (or some subversive releases, like 5.003_08),
+However, as of version 5.004 (or some subversive releases, like 5.003_08),
UNIVERSAL has some methods in it already. These are built-in to your Perl
binary, so they don't take any extra time to load. Predefined methods
include isa(), can(), and VERSION(). isa() tells you whether an object or
$his_vers = $ob->VERSION();
However, we don't usually call VERSION ourselves. (Remember that an all
-upper-case function name is a Perl convention that indicates that the
+uppercase function name is a Perl convention that indicates that the
function will be automatically used by Perl in some way.) In this case,
it happens when you say
use Some_Module 3.0;
-If you wanted to add versioning to your Person class explained
+If you wanted to add version checking to your Person class explained
above, just add this to Person.pm:
use vars qw($VERSION);
sub new {
my $self = [];
$self->[$NAME] = undef; # this is unnecessary
- $self->[$AGE] = undef; # as it this
+ $self->[$AGE] = undef; # as is this
$self->[$PEERS] = []; # but this isn't, really
bless($self);
return $self;
1; # so the require or use succeeds
-You might guess that the array access will be a lot faster than the
-hash access, but they're actually comparable. The array is a little
+You might guess that the array access would be a lot faster than the
+hash access, but they're actually comparable. The array is a I<little>
bit faster, but not more than ten or fifteen percent, even when you
replace the variables above like $AGE with literal numbers, like 1.
A bigger difference between the two approaches can be found in memory use.
A hash representation takes up more memory than an array representation
-because you have to allocation memory for the keys as well as the values.
-However, it really isn't that bad, especially since as of 5.004,
+because you have to allocate memory for the keys as well as for the values.
+However, it really isn't that bad, especially since as of version 5.004,
memory is only allocated once for a given hash key, no matter how many
hashes have that key. It's expected that sometime in the future, even
these differences will fade into obscurity as more efficient underlying
version of $self.
When a method like C<$him-E<gt>name("Jason")> is called, its implicit
-zeroth argument is the invoking object just as it is with all method
+zeroth argument is the invoking object--just as it is with all method
calls. But in this case, it's our code reference (something like a
function pointer in C++, but with deep binding of lexical variables).
There's not a lot to be done with a code reference beyond calling it, so
One of the older ones is Class::Template. In fact, its syntax and
interface were sketched out long before perl5 even solidified into a
-real thing. What it does is provide you a way to "declare"
-a class as having objects whose fields are of a specific type.
-The function that does this is called, not surprisingly
-enough, struct().
+real thing. What it does is provide you a way to "declare" a class
+as having objects whose fields are of a specific type. The function
+that does this is called, not surprisingly enough, struct(). Because
+structures or records are not base types in Perl, each time you want to
+create a class to provide a record-like data object, you yourself have
+to define a new() method, plus separate data-access methods for each of
+that record's fields. You'll quickly become bored with this process.
+The Class::Template::struct() function alleviates this tedium.
Here's a simple example of using it:
functions in the 5.004 release of Perl in File::stat, Net::hostent,
Net::netent, Net::protoent, Net::servent, Time::gmtime, Time::localtime,
User::grent, and User::pwent. These modules have a final component
-that's all lower-case, by convention reserved for compiler pragmas,
+that's all lowercase, by convention reserved for compiler pragmas,
because they affect the compilation and change a built-in function.
They also have the type names that a C programmer would most expect.
call a class method (one expecting a string argument) on an
object (one expecting a reference), or vice versa.
->From the C++ perspective, all methods in Perl are virtual.
+Z<>From the C++ perspective, all methods in Perl are virtual.
This, by the way, is why they are never checked for function
prototypes in the argument list as regular built-in and user-defined
functions can be.