in reply to Re^2: what is difference between calling the function in Perl/Tk in the following ways
in thread what is difference between calling the function in Perl/Tk in the following ways

It's the circular reference preventing garbage collection that would cause the leak. The same sort of leak that happens when you repeatedly create anonymous recursive subs like:
my $fact; $fact = sub { my $n = shift; return $n if $n <= 1; return $n*fact($n-1); }
Hmm, you're right, the other one would probably leak also if they both contain $button. (update: oops...looking at wrong thread since the OP didn't even have a circular reference in the example).
  • Comment on Re^3: what is difference between calling the function in Perl/Tk in the following ways
  • Select or Download Code

Replies are listed 'Best First'.
Re^4: what is difference between calling the function in Perl/Tk in the following ways
by ikegami (Patriarch) on Apr 16, 2010 at 22:21 UTC

    the other one would probably leak also if they both contain $button

    Exactly.

    my $obj; my $obj = { data => [ \&f, \$obj ] }; # leaks my $obj; my $obj = { data => [ sub { f($obj) } ] }; # leaks my $arg; my $obj = { data => [ \&f, \$arg ] }; # doesn't leak my $obj; my $obj = { data => [ sub { f($arg) } ] }; # doesn't leak