in reply to Template Method contra passing subroutines to constructor.

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.
  • Comment on Re: Template Method contra passing subroutines to constructor.

Replies are listed 'Best First'.
Re: Re: Template Method contra passing subroutines to constructor.
by hardburn (Abbot) on Jan 13, 2004 at 18:47 UTC

    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.

        Hmm, in that case you might be constrained by your design, but it is still possible like this:

        package Foo; sub filter { . . . } package Bar; my $obj = Baz->new( \&Foo::filter );

        Admittedly, this could get hackish fast.

        ----
        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