The problem is with the closure (the anonymous
sub{...}</> that -command is) and the scope of $t. Put a <c>warn $t in the sub{} to see what it's doing -- you'll see that it's always 3 which is the value of t after the
foreach my $f (@foo) ... Here are two different approaches to solving it:
Here, you can use a temp value to hold the current value of $t. Since $i is scoped inside the for loop, it is different for each closure that is made.
my $i=$t;
$bill_table->Button(-text=>'Submit',
-command=>sub{if ($checkboxvalue[$i] ==1){prin
+t $f}},
)->pack;
Alternatively, use the form of
Tk::callback that lets you provide arguments to a sub. Here our anonymous sub takes two args -- a reference to a scalar which is the checkbox value, and a scalar which is the text of the checkbox.
$bill_table->Button(-text=>'Submit',
-command=>[
sub{
my $valref = shift;
my $valname=shift;
if ($$valref ==1){print $valname}
},
\$checkboxvalue[$t],
$f,
],
)->pack;
Now, there's the issue of your
Use of uninitialized value in numeric eq (==) .. That is because the checkbox values are either 1 or undef, so when you check
<thecheckboxvalue>==1 it'll do either
1==1 (ok) or
undef==1 (warning).
To solve, simply do one of these (probably the first? not sure that being exactly 1 is important, as long as it's true:
if( $foo ){ print $f }
if( defined $foo ){ print $f }
if( $foo && $foo==1){ print $f }
if( defined $foo && $foo==1){ print $f }