[prog] perl interfaces

Jacinta Richardson jarich at perltraining.com.au
Thu Apr 15 21:18:23 EST 2004


On Thu, 15 Apr 2004, Caroline Johnston wrote:

> compile your code? I know it probably goes against the spirit of perl, but
> I really want to force people to implement ALL of the methods in the
> interface. 

You could offer a test function.  Something like this:

package Foo;

sub testall {
        my $this = shift;

	foreach my $funct (qw/virtualfunction1 virtualfunction2
			      virtualfunction3) {
		eval {
			$this->$funct();
		};

		if($@) {
			print "$funct doesn't appear to be implemented in ".
				ref($self) . "\n";
		}
	}
}

sub virtualfunction1 {
	croak "Abstract function called.";
}

Then programmers can easily test that they've done everything at their
will.  Mind you I think Class::Contract or Class::Virtually::Abstract
probably do this kind of thing better than I can run up in a moment's
thought.

This doesn't work if the implementation for a subroutine is something like:

sub virtualfunction2 {
	die "Missing all my arguments" unless @_;
}
	
> I guess I could cobble together something that would check
> this, but the whole point of the exercise is to make the code easy to
> understand and easy to add to, so if there's some standard way of doing
> it, I'd rather use that.

Try the above mentioned modules.  

> PS Is the Damian Conway book useful? Any other OOperl book 
> reccommendations?

Damian's book is the best we've got. 

	Jacinta

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001         |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |



More information about the Programming mailing list