in reply to Re: Template Method contra passing subroutines to constructor.
in thread Template Method contra passing subroutines to constructor.

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

Replies are listed 'Best First'.
Re: Re: Re: Template Method contra passing subroutines to constructor.
by perrin (Chancellor) on Jan 13, 2004 at 18:54 UTC
    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

        That would work, but by then there's no advantage to sub refs over inheritance. You are basically hard-coding inheritance here.