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.


In reply to Re: coderef to an object method by ambrus
in thread coderef to an object method by genecutl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.