Hi,

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:

Any good practices or advice welcome,

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.