When you call methods, it would not be easy to take a
code reference, as the function that gets called depends
on the class that the object is blessed into.
(A module might even do tricks like blessing objects
to different classes depending on its constructor options
or even rebless it when it changes a state; or it
can change the method dynamically with AUTOLOAD or else.)
Thus, the perl equivalent of a pointer to method
is to simply use a string. There's a less known way
to call a method inderectly via a string, which is like
this:
use Math::BigRat; $m = "new"; $a = Math::BigRat->$m(5); $b = Math::Big
+Rat->$m(8); for $m (qw(badd bsub bmul bdiv)) { $x = $a->copy->$m($b);
+ print "$a $m $b = $x\n" }
The problem with this notation is that you can not
replace the method name with an arbitary braced
expression, so you must use for example
use Math::BigRat; $m = "new"; $a = Math::BigRat->$m(5); $b = Math::Big
+Rat->$m(8); for $m (qw(add sub mul div)) { $x = $a->copy->${\("b".$m)
+}($b); print "$a $m $b = $x\n" }
if you use a more complicated expression than a single variable.
(Doesn't anyone know where this indirect method syntax is
documented in perldoc?)
Update 2:
Ah, it seems that I have misuderstood your question.
If I understand correctly, you need code references
so that you can give it as callbacks.
I still say you should not directly take coderefs
to a method, as that would break the OO approach.
Instead, you need to create anonymous functions that
call the method, like this:
package ParentClass;
# [...]
sub init_server {
my $self = shift;
# [...]
$self->{'server'}->setcallback(
data => sub { $self->gotdata(@_); },
connect => sub { $self->connected(@_); },
disconnect => sub { $self->disconnected(@_),
) || die "Error setting callbacks: $@\n";
# [...]
}
# [...]
Or you can factor the subroutine creations out like this:
sub UNIVERSAL::method {
my($self, $name, @rest) = @_;
sub { $self->$name(@rest, @_); };
}
package ParentClass;
# [...]
sub init_server {
my $self = shift;
# [...]
$self->{'server'}->setcallback(
data => $self->method("gotdata"),
connect => $self->method("connected"),
# [...]
Update 3: it seems that etcshadow has already given
essentially the same answer. |