|
For the sake of clarity, I
oversimplified some of the facts about objects. Here
are a few of the gorier details:
• Every example I gave of a
constructor was a class method. But object methods
can be constructors, too, if the class was written to
work that way: $new = $old->copy, $node_y =
$node_x->new_subnode, or the like.
• I’ve given the impression that
there’s two kinds of methods: object methods
and class methods. In fact, the same method can be
both, because the distinction has to do only with how
it parses parameters.
• The term "object value" isn’t
something you’ll find used much anywhere else.
It’s just my shorthand for what would properly
be called an "object reference" or "reference to a
blessed item". In fact, people usually say "object"
when they properly mean a reference to that
object.
• I mentioned creating objects with
constructors, but I didn’t mention
destroying them with destructors — a
destructor is a kind of method that you call to tidy
up the object once you’re done with it, and
want it to neatly go away (close connections, delete
temporary files, free up memory, and so on). But
because of the way Perl handles memory, most modules
don’t require destructors.
• I said that class method syntax has
to have the class name, as in $session =
Net::FTP->new($host). Actually, you
can instead use any expression that returns a class
name: $ftp_class = 'Net::FTP'; $session =
$ftp_class->new($host). Moreover,
instead of the method name for object- or
class-method calls, you can use a scalar holding the
method name: $foo->$method($host).
In practice, these formulations are rarely
useful.
Finally, to learn about objects from the
perspective of writing your own classes, see the
perltoot documentation, or Damian
Conway’s exhaustive and clear book Object
Oriented Perl (Manning Publications 1999, ISBN
1-884777-79-1).
|