in reply to Re: Re: Passing a reference to a subroutine in a constructor
in thread Passing a reference to a subroutine in a constructor

here it is in separate files works fine:

[bobn@trc2:/home/bobn/misc]# cat subref.pl #!/usr/bin/perl -w use lib q/./; use Collection; use Sort; my $collection = Collection->new({ 'sorter' => \&Sort::mysorter }); $collection->doit('here is something'); [bobn@trc2:/home/bobn/misc]# cat Collection.pm package Collection; my $self = {}; sub new { my ($class, $options) = @_; $self->{sorter} = $options->{sorter}; bless $self, $class; } sub doit { my ($self, $stuffToSort ) = @_; print "in doit: $stuffToSort\n"; $self->{sorter}->($stuffToSort); print "in doit: $stuffToSort\n"; } 1; [bobn@trc2:/home/bobn/misc]# cat Sort.pm package Sort; sub mysorter { print "in mysorter, @_\n"; } 1; [bobn@trc2:/home/bobn/misc]# ./subref.pl in doit: here is something in mysorter, here is something in doit: here is something

All code given here is UNTESTED unless otherwise stated.

--Bob Niederman, http://bob-n.com