Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: coderef to an object method

by etcshadow (Priest)
on Jul 21, 2004 at 05:33 UTC ( [id://376166]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://376166]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found