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 }
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.