I'm about to embark on an oo development project in perl and require (no pun intended) a way of implementing an interface in a fool proof fashion with compile time enforcement on required virtual methods. After having read through some info on abstract classes and interfaces, I decided to go with Class::Virtually::Abstract because it seemed to best meet my needs. Incidentally, I'm not sure why but I suspect overriding bless and import is fundamentally evil.
Anyway, the following code compiles, but there are still a few missing pieces of the puzzle. Namely:
Thanks.
#################################### package iface2; print "\n\n",__PACKAGE__,"\n\n"; use base qw(Class::Virtually::Abstract); __PACKAGE__->virtual_methods(qw(new iface2_method)); 1; __END__ #################################### package iface1; print "\n\n",__PACKAGE__,"\n\n"; use base qw(Class::Virtually::Abstract); __PACKAGE__->virtual_methods(qw(new iface1_method)); 1; __END__ #################################### package class; use base qw(iface1 iface2); # See what methods we're obligated to implement. my @must_implement = __PACKAGE__->virtual_methods; print "\n",__PACKAGE__," must implement @must_implement\n\n"; # Check to make sure this PACKAGE implemented everything my @missing = __PACKAGE__->missing_methods; die __PACKAGE__ . ' forgot to implement ' . join ', ', @missing if @mi +ssing; sub new { my ( $class ) = shift; my $self = {}; bless $self, $class; return $self; } sub iface1_method { my ( $self, $arg ) = @_; print"\nclass->iface1_method : called with $arg\n\n"; return $self; } sub iface2_method { my ( $self, $arg ) = @_; print"\nclass->iface2_method : called with $arg\n\n"; return $self; } #################################### # bootstrap #################################### #!/ms/dist/perl5/bin/perl5.8 use Data::Dumper; use class; my $c = new class; $c->iface1_method("passed in to 1"); $c->iface2_method("passed in to 2"); print "Dump of object: class:\n",Dumper $c; print "\nEND \n\n";
In reply to How best to implement multiple interfaces in perl by starbuck
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |