Is there an OO Perl equivalent to an interface?
interface, oop, perl
Solution
You can create a pure virtual class (or role if you are using Moose or MooseX::Declare):
package Foo;
use strict;
use Carp;
sub new { croak "new not implemented" };
sub do_x { croak "do_x not implemented" };
sub do_y { croak "do_y not implemented" };
But the enforcement will be at run-time. In general, interfaces are needed because the language does not support multiple inheritance and is strictly typed. Perl supports multiple inheritance and (using Moose) something like multiple inheritance (but better) called roles and it is dynamically typed. The strict vs dynamic typing comes down to duck typing (if it quacks() like duck, walks() like a duck, and swims() like a duck, then it is a duck). In Perl, you say:
if ($some_obj->can("quack") {
$some_obj->quack;
} else {
croak "incompatible class type: ", ref $some_obj;
}
Problem
I know with OO Perl I can have objects and inheritance, but are interfaces implemented? If so, how are they enforced?