Don't subclass modules unless they were designed to be subclassed.

[ In the following text I use "you" a lot. I'd use "one" instead but that is a style that I think many would find a bit strange. In other words, I'm using "you" to refer to you, the reader, not to Ovid in particular. ]

Modules have two public interfaces. The first defines how you use the module and is a contract that, if you follow it, ensures that future versions of the module will still work with your code. This should be documented in the POD for that module.

The second defines how you can customize the module. Most modules don't bother to define this. So I think you would be a fool to subclass most modules. The module designer probably has made no promise that the next release of the module won't switch to using arrays for member data instead of hashes. Or that they won't switch to having the main class of the module only containing class methods including a factory (called "new") that creates objects in a different class. Or any number of other reasonable design refinements for the internals that would break your subclass.

It is perfectly reasonable to have utility functions in an OO module. If these aren't defined in the documentation on how to customize the module then you shouldn't be calling them directly at all because there is no reason to expect that the next version of the module won't change such things. So of course they are inconvenient to call; it is a clue to tell you "Stop doing that!".

If you find a need to subclass such a module, then you really need to first patch the module to support subclassing (which requires getting buy-in from the module maintainer on "freezing" some features of the implementation). Of course, I wouldn't be surprised if you were hoping to use subclassing in no small part because you didn't want to patch the module.

Now, if you are trying to write a module that supports subclassing and you have utility functions that you want to make conveniently available to subclasses... then you should probably just turn them into methods (that ignore their first argument).

        - tye

In reply to (tye)Re: $functions xor $methods by tye
in thread $functions xor $methods by Ovid

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.