The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

MRP::Interface - defines object interfaces

DESCRIPTION

Allows you to specify the interface that an object is expected to implement without implying an inheritance hierachy.

SYNOPSIS

  MRP::Interface->create(Foo => {''=>'My realy usefull interface',  # interface description
                                 action=>'performs an action',      # functions and their descriptions
                                 reaction=>'responds to an action', # ...
                                });

 MRP::Interface->Foo->implementedBy('Bar'); # register package Bar as implementing interface Foo
                                            # Package Bar must have functions 'action' and 'reaction'
                                            # or a fatal error is generated.
 $bar = new Bar;
 $hoot = new Hoot;

 MRP::Interface->Foo->implementedBy($bar);  # returns true as $bar does implement Foo
 MRP::Interface->Foo->implementedBy($hoot); # returns false as $hoot doen't implement Foo
 MRP::Interface->Gee->implementedBy($bar);  # fatal error if interface Gee is not defined


 $int = MRP::Interface->Foo;                # gets the interface object for 'Foo'
 print $int->name();                        # prints out 'Foo'
 %functions = %{$int->functions()};         # %functions now contains Foo's functions and descriptions

FUNCTIONS

create

Creates a new interface. You can create any number of interfaces with a single call. Treats the parameters as a hash, where each key is an interface name, and each value is a hashref. The hasref key/value pairs specify function names and descriptions for those functions. If the function name is '' then this is used as the interface description.

 MRP::Interface->create( interfaceName => { '' => $description_for_interfaceName,
                                            func1 => $description_for_func1,
                                            func2 => $description_for_func2,
                                            ...
                                            funcn => $description_for_funcn },
                         otherInterface => { ..... }
 );
functions

Returns the hash of function names and descriptions used to create the interface.

name

Returns the name of the interface.

implementedBy

If the argument is a reference (presumably an object) then return true or false depending on whether the object is of a class that implements the interface.

If the argument is a package name (not a reference) then the package is registered as implementing the interface. If the package does not provide all of the required functions then an error is thrown.

anything else

Any other method is treated as if it where the name of an interface to retrieve. Thus, MRP::Interface->Holly will retrieve the interface named Holly. If Holly does not exist then a fatal error is thrown. So, you can have code like:

 MRP::Interface->Holly->implementedBy($obj)
  || die "$obj does not implement interface Holly";

AUTHOR

Matthew Pocock mrp@sanger.ac.uk