in reply to Passing a reference to a subroutine in a constructor

I think this is what you want(untested)
my $collection = Collection->new({ 'sorter' => \&Sort::mysorter }); ... sub new { my ($self, $options) = @_; . . . $self->{sorter} = $options->{sorter}; }
You actually pass the CODEREF into the constuctor, instead of using a soft reference.

Replies are listed 'Best First'.
Re: Re: Passing a reference to a subroutine in a constructor
by DrSax (Sexton) on Aug 22, 2003 at 18:48 UTC
    I get the same result when I do that. Is the execution of the sort method itself?
    $self->{sorter}($keys);
    Dr. Sax
      $self->{sorter}->($keys);

      Note the squigglies when de-referencing $self and the round parens when de-referencing the sort routine coderef.

      ------
      We are the carpenters and bricklayers of the Information Age.

      The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

        Rob,

        It's always a pleasure to see a guy whose sig line is longer than his response... ;-)

        Brian (a.k.a. DrSax)