radiantmatrix has asked for the wisdom of the Perl Monks concerning the following question:
I ran across some interesting behavior with lexically-scoped variables; it first struck me as so odd that I thought I'd discovered a bug in my stack1. However, I couldn't believe that I was doing something so unusual that I was the first to discover such a bug, so I built a smaller test case. What results is not, I think, a bug in the stack (oh, yes it is, sort of: see this reply), but is still behavior I don't entirely understand. Perhaps some more enlightened monk can assist me.
I know, of course, that when I store a reference to a lexically-scoped value, then let the original go out of scope, the reference still exists, so the variable is not entirely destroyed. However, it surprised me to learn that I can enter that scope again, specifically set the value of the variable, and not have it work. Check it out:
#!/usr/bin/perl package main; use strict; use warnings; use Tk; use Tk::ProgressBar; my $mw = MainWindow->new(-title=>"Bug Demo"); my $pb = $mw->ProgressBar( -width => 20, -length => 200, -blocks => 20, )->pack(); my $button = $mw->Button( -text => 'Press this!', -command => \&count_up, )->pack(); MainLoop; sub count_up { $mw->update; my $progress = 0; $pb->configure( -from => 0, -to => 20, -variable => \$progress, ## store the reference. ); my $cnt = 0; print "\$cnt=$cnt; \$progress=$progress\n"; for (1..20) { $progress++; $cnt++ unless ($_ % 2); $mw->update; } $mw->update; }
Note how I specifically state my $progress = 0; in the sub. If I click the button 3 times, thus calling the sub 3 times, I get the following console output:
$cnt=0; $progress=0 $cnt=0; $progress=20 $cnt=0; $progress=40
I don't understand why that's happening -- if I'm setting $progress to zero each time, why does it always increment in that way? I've managed to refactor the production code that has this issue in such a way as to avoid this behavior, so this isn't life-or-death by any stretch. However, it is really bugging me, and I'd appreciate some enlightenment.
Thanks in advance!
-----
1: i.e. in some module I was using, its dependencies, or perl itself (yes, I know how unlikely the last is).
Updates:
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Odd lexical variable behavior -- why does this happen?
by jdporter (Paladin) on Sep 12, 2007 at 22:09 UTC | |
|
Re: Odd lexical variable behavior -- why does this happen?
by ikegami (Patriarch) on Sep 12, 2007 at 20:37 UTC | |
by ikegami (Patriarch) on Sep 12, 2007 at 21:25 UTC | |
by radiantmatrix (Parson) on Sep 13, 2007 at 15:36 UTC | |
|
Re: Odd lexical variable behavior -- why does this happen?
by perrin (Chancellor) on Sep 12, 2007 at 20:25 UTC | |
|
Re: Odd lexical variable behavior -- why does this happen?
by demerphq (Chancellor) on Sep 12, 2007 at 21:28 UTC | |
|
Re: Odd lexical variable behavior -- why does this happen?
by shmem (Chancellor) on Sep 16, 2007 at 11:13 UTC |