Ok, so I did some digging. No MULTICALL. I was off track in my first post.
sub count_up { { . . . } }
$cnt=0; $progress=0 $cnt=0; $progress=20 $cnt=0; $progress=40 $cnt=0; $progress=60
Turns out the variable is always different than the one in the previous call to the function. (As it should be since we keep creating external references to it.)
my $r = \$progress; print("address of \$progress=$r\n");
$cnt=0; $progress=0 address of $progress=SCALAR(0x19fc970) $cnt=0; $progress=20 address of $progress=SCALAR(0x19fe710) $cnt=0; $progress=40 address of $progress=SCALAR(0x1cfdb0c) $cnt=0; $progress=60 address of $progress=SCALAR(0x1d13fc4)
That means something is setting the new variable to the value the old one had. It must be configure.
$pb->configure( -from => 0, -to => 20, -variable => \$progress, ## store the reference. ); $progress = 0;
$cnt=0; $progress=0 $cnt=0; $progress=0 $cnt=0; $progress=0 $cnt=0; $progress=0
I confirmed this by looking at the source. Note $$varref = $oldval;.
sub variable { my $c = shift; my $oldvarref = $c->{'-variable'}; my $oldval = $$oldvarref if $oldvarref; if(@_) { my $varref = shift; if ($oldvarref) { $c->traceVdelete($oldvarref); } $c->{'-variable'} = $varref; $c->traceVariable($varref, 'w', sub { $c->value($_[1]) }); $$varref = $oldval; _layoutRequest($c,2); } $oldval; }
Changing the var that holds the progress shouldn't change the value of the progress bar, and there's code to makes sure of that.
Perhaps you want
use strict; use warnings; use Tk; use Tk::ProgressBar; my $mw = MainWindow->new(-title=>"Bug Demo"); my $progress; my $pb = $mw->ProgressBar( -width => 20, -length => 200, -blocks => 20, -from => 0, -to => 20, -variable => \$progress, ## store the reference. )->pack(); my $button = $mw->Button( -text => 'Press this!', -command => \&count_up, )->pack(); MainLoop; sub count_up { $progress = 0; $mw->update; for (1..20) { select(undef, undef, undef, 0.01); $progress++; $mw->update; } }
In reply to Re^2: Odd lexical variable behavior -- why does this happen?
by ikegami
in thread Odd lexical variable behavior -- why does this happen?
by radiantmatrix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |