Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Interfaces

by Dominus (Parson)
on Dec 01, 2000 at 04:28 UTC ( [id://44271]=note: print w/replies, xml ) Need Help??


in reply to Interfaces

[oops, this is actually an answer to the very similar question posed in this thread. It's pretty much the same question, but the poster wants the checking for missing methods to occur at compile time instead of at run time (or not at all.) I hope this explanation makes clearer what problem I'm trying to solve and why I needed more code than the simpler solutions shown above]

Well, I have an idea, but I've never really tried it out, so I don't know how practical it is. My idea is that the abstract base class can keep track of who is derived from it, and have an INIT block that checks to make sure all its derived classes define the appropriate methods. The INIT block is called after compilation is complete, but before program execution begins. A test implementation looked reasonable:

package Abstract; use Carp; my @inheritors; sub import { my $caller = caller; push @inheritors, $caller; } my @abstract_methods = qw(swim fly); sub INIT { my $bad = 0; for my $class (@inheritors) { for my $meth (@abstract_methods) { no strict 'refs'; unless (defined &{"${class}::$meth"}) { $bad=1; warn "Class $class inherits from Abstract, but does not define + $meth.\n"; } } } croak "Compilation aborted" if $bad; } 1;
Abstract wants its subclasses to define swim and fly methods. If you define a class, say Fish, which inherits from Abstract and defines a swim method but no fly method, you get a fatal error at compile time:
Class Fish inherits from Abstract, but does not define fly. Compilation aborted at fish.pl line 0
Here's the Fish I used:
package Fish; use Abstract; sub swim { "bloop bloop bloop"; } 1;
Then test with perl -e 'use Fish'.

There are some problems with this implementation. For example, you might want some way to derive less-abstract classes from the abstract base class, and this implementation doesn't allow that. But I think the basic idea is sound.

The other thing that came to mind is that Damian Conway probably has something interesting to say about this. Have you checked his book?

Replies are listed 'Best First'.
Re^2: Interfaces
by petemar1 (Pilgrim) on Apr 30, 2005 at 00:20 UTC

    Given...


    #!/usr/local/bin/perl -w # Doable.pm # my dbc OOPerl "interface" use strict; use warnings; use FindBin::libs; use Class::Contract; package Doable; { unless( eval { contract { # constant class attr 'NADA'; invar { ${self->NADA} = 0; }; abstract ctor 'new'; abstract dtor; # Control abstract method 'doTheThing'; # Test: DON'T implement doThatThing # and hope an exception is raised. abstract method 'doThatThing'; pre { defined ${Doable::caller} }; }; # end contract sub new; sub DESTROY; sub doTheThing; # sub doThatThing; 1; } # end eval ) { die( "\n\nThere has been a breach of contract.\n\n" ); } # end + unless } 1; # end Doable


    #!/usr/local/bin/perl -w # Doer.pm # my OOPerl "implementation" use strict; use warnings; use FindBin::libs; use Class::Contract; use private qw(_semprini); use protected qw(_friendly); use public qw(face); # Should be implementing rather than overloading or overriding use overload "Doable::doTheThing" => "Doer::doTheThing"; use Doable; # I want to IMPLEMENT, # not INHERIT, but anyway... package Doer; @Doer::ISA = qw(Doable); { sub new { my $classname = shift; my $self = bless { }, $classname; $self->{_semprini} = 'naughty'; $self->{face} = 'blank'; return($self); } # end new sub DESTROY { my $self = shift; } # end DESTROY sub doTheThing { print"\nTesting...\n\n"; } # end doTheThing # Don't uncomment unless testing. # sub doThatThing { return(0); } # end doThatThing } 1; # end Doer


    #!/usr/local/bin/perl -w # MyApp.pl # my OOPerl "application" use strict; use warnings; use private qw(_MyApp _main _d); use Doer; # I don't want to inherit here, # I just want to "use" and make sub calls package _MyApp; @_MyApp::ISA = qw(Doer); { sub _main; { my $_d = Doer->new; # constructor $_d->doTheThing; # method # print $_d->NADA; # can't see the constant $_d->DESTROY; # destructor print("\nDone! exiting...\n\n"); exit(0); } # end _main } 1; # end _MyApp


    ...What's a clean way of doing that using Class::Contract ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://44271]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-28 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found