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
In reply to reference to self function by anjiro
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |