in reply to Garbage-collecting with closures

All right, suppose I pass a closure to a Tk function, as in:
$mw->Button(-text => "text", -command => sub { print "demo closure" }) +;
Will that also cause problems?

Replies are listed 'Best First'.
Re: Re: Garbage-collecting with closures
by mojotoad (Monsignor) on Feb 20, 2003 at 03:37 UTC
    That's not a closure -- it's merely an anonymous sub reference.

    This would be a closure being passed to a Tk function:

    sub make_counter { my $count = shift; sub { ++$count } } $mw->Button(-text => "count", -command => &make_counter(0)});

    That in and of itself shouldn't produce leaks -- the closure will be deconstructed along with the Button deconstruction. I would imagine you need be wary of reference cycles, however, like this (functionally meaningless):

    sub make_ref_reporter { my $thing = shift; sub { ref $thing } } $mw->Button(-text => "refthing", -command => &make_ref_reporter($mw) +);

    Matt