[tpm] Understanding 'use base ...'

Jim Graham james.a.graham at gmail.com
Mon Jul 14 12:04:22 PDT 2008



  > perldoc base

        base − Establish IS−A relationship with base classes at  
compile time

            package Baz;
            use base qw(Foo Bar);

        Allows you to both load one or more modules, while setting up  
        tance from those modules at the same time.  Roughly similar in  

            package Baz;
            BEGIN {
                require Foo;
                require Bar;
                push @ISA, qw(Foo Bar);

The @ISA array is the fundamental way of creating inheritance of  
objects in Perl. Literally, if you define one class 'Baz' as a sub- 
class of another (base) class 'Foo', you say 'Baz' ISA 'Foo'. See http://en.wikipedia.org/wiki/Isa_%28computer_science%29

'use base' is short-hand for setting up the inheritance yourself. It  
says, 'use the following classes as base classes for my class'.

Perl allows multiple inheritance, so in the above example, 'Baz' ISA  
'Foo' and 'Bar'. This is distinct from Java and Ruby, which only allow  
direct inheritance, although they address these issues with interfaces  
and mix-ins, respectively.

   Hope this helps, Jim

On 14-Jul-08, at 2:53 PM, Madison Kelly 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

More information about the toronto-pm mailing list