Ok, so I did some digging. No MULTICALL. I was off track in my first post.

I confirmed this by adding an extra scope. That would fix the problem I've observed before.
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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.