There are two main methods of parametrising objects with a subroutine: you can pass the sub as a parameter to the constructor or you can code a stub method in the class and then override that method in subclass (that is the classic Template Method design pattern).

I don't want to begin some heated debate but I allways find the first method much easier to use. Am I representative in this case?

I think the Template Method is something that got to Perl from languages that can't so easilly pass subroutines around. People just got used to this. For example it is the traditional way of designing parsers.

Here are some links on that subject: Template Method (general description), Perl Design Patterns: Template Method, perl.com article on Perl Design Patterns

Update: After reading merlyns response I see that it really is about class parametrisation (the Template Method) versus object parametrisation (passing subroutines to constructor). And I must admit that both have their uses.


In reply to Template Method contra passing subroutines to constructor. by zby

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.