in reply to Re^6: 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

We did the testing with perl 5.6 several years ago. Another danger of the non-list approach is that the function sees the lexical context of its definition and can accidentaly use something from there, which might also leak.

As of now, perl 5.10.0, Tk 804.028, even this code leaks:

perl -e 'use Tk;use Tk::Button;my $top = new MainWindow;for (1..100) { for (1..1000) { $top->Button(-text=>"foo")->destroy; } sleep 1; } '
  • Comment on Re^7: what is difference between calling the function in Perl/Tk in the following ways
  • Download Code

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

    There's no function in there, so it supports what I guessed: It's got nothing to do with closures.

    Another danger of the non-list approach is that the function sees the lexical context of its definition and can accidentally use something from there

    No, they don't accidentally capture anything. Closures only capture the variables they need.

    The following shows that $x wasn't captured even though it was visible to the capture.

    use strict; use warnings; printf "x=%s\n", do { my $x = 2; sub { $x } }->() // 'undef'; printf "x=%s\n", do { my $x = 2; sub { eval '$x' } }->() // 'undef';
    x=2 Variable "$x" is not available at (eval 1) line 2. x=undef

    The warning is new, but the behaviour is ancient.

      I perhaps do not use English conjunctions (or other words) in the right way, for which I apologize. But by accidental leaking I meant an error made by the programmer (like missing "my" inside of the annonymous sub), not by perl. And the example of the behaviour of Button was given to show why I am no longer able to reproduce the tests we did.
Re^8: what is difference between calling the function in Perl/Tk in the following ways
by lamprecht (Friar) on Apr 18, 2010 at 23:15 UTC
    Hi,

    maybe you could check this using the latest devel release Tk-804.028_502. Your code should not leak - and I do not see it leaking using 804.028_501.

    Cheers, Christoph