[tpm] Understanding 'use base ...'

Richard Dice rdice at pobox.com
Mon Jul 14 15:15:36 PDT 2008


Hi Madi,

I've seen a few replies to your posting but none so far actually talk about
the idea behind "is a", which it seemed to me you were looking for.

In object oriented programming, the two major kinds of relationships classes
and objects might have with each other is the "is a" relationship (i.e.
inheritance) and the "has a relationship" (i.e. composition).

By declaring class Dog to have an is-a relationship with class Mammal, you
are saying that Dogs have all the properties that Mammals do (for instance,
live birth of young, production of milk, hair/fur, kinship with Dan
Fielding), plus they have some specific properties that only Dogs have
(barking, chasing after Frisbees, being man's best friend).  [You could go a
bit further and state that no animal can be created _directly_ as being a
Mammal;  animals can only be specific types of Mammals.  As such, you could
consider Mammal to be an interface specification class (aka virtual class),
but this is a more advanced concept that we don't really need to go into
now.)

In English, you would read this relationship as "a dog is a mammal."  In
Perl, you would say:

package Dog;
use base qw (Mammal);

But really, Dogs are Canines, so you'd probably say Dog is-a Canine, and
you'd pick up on being a Mammal by virtue of Canine is-a Mammal, and Mammal
is-a Chordate, and Chordate is-a Animal, and Animal is-a Life.  Is-a
relationships often chain their way to some level of complexity.

package Life;
# in Perl, implicitly uses the base class "UNIVERSAL"
.
. # DNA, i haz it!
.
package Animal;
use base qw (Life);
.
.
.
package Mammal;
use base qw (Animal);
.
.
.
package Canine;
use base qw (Mammal);
.
.
.
package Dog;
use base qw (Canine);
.
.
.
1;

I've seen some talk of multiple inheritance.  This is technically possible
in Perl but often controversial.  That would be like saying Dog is-a Canine,
but Dog is-a Pet also.  (Rocks occasionally are pets too, as are peeves.)
In Perl this would be:

package Dog;
use base qw (Canine Pet);
.
.
.
1;

Old-school, you'd establish is-a relationships by manipulating the @ISA
array.

Has-a relationships is where the thing possesses another thing.  Dogs have
eyes.  They have tails too.  They have collars and leashes.  Each of these
things can be had by wildly different other things (e.g. octopuses, jet
airplanes, B&D fetishists) so these objects can be free-floating things that
you can tack onto the side of anything else.  They can have their own is-a
relationship hierarchies too.  There isn't a syntax in the language that
specifically implements has-a relationships.  You just kind of throw those
into your objects.

package Dog;

use base qw (Canine Pet);

use Eye;
use Tail;
use NaughtyNaughty;

sub new {
    my $class = shift;
    my $eye = Eye->new();
    my $tail = Tail->new();
    my $collar = NaughtyNaughty->new();
    my $self = bless { eye => $eye, tail => $tail, collar => $collar },
$class;
    return $self;
}

.
.
.

1;

Cheers,
 - Richard

On Mon, Jul 14, 2008 at 2:53 PM, Madison Kelly <linux at alteeve.com> wrote:

> Hi all,
>
>  I am working on some docs, and noticed that a module is called via:
>
> use base qw(Net::DBus::Object);
>
>  I am trying to understand the 'use base' syntax, and all I've been able to
> find in perldoc is in perlmodlib:
>
> base        Establish IS-A relationship with base classes at compile time
>
>  Can someone either explain to me what this means, or better, point me at
> what docs explain "IS-A relationship"s? I hate "just accepting" that
> something is at it is. Probably why I never get anything actually done. :)
>
>  Thanks as always!
>
> Madi
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20080714/c2e1274e/attachment.html>


More information about the toronto-pm mailing list