Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: coderef to an object method

by ambrus (Abbot)
on Jul 21, 2004 at 09:04 UTC ( [id://376182]=note: print w/replies, xml ) Need Help??


in reply to coderef to an object method

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.

Replies are listed 'Best First'.
Re^2: coderef to an object method
by chromatic (Archbishop) on Jul 21, 2004 at 17:00 UTC
    When you call methods, it would not be easy to take a code reference

    Sure it is, if the class writer has done his job correctly. Just call can.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://376182]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found