I've not done any extensive research on the matter, but my impression is that subclassing - in particular, subclassing another author's classes - seems to be done less commonly in Perl than in some other languages, such as Java. (The exceptions being the frequent subclassing of Moose::Object, Mouse::Object and Moo::Object, where the subclassing is not done deliberately, but incidentally by the OO framework in use; and subclassing of Exporter where subclassing is part of its older flawed design, and still used for back-compat with pre-5.57 versions.)

I mostly put this down to the lack of separation between public, protected and private methods in Perl. In many programming languages, methods are divided into:

public methods
The consumer API - i.e. for external code to use.
protected methods
The development API - i.e. for subclasses to use.
private methods
The internal API - i.e. only used within the class itself.

In Perl there's the underscore convention, but that confuses several different things. (I'll give Moose examples here, not because Moose is especially bad at this, but because I'm familiar with much of its internals.)

So if I want to write a subclass of something I've found on CPAN, I don't know what methods are safe to override. I don't know which methods are stable to stand on, and which methods might be pulled out from under my feet with the next release.

If I see an underscore method, is it a part of the public API which the author thought esoteric; is it a method intended for subclasses to override; or is it truly private? I could consult the documentation, but developers rarely document underscore methods. I could consult the source code, but if the method is commented at all, it's probably an explanation of what the method does or how it does it, and not a note about the method's privacy.

So I forget subclassing, and write a wrapper instead. CPAN is full of wrappers for this and that.

I think the solution is perhaps a two-level system of documentation, where we encourage developers to document protected methods, and make them as stable as the public methods are between releases. Pod::Weaver-like tools might help, allowing two sets of class documentation (the end-user API docs, and the developer API docs) to be generated from the same input pod.

Alternatively, adding clear comments to hooks for subclasses in the source code is a good start. I think I've done a fairly good job in MooX::late.

Getting back to the original question, I'd probably write a MY::FWCollection class, offering add, remove, search, etc methods. I might even make it a subclass of Set::Equivalence. :-)

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

In reply to Re^4: OO search in objects by tobyink
in thread OO search in objects by Hossein

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.