Recently I opened up one of my old college textbooks to see what would happen if I tried to implement some examples in Perl. When I ran across abstract classes, the gears in my head started to churn: "Why would Perl even need abstract classes?"

This question evently led me to question how much OOP I have written with Perl. Not much. As a matter of fact, whenever I did, it was not a serious endeavor.

Rather than question just how practical OOP is in Perl, I instead focus on abstract classes in Perl. Why do you need an abstract class in Perl?

First off. What is an abstract class?
don't answer that, it's a rhetorical question :)

An abstract class is class that is never meant to be instantiated, instead its existance is to provide a basis for other classes that will inherit from it. Java and C++ have reserved tokens for declaring abstract classes, but Perl does not. For that matter, Perl doesn't even have a 'class' reserved token (who needs it anyways). But that's okay, Perl is just following a path that has been accepted throughout the existance of modern day computers - if the 'hardware' can't do it, 'software' can and will be written to do it. Perl is just as capable of providing classes, abstract and concrete - it just might not be as 'pretty' as other languages.

Why are abstract classes even needed? What convience do they provide?

At first glance, the obvious answer is common functionality. For example, Java provides a toString() method that is defined in the Object class. Since all objects in Java are sub-classes of Object, they too have a toString() method. But Object is not an abstract class - you can create an Object class. Looks like common functionality is not the reason . . .

Rather than ask the question again - I will now jump straight to the point. When I said that an abstract class provides a common basis for other classes, I was not quite accurate, because a class does not have to be abstract to provide this basis. I should have instead said that an abstract class provides a common interface.

The textbook I was looking at was Design Patterns by qw(Gamma Helm Johnson Vlissides), by the way. Here is their answer to why abstract classes are a 'good thing' in a nut shell:

  1. classes do not depend upon the 'types' of objects and attributes
  2. classes are only bound to an interface
and I have to reprint their mantra - because it's a good one: Interfaces provide the ability to change the 'type' of an attribute within a particular class without breaking any clients that use that class. There is an old saying that goes something like this, "A function should never return a value, a function should return a pointer to a structure that contains a pointer to that value".

Now that is a bit extreme - but you will never have to worry about the 'type' of that value changing from a 'short' integer to a 'long' integer should scalability rear its ugly head.

But wait a second, Perl doesn't make the programmer worry about types - there are no 'floats' or 'double longs' - only scalars.

So, why does one need abstract classes in Perl? Also, are there any CPAN modules that use abstract classes?

UPDATE: Let me add to that last question:
"are there any CPAN modules that use abstract classes internally?" I will continue to look myself.

Thanks,
Jeff

R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
L-L--L-L--L-L--L-L--L-L--L-L--L-L--

In reply to Why do you need abstract classes in Perl? by jeffa

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.