Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-25 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found