=head2 Object Representation
By far the most common mechanism used in Perl to represent a Pascal
-record, a C struct, or a C++ class an anonymous hash. That's because a
+record, a C struct, or a C++ class is an anonymous hash. That's because a
hash has an arbitrary number of data fields, each conveniently accessed by
an arbitrary name of your own devising.
Destruction happens automatically via Perl's garbage collection (GC)
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.
+Perl's notion of the right time to call a destructor is not well-defined
+currently, which is why your destructors should not rely on when they are
+called.
Why is DESTROY in all caps? Perl on occasion uses purely uppercase
function names as a convention to indicate that the function will
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
+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
then the new derived class can be used as a drop-in replacement for the
old one. This means you should be able to write a program like this:
- use Employee
+ use Employee;
my $empl = Employee->new();
$empl->name("Jason");
$empl->age(23);
Perl programmers have responded to this by creating several different
class construction classes. These metaclasses are classes
that create other classes. A couple worth looking at are
-Class::Template and Alias. These and other related metaclasses can be
+Class::Struct and Alias. These and other related metaclasses can be
found in the modules directory on CPAN.
-=head2 Class::Template
+=head2 Class::Struct
-One of the older ones is Class::Template. In fact, its syntax and
+One of the older ones is Class::Struct. 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
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.
+The Class::Struct::struct() function alleviates this tedium.
Here's a simple example of using it:
- use Class::Template qw(struct);
+ use Class::Struct qw(struct);
use Jobbie; # user-defined; see below
struct 'Fred' => {
printf "perl.com's real name is %s, address %s\n",
$h->name, inet_ntoa($h->addr);
-Here's how to do this using the Class::Template module.
+Here's how to do this using the Class::Struct module.
The crux is going to be this call:
struct 'Net::hostent' => [ # note bracket
addr_list => '@',
};
-and then Class::Template would have used an anonymous hash as the object
+and then Class::Struct would have used an anonymous hash as the object
type, instead of an anonymous array. The array is faster and smaller,
but the hash works out better if you eventually want to do inheritance.
Since for this struct-like object we aren't planning on inheritance,
BEGIN {
use Exporter ();
- use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
- @ISA = qw(Exporter);
+ use vars qw(@EXPORT @EXPORT_OK %EXPORT_TAGS);
@EXPORT = qw(gethostbyname gethostbyaddr gethost);
@EXPORT_OK = qw(
$h_name @h_aliases
}
use vars @EXPORT_OK;
- use Class::Template qw(struct);
+ # Class::Struct forbids use of @ISA
+ sub import { goto &Exporter::import }
+
+ use Class::Struct qw(struct);
struct 'Net::hostent' => [
name => '$',
aliases => '@',
sub populate (@) {
return unless @_;
- my $hob = new(); # Class::Template made this!
+ my $hob = new(); # Class::Struct made this!
$h_name = $hob->[0] = $_[0];
@h_aliases = @{ $hob->[1] } = split ' ', $_[1];
$h_addrtype = $hob->[2] = $_[2];
We've snuck in quite a fair bit of other concepts besides just dynamic
class creation, like overriding core functions, import/export bits,
-function prototyping, and short-cut function call via C<&whatever>.
-These all mostly make sense from the perspective of a traditional module,
-but as you can see, we can also use them in an object module.
+function prototyping, short-cut function call via C<&whatever>, and
+function replacement with C<goto &whatever>. These all mostly make
+sense from the perspective of a traditional module, but as you can see,
+we can also use them in an object module.
You can look at other object-based, struct-like overrides of core
functions in the 5.004 release of Perl in File::stat, Net::hostent,
as with any other local().
It would be nice to combine Alias with
-something like Class::Template or Class::MethodMaker.
+something like Class::Struct or Class::MethodMaker.
=head2 NOTES
and
L<overload>.
-=head1 COPYRIGHT
+=head1 AUTHOR AND COPYRIGHT
+
+Copyright (c) 1997, 1998 Tom Christiansen
+All rights reserved.
-I I<really> hate to have to say this, but recent unpleasant
-experiences have mandated its inclusion:
-
- Copyright 1996 Tom Christiansen. All Rights Reserved.
-
-This work derives in part from the second edition of I<Programming Perl>.
-Although destined for release as a manpage with the standard Perl
-distribution, it is not public domain (nor is any of Perl and its docset:
-publishers beware). It's expected to someday make its way into a revision
-of the Camel Book. While it is copyright by me with all rights reserved,
-permission is granted to freely distribute verbatim copies of this
-document provided that no modifications outside of formatting be made,
-and that this notice remain intact. You are permitted and encouraged to
-use its code and derivatives thereof in your own source code for fun or
-for profit as you see fit. But so help me, if in six months I find some
-book out there with a hacked-up version of this material in it claiming to
-be written by someone else, I'll tell all the world that you're a jerk.
-Furthermore, your lawyer will meet my lawyer (or O'Reilly's) over lunch
-to arrange for you to receive your just deserts. Count on it.
+When included as part of the Standard Version of Perl, or as part of
+its complete documentation whether printed or otherwise, this work
+may be distributed only under the terms of Perl's Artistic License.
+Any distribution of this file or derivatives thereof I<outside>
+of that package require that special arrangements be made with
+copyright holder.
+
+Irrespective of its distribution, all code examples in this file
+are hereby placed into the public domain. You are permitted and
+encouraged to use this code in your own programs for fun
+or for profit as you see fit. A simple comment in the code giving
+credit would be courteous but is not required.
+
+=head1 COPYRIGHT
=head2 Acknowledgments