and a classpackage Callback; sub do_callback { my $coderef = shift; $coderef->(@_); } 1;
and a sub-classpackage Parent; use Callback; sub new { bless {}, shift } sub do_something { ... } sub init { my $self = shift; my $coderef = \&do_something; Callback::do_callback($coderef, $self); } 1;
then, in a nearby piece of code..package Child; use base qw(Parent); sub do_something { ... } 1;
my $child = Child->new;
how can I pass values to Callback::do_callback() so that it calls the $child's do_something() method, without Callback knowing in advance that an object is expected?
I know that the ref of do_something() in package Parent will be that of the subroutine defined in that class.. but it seems like there should be a nice OOPy way of calling the correct method from a package external to where that method is defined.
Obviously this is a trivial example; in practice Callback might have to deal with unforeseen packages. Also assume that the guts of Callback can be examined but not modified.
Even if Callback knows to expect an object, calling
$obj->$coderef()
does not do The Right Thing.
If I've made myself clear, is there any way of achieving this functionality?
In reply to Package coderefs by moot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |