The Gory Details
Sean M. Burke

The Perl Journal, Spring 2000

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).

[Back to main article]
[Back to list of articles]