Mail::Box

Main
back: Mail::Box
next: Autoloading

With some effort (based on experience), object oriented code can be very readable. One thing you must not be afraid of, is to create many small packages.

Let us look at different ways to do the same, starting with a blunt approach, by adding a field to the internal data of the object.

package Cat;
sub new() {...; $self->{persian} = ...}
sub isPersian() { shift->{persian} }

package main;
my $mycat = Cat->new(persian => 1);
if($mycat->isPersian) {...}

A nicer approach on the same problem is:

package Cat;
sub isPersian() {0};

package Cat::Persian;
use base 'Cat';
sub isPersian() {1};

package main;
my $mycat = Cat::Persian->new;
if($mycat->isPersian) {...}
Now the data-field is replaced by an object-type.

The third solution is shown on the image at top. There you see we do not need a field, nor a specific method to encode a specialised cat.

There are no strict rules about what to represent in a type, and what in internal data of the object. There should be a strive for small, but not too small objects, which form some consistent pattern. Usually you known at the third or fourth implementation.


 
Mail::Box Autoloading

Created by Mark Overmeer with PPresenter on 5 August 2001