in reply to Java-style interfaces in Perl
I've done this in the past, using code like the following:
package Base; use Carp; my @methods = qw(new something something_else); my %methods; @method{@methods} = (); sub AUTOLOAD { my ($pkg, $method) = $AUTOLOAD =~ /^(.*)::(.*)$/; if (exists $method{$method}) { croak qq(Package "$pkg" must override the method "$method"); } } 1;
If you then define a subclass that uses the Base.pm interface, you get an error if you call an undefined method:
#!/usr/bin/perl -w use strict; package MyObj; use vars qw(@ISA); use Base; @ISA = qw(Base); sub new { return bless {}, shift; } package main; my $thing = MyObj->new; $thing->something;
Of course, this only generates the error if a missing method is called. It's probably possible to do a more stringent check when the derived class is loaded. I'll give that some thought...
--"The first rule of Perl club is you don't talk about Perl club."
|
|---|