Class::Interface - A class for implementing/extending interfaces/abstracts in Perl.
package Bouncable; use Class::Interface; &interface; # this actually declares the interface sub bounce; sub getBounceBack; 1;
This creates an interface (a contract between classes if you like) that specifies that each class implementing the Bouncable interface must have an implementation of the routines bounce and getBounceBack.
package Ball; use Class::Interface; &implements( 'Bouncable' ); sub bounce { my $self = shift; print "The ball is bouncing @ ".$self->getBounceBack." strength" } sub getBounceBack { return 10; } 1;
package AbstractInterestCalculator; use Class::Interface; &abstract; # this actually declares this class to be abstract; use Class::AccessorMaker { interest => 5.1, maxInterestValue => 0, } # a hook for doing calculations sub calculate { my ( $self, $value ) = @_; $self->prepare(); $value += $self->getInterestValue( $value ); return $value; } sub prepare; # prepare calculations sub getInterstValue; # get the interest value 1;
package LowInterestCalculator; use Class::Interface; &extends( 'AbstractInterestCalculator' ); sub prepare { my ( $self ) = @_; $self->interest(1.3); # we don't give interest if the value of the account is or # exceeds $10.000 $self->maxInterestValue(10000) } sub getInterstValue { my ( $self, $value ) = @_ if ( $self->maxInterestValue && $value >= $self->maxInterestValue ) { return 0; } $value *= $self->interest; return $value; }
Performs some underwater perl-magic to ensure interfaces are interfaces and classes that implement the interface actually do so.
An interface must use the Class::Interface module.
An interface must call the 'interface' method.
An interface must declare at least one routine
Routines may not have an implementation
An abstract must use the Class::Interface module.
An abstract must call the 'abstract' method.
An abstract must declare at least one abstract routine.
Routines must be declared as one of:
NOTE: When using curly braces in routine declarations they must stay on the same line. The amount of whitespace between them and/or the routine name is free of ruling.
It helps to think of these methods as Java style annotations. But instead of calling them with @interface you use &interface.
Turns the calling class into an interface.
Turns the calling class into an abstract.
Loads the given interfaces and checks the calling class for presence of the wanted routines.
If all goes well pushes the name of the interface to the ISA array of the class.
Loads the given abstract class and checks the calling class for presence of the abstract routines.
If all goes well pushes the name of the abstract class to the ISA array of the class.
To add even more Java behaviour to perl...
Extending or implementing classes that do not already have a constructor can get one injected automaticly.
The code for such a routine is as follows:
sub new { my $class = ref($_[0]) || $_[0]; shift; my $self = bless({}, $class); my %value = @_; foreach my $field ( keys %value ) { $self->$field( $value{$field} ) if $self->can( $field ) } return $self }
In english: An object with a hashref is setup. The constructor can be called like this:
my $object = Object->new( attribute1 => "value", attribute2 => [ qw(a b c)], );
if attributeX exists as an accessor routine in the object it will be set by calling the actual routine.
I would strongly advice using something like Class::AccessorMaker though...
If you want magic constructors; set $Class::Interface::AUTO_CONSTRUCTOR to 1
If anything fails uses Carp::croak. Once you set $Class::Interface::CONFESS to 1 it will spill the guts using confess.
Using things like Class::AccessorMaker accessors are dynamcly created. Class contracts can specify some getters to be present. Does Class::Interface recognize them?
The checks implements() and extends() perform happen well after use time. So using Class::AccessorMaker is save. It performs it magic in use time. Any class that will dynamicly create methods in use time should be usable with Class::Interface.
If your class extends an abstract which provides methods for an interface you are implementing you must first call the &extends annotation.
So:
&extends('Runner'); &implements('Runnable');
And not:
&implements('Runnable'); &extends('Runner');
Carp, UNIVERSAL
Hartog C. de Mik
(cc-sa) 2008, Hartog C. de Mik
cc-sa : http://creativecommons.org/licenses/by-sa/3.0/
To install Class::Interface, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Class::Interface
CPAN shell
perl -MCPAN -e shell install Class::Interface
For more information on module installation, please visit the detailed CPAN module installation guide.