in reply to cleaning up an indirect
I have some code I'll be posting soon that illustrates this better, but try using the 'can' method. The advantage to this approach is it even passes 'use strict;'
sub foo_set { my ($self, $code) = @_; my $code_ref = $self->can($code); if (defined($code_ref)) { $self->{'foo_code_ref'} = $code_ref; } else { die "Unable to handle '$code'"; } } sub foo { my ($self, $arg) = _; my $code_ref = $self->{'foo_code_ref'}; $self->code_ref($arg); }
The 'can' method determines if the class can use that method and returns a code reference. You can then just call the code reference on the object. And like I said, it will pass 'use strict'. With error checking and Carp::croak, you can make it even telling you where in the calling code you made a type.
This works in at least perl 5.005_03 and I believe also in perl 5.004. I don't know about older version of perl.
This takes advantage of the 'can' method in the UNIVERSAL package which every package inherits from. If you override the 'can' function in your inheritance tree, this won't work. But that should rarely happen.
|
|---|