member callback function

Discussion in 'Perl Misc' started by Shea Martin, Jul 11, 2005.

  1. Shea Martin

    Shea Martin Guest

    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
     
    Shea Martin, Jul 11, 2005
    #1
    1. Advertising

  2. Shea Martin

    Guest

    "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
     
    , Jul 11, 2005
    #2
    1. Advertising

  3. "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::Dispatcher;

    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::Dispatcher->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/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jul 11, 2005
    #3
  4. 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
     
    Brian McCauley, Jul 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prettysmurfed
    Replies:
    6
    Views:
    713
    prettysmurfed
    Jul 22, 2003
  2. Chris Morley

    Member Function Callback

    Chris Morley, Sep 14, 2004, in forum: C++
    Replies:
    2
    Views:
    616
    Uwe Schnitker
    Sep 15, 2004
  3. JDT
    Replies:
    6
    Views:
    568
    James Kanze
    Mar 29, 2007
  4. Ramesh
    Replies:
    11
    Views:
    4,328
    James Kanze
    Dec 27, 2008
  5. Angus
    Replies:
    3
    Views:
    721
    James Kanze
    Mar 2, 2009
Loading...

Share This Page