in reply to coderef to an object method

Well, first of all... when you do
data => \&gotodata,
you're doing essentially the same thing as:
data => sub { gotodata(@_) },
(they aren't the same thing, exactly, but they're functionally equivalent for your purposes, one is a named referrence and the other is an anonomous referrence to essentially the same thing). I bother to point that out because then it's easier to see the transition to what you should be doing for method calls, which is this:
data => sub { $self->gotodata(@_) },
That is: creating an anonomous subroutine reference for a closure around the method call.
------------ :Wq Not an editor command: Wq

Replies are listed 'Best First'.
Re^2: coderef to an object method
by rinceWind (Monsignor) on Jul 21, 2004 at 09:48 UTC
    data => sub { $self->gotodata(@_) },
    There is a gotcha in this, in that you have just made your anonymous sub into a closure. Note that $self is in the pad space of the surrounding code, and its value is the one object that is around when the line is executed.

    I have seen memory leaks, where the coderef gets stored into a member of $self's hash (or some structure deeper under $self), leading to a subtle circular reference.

    --
    I'm Not Just Another Perl Hacker