Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > member callback function

Reply
Thread Tools

member callback function

 
 
Shea Martin
Guest
Posts: n/a
 
      07-11-2005
How to I pass a member function as a callback.

For a normal sub I would do this: \&myFunc,

but what do I do for a member func?
I tried this, but it doesn't work: \&$myClassInstance->myFunc()
I also tried a number of other things.

~S


 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      07-11-2005
"Shea Martin" <> wrote:
> How to I pass a member function as a callback.
>
> For a normal sub I would do this: \&myFunc,
>
> but what do I do for a member func?
> I tried this, but it doesn't work: \&$myClassInstance->myFunc()
> I also tried a number of other things.
>
> ~S


sub { $myClassInstance->myFunc(@_) }

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-11-2005
"Shea Martin" <> wrote in news:RQvAe.59661
$:

> How to I pass a member function as a callback.


Instance methods do not exist independently of a specific instance of
the class. Just as you cannot cut the arm of a master knitter and send
it to another location, and expect it to work, you cannot expect an
instance method to do anything useful without the rest of the object.

> For a normal sub I would do this: \&myFunc,>
> but what do I do for a member func?
> I tried this, but it doesn't work: \&$myClassInstance->myFunc()
> I also tried a number of other things.


You'll need to use a closure. The code below is untested (as you have
not provided any code -- see the recommendations in the posting
guidelines for this group on how you can maximize both the quantity and
quality of the help you receive by putting some effort into your post).

#!/usr/bin/perl

use strict;
use warnings;

package My::Handler;

sub new {
my $class = shift;
bless { }, $class;
}

sub handle {
my $self = shift;
print "Go away, I am handling this!\n";
}

package My:ispatcher;

sub new {
my $class = shift;
bless { }, $class;
}

sub register_callback {
my $self = shift;
$self->{callback} = shift;
}

sub generate {
my $self = shift;
$self->{callback}->();
}

package main;

my $dispatcher = My:ispatcher->new;
my $handler = My::Handler->new;
$dispatcher->register_callback( sub { $handler->handle } );

$dispatcher->generate;

__END__


--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      07-13-2005
Shea Martin wrote:

> How to I pass a member function as a callback.


> I also tried a number of other things.


But, oddly, not the FAQ...

perldoc -q method
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to deal with calling a member function from a static callback Ctype function Angus C++ 3 03-02-2009 12:43 AM
Assigning a member function to signal callback function pointer Ramesh C++ 11 12-27-2008 09:36 AM
can a class member function be used as a callback function? JDT C++ 6 03-29-2007 12:45 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM
Howto: Class member function as callback function for dialog box prettysmurfed C++ 6 07-22-2003 06:17 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57