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:

<radiant.matrix>
Ramblings and references
The Code that can be seen is not the true Code
I haven't found a problem yet that can't be solved by a well-placed trebuchet

In reply to 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.