in reply to Re^2: problem with TK, tag, passing param with binding, destroying binds
in thread problem with TK, tag, passing param with binding, destroying binds

Why your code sub { selectAccount(+$i) } works?
And why my code sub { &selectAccount(+$i) } did not?
... I have always been using with the ampersand with binds.

Further to tybalt89's reply...
The  & (ampersand) has nothing to do with it. (Likewise the  + (plus) sign in front of the $i; I don't know where that came from.) The trick is to switch from your C-style  for (my $i=1; $i<200; $i++) { ... } loop to a Perl-style  for my $i (1..200) { ... } loop.

In the C-style loop, the variable  $i is defined once and given many values, the last of which is 200 (because that's the value that finally fails the  $i<200 test). All the  sub { selectAccount($i) } and  sub { markAccount($i) } expressions symbolicly reference (if that's the right term) this single  $i variable which has only one final value. At the end of the  InitializeListBox() routine, the referential bindings "close" over the single  $i variable (with its single value) because all symbolic references to the variable are used within code references which are passed to another subroutine, tag(), and must therefore persist. (Again, I may not have quite the right terminology here, but that's the general idea.)

In the Perl-style loop, the  $i variable is aliased to many different values. Each  sub { ... } expression symbolically references a different value via the  $i alias. All these different, individual values are then closed over. I think that's about right.

See closure.

As to your other, Tk-related questions, it's been a while since I've played with Tk very much and someone else could, I'm sure, give you much better answers with much less mental effort.

Update: Made a few minor changes to wording in the interest, I hope, of clarity.


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^4: problem with TK, tag, passing param with binding, destroying binds
by jsteng (Beadle) on Apr 20, 2018 at 05:33 UTC
    I actually never used any perl style loops before other than foreach my $i ( ... );
    and the working script I took that code indeed used foreach statement.

    I got alot of recoding work to do, replacing all C style loops with a Perl style loop. Thanks for clarification.
      ... replacing all C style loops ...

      I would urge you to approach this with caution. C-style loops have their uses, and it is not always advantageous or appropriate (or even possible) to replace such a loop with a Perl-style loop.

      I actually never used any perl style loops before other than foreach my $i ( ... );

      It's important to realize that the keywords for and foreach are exactly equivalent and completely interchangeable in any such statement. The different behaviors of the two different loop styles are entirely determined by whether the loop is written with an init/test/final block (C-style) or a list (Perl-style). Please see For Loops (which discusses C-style loops) and Foreach Loops (which discusses Perl-style loops), both in perlsyn. Again, regardless that the two different loop styles are discussed in sections clearly labeled "For" and "Foreach", respectively, the two keywords are completely interchangeable. (And then, of course, there's the statement modifier form!)


      Give a man a fish:  <%-{-{-{-<