[prog] perl interfaces

Caroline Johnston johnston at biochemistry.ucl.ac.uk
Thu Apr 15 10:56:06 EST 2004


Thanks Mary,

Quick question: If I understood the code properly, doesn't this mean that
if you don't implement a method, you won't get any error until that method
is called? Is there any standard way of getting an error when you try to
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. 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.

Cheers,

Cxx.

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


On Thu, 15 Apr 2004, Mary wrote:

> On Wed, Apr 14, 2004, Caroline Johnston wrote:
> > Any pitfalls I should know about? Any other solutions I ought to take
> > a look at?
> 
> Damian Conway, in "Object Oriented Perl" (section 6.2.5 in my edition)
> suggests the following boiler-plate:
> 
> sub METHOD::ABSTRACT
> {
>     my ($self) = @_;
>     my $object_class = ref($self);
>     my ($file, $line, $method) = (caller(1))[1..3];
>     my $loc = "at $file, line $line\n";
>     die "call to abstract method $(method) $loc";
> }
> 
> and then for any method you want to be abstract, define it in the
> interface class as:
> 
> sub abstractMethod { ABSTRACT METHOD @_ };
> 
> If abstractMethod is ever called, then it will execute the boilerplate
> and the program will die with a nice "call to method abstractMethod at
> file FILE line LINE" message.
> 
> The caller(1) call gets the details of the call to abstractMethod
> including file and line number. (It's caller(1) rather than caller(0)
> because we don't want it to say "call to method ABSTRACT at
> file FILE line LINE".)
> 
> -Mary
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
> 



More information about the Programming mailing list