Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Java-style interfaces in Perl

by meonkeys (Chaplain)
on Sep 27, 2001 at 13:07 UTC ( [id://115035]=perlquestion: print w/replies, xml ) Need Help??

meonkeys has asked for the wisdom of the Perl Monks concerning the following question:

Interfaces are an idea that I appreciated when learning Java and I'd like to apply this logic to Perl.

Basically, a Java interface is an abstract base class whose methods must be redefined by classes that implement the interface (thrown errors must also be thrown or caught, but let's overlook this for now to keep it simple). The closest available analogy I can think of in Perl is when you want to implement tie, you can write your own STORE, FETCH, DELETE, etc. I like interfaces because they bring usefulness from many packages while avoiding multiple inheritance and the problems that can ensue from incorrectly implementing multiple inheritance.

I was curious on your experiences with this approach, and if you think it's a good approach at all. If you have done it, how did you set it up?

Thank you,
Adam

Replies are listed 'Best First'.
Re: Java-style interfaces in Perl
by mirod (Canon) on Sep 27, 2001 at 13:31 UTC

    Look no further than the bible of OO in Perl: Object Oriented Perl describes how to implement this on page 185-186.

    The basic idea is to set all the methods in the abstract class so that they die (or croak) if called. Of course you may want one more level of abstraction and write a generic method that will do this, and just have any other method call it.

Re: Java-style interfaces in Perl
by davorg (Chancellor) on Sep 27, 2001 at 13:27 UTC

    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...

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re (tilly) 1: Java-style interfaces in Perl
by tilly (Archbishop) on Sep 27, 2001 at 16:35 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found