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.

  • Comment on Template Method contra passing subroutines to constructor.

Replies are listed 'Best First'.
•Re: Template Method contra passing subroutines to constructor.
by merlyn (Sage) on Jan 13, 2004 at 18:18 UTC
    I've used both, at different times, and for different reasons.

    If a collection of parameterizations that would have been passed as coderefs into a constructor deserve "a life of their own", then the override method makes more sense. You create a named class that contains the code to create this newly-abled object.

    So, some objects have instance-specific behavior, while other objects share behavior.

    For an interesting way to eliminate the distinction, see the Self language, or look at Perl workalikes like Class::Prototyped.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Template Method contra passing subroutines to constructor.
by hardburn (Abbot) on Jan 13, 2004 at 19:02 UTC

    I like using the template method for factory operations (i.e., bless a new object that is not necessiarily part of the class you are doing this from). This allows you to avoid hard-coding the class you bless into. Example (admittedly arbitrary):

    package Foo; sub FACTORY_CLASS () { 'Bar' } sub make_new_bar { my $self = shift; my @params = @_; bless \@params, $self->FACTORY_CLASS(); }

    This allows a subclass to easily change the class of the object without having the override the entire factory method, which could be quite complex and would lead to a lot of code duplication.

    It's important that FACTORY_CLASS is called in $obj->method() form, and not plain method() form. Otherwise, subclasses won't be able to override it, which is the whole point.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: Template Method contra passing subroutines to constructor.
by perrin (Chancellor) on Jan 13, 2004 at 18:14 UTC
    Passing a sub ref feels very "one-off" to me. What happens if you want to use that constructor in multiple places? I like the subclass approach better.

      Nothing stopping you from doing that:

      my $sub = sub { . . . }; my $obj1 = Foo->new( $sub ); my $obj2 = Foo->new( $sub );

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        I meant two different modules that want to use the same sub ref.