I ran into this problem the other day, and seek enlightenment:
#!/usr/bin/perl -w use strict; use Tk; my @SLIDERVALUES = (0, 0, 0, 0); my @slider; my $i; sub show_array { print "\@SLIDERVALUES = ($SLIDERVALUES[0], $SLIDERVALUES[1], $SLIDER +VALUES[2], $SLIDERVALUES[3])\n"; } # window setup my $mw = MainWindow->new; foreach my $i (0 .. 3) { $slider[$i] = $mw->Scale (-from => 30, -to => 0, -orient => 'vertical', -variable => \$SLIDERVALUES[$i], )->pack ( -side => 'left', ); } show_array(); # This breaks things! @SLIDERVALUES = (5, 10, 15, 20); show_array(); $SLIDERVALUES[0] = 7; $SLIDERVALUES[1] = 14; $SLIDERVALUES[2] = 21; $SLIDERVALUES[3] = 28; show_array(); MainLoop;
When the line

    @SLIDERVALUES = (5, 10, 15, 20);

is commented out, the command line output is

@SLIDERVALUES = (0, 0, 0, 0) @SLIDERVALUES = (0, 0, 0, 0) @SLIDERVALUES = (7, 14, 21, 28)
and the sliders take up position at 7, 14, 21, and 28, as expected.

If, however, that line is not commented out, we get

@SLIDERVALUES = (0, 0, 0, 0) @SLIDERVALUES = (5, 10, 15, 20) @SLIDERVALUES = (7, 14, 21, 28)
at the command line, and the sliders all stay at 0.

I'd like to understand how the -variable option is being ignored, and I'd also like tips on how I could have further diagnosed this problem.

Many thanks...


In reply to Perl/Tk and the "-variable => \$var" option by Mikster

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.