anjiro has asked for the wisdom of the Perl Monks concerning the following question:
The idea here is that I'll have a variety of drop-in functions, and it'd be nice to be able to do something like:package Foo; sub new { my($pkg, %args) = @_; my $self = { method => 'correlation', %args }; $self->{funcs}{correlation} = \&$self->correlation; return bless $self, $pkg; } sub correlation { print "This is the correlation function!"; }
and just have a check in the Foo module:my $x = new Foo(method => 'biscuit');
However, trying this in various ways gives me various error messages:if exists $self->{funcs}{$self->{method}} { $self->{funcs}{$self->{method}}(params); } else { die "Sorry, the method $self->{method} isn't valid!"; }
So, any suggestions? I dug around for quite a while but failed to find any information on this. Help would be most appreciated (as would suggestions on how to do this other ways)!#This says "Not a CODE referece at Foo.pm line..." $self->{funcs}{correlation} = \&$self->correlation; #This says "Can't call method 'correlation' on unblessed # reference at Foo.pm line..." $self->{funcs}{correlation} = \&{$self->correlation}; #And this says "Scalar found where operator expected at # Foo.pm line... $self->{funcs}{correlation} = \&($self->correlation);
Thanks!
dan
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: reference to self function
by chromatic (Archbishop) on Jun 25, 2003 at 06:59 UTC | |
by anjiro (Beadle) on Jun 25, 2003 at 07:12 UTC | |
|
Re: reference to self function
by pernod (Chaplain) on Jun 25, 2003 at 07:41 UTC | |
|
Re: reference to self function
by ihb (Deacon) on Jun 25, 2003 at 12:49 UTC | |
|
Re: reference to self function
by sgifford (Prior) on Jun 25, 2003 at 06:43 UTC | |
by anjiro (Beadle) on Jun 25, 2003 at 07:05 UTC | |
by sgifford (Prior) on Jun 25, 2003 at 07:12 UTC | |
by anjiro (Beadle) on Jun 25, 2003 at 07:23 UTC |