in reply to Why do you need abstract classes in Perl?

"Abstract classes and Perl" just makes me chuckle. Like you said, abstract classes allow you to define an interface... And in Perl, that comes down to precisely "a few method names". Despite there being at least two implementations (metioned above by tilly) of abstract classes in Perl, I can't help but wonder whether the minimal value is worth the effort.

Sure, using an abstract class in Perl prevents you from completely forgetting to implement one of the methods. It doesn't require that you even come close to using the same interface in the methods that share a name, because Perl has no useful, built-in tools for defining interfaces (prototypes are built-in but not useful for defining an interface and there are some modules that help you define an interface but they aren't built-in).

Now, if your interface was defined via something like Class::Contract, then an abstract class might be a worthwhile thing.

        - tye (but my friends call me "Tye")
  • Comment on (tye)Re: Why do you need abstract classes in Perl?

Replies are listed 'Best First'.
Re: (tye)Re: Why do you need abstract classes in Perl?
by salvadors (Pilgrim) on Mar 06, 2001 at 16:35 UTC

    I can't help but wonder whether the minimal value is worth the effort. Sure, using an abstract class in Perl prevents you from completely forgetting to implement one of the methods...

    This, by itself, can be enough.

    Take, for example, an online shop that sells several different types of items - each of which will be a class. It quite often makes sense here to ensure that each of these has all the methods that will be needed in the system to be able to buy the item - a title, price, SKU, whatever.

    Yes, you could just add it to your standards that anyone adding a new class of item must add these, and ensure that your reviewers look for this. But if you have an abstract class that all of these use, with an array of necessary types that you just add to any time you need to insist on a new abstract class method, then you don't need to do this. You just wait for the @ISA line to blow up!

    Tony

Re: (tye)Re: Why do you need abstract classes in Perl?
by Madams (Pilgrim) on Mar 06, 2001 at 07:34 UTC
    Just to butt my $0.02 in...

    Doesn't perl provide one abstract class?
    package ("class") UNIVERSAL? it provides a basic set of interfaces: can(), isa(), and VERSION().
    Or am I confused (possible)?
    _________________
    madams@scc.net
    (__) (\/) /-------\/ / | 666 || * ||----||

      No, if UNIVERSAL were an abstract class, then the can() and isa() methods would be unimplemented (that is what makes a class abstract -- none of its methods are implemented, the class consists of only a interface definition and no implementation) and you would be required to implement them yourself in any class you build that inherits from UNIVERSAL (which would be all classes since all classes inherit from UNIVERSAL implicitly).

              - tye (but my friends call me "Tye")
        I respectfully beg to differ with this definition of abstract. An abstract class most certainly can provide implementations. Here's what the Gang of Four (Gamma, Helm, Johnson, and Vlissides) have to say on page 15 of Design Patterns.
        An abstract class is one whose main purpose is to define a common interface for its subclasses. An abstract class will defer some or all of its implementation to operations defined in its subclasses; hence an abstract class cannot be instantiated. The operations that an abstract class declares but doesn't implement are called abstract operations. Classes that aren't abstract are called concrete classes.
        Without the ability to have implementations in abstract classes, you'd need a pair of superclasses to factor common code out of like classes: one for the abstract operations, the other for the common, factored operations. This wastes a class, and makes the class hierarchy one deeper than is really necessary.

        Thanks tye,
        Methinks I was wacked on abstract, pure abstract, virtual, and pure virtual. None of which mean the same things.
        _________________
        madams@scc.net
        (__) (\/) /-------\/ / | 666 || * ||----||