peter.mao has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm trying to fill in a textvariable in a Tk grid, and it only seems to work if I assign the array elements to scalar variables first.

So it works when I use:
->Label( -textvariable => \$some_scalar )
but not when I do this:
->Label( -textvariable => \$some_array[1] )

In any perl documentation I've seen, they should both be treated as references to scalars, but the text never shows up in the second case. Here's my stripped down code that demonstrates the problem (at least on all the systems I've tried!). It should make a 2x2 grid with "abc" and "def" on the top row, and "1" and "2" on the bottom row. "abc" doesn't show up when I use the array element reference.

What am I overlooking?

Peter

#!/usr/bin/perl use Tk; use strict; use warnings; my (@status_label, @status_value); my ($entry11, $entry12, $entry21, $entry22); #--- my $mainwindow = MainWindow->new(); $mainwindow -> title('testtk'); my $frame_top = $mainwindow ->Frame() ->pack(-side=>'top'); my $label1 = $frame_top ->Label( -textvariable => \$status_label[0] ) ->grid(-row=>1,-column=>1); my $label2 = $frame_top ->Label( -textvariable => \$entry12 ) ->grid(-row=>1,-column=>2); my $value1 = $frame_top ->Label( -textvariable => \$entry21 ) ->grid(-row=>2,-column=>1); my $value2 = $frame_top ->Label( -textvariable => \$entry22 ) ->grid(-row=>2,-column=>2); #--- @status_label = ("abc","def"); @status_value = (1,2); &update_interface(); MainLoop; #--- sub update_interface { $entry11 = $status_label[0]; $entry12 = $status_label[1]; $entry21 = $status_value[0]; $entry22 = $status_value[1]; }

Replies are listed 'Best First'.
Re: why won't Tk textvariable take a reference to an array element?
by serf (Chaplain) on Jan 24, 2006 at 18:52 UTC
    As long as you do the:
    @status_label = ( "abc", "def" );
    before the:
    my $label1 = $frame_top->Label( -textvariable => \$status_label[0] ) ->grid( -row => 1, -column => 1 );
    It will work.

    Otherwise the

    my $label1 = $frame_top->Label( -textvariable => \$status_label[0] )

    Update: (thanks runrig - see Re: why won't Tk textvariable take a reference to an array element?)

    is referring to $status_label[0] which has not been defined yet.

    Make that: "is referring to the original instance of $status_label[0] which is destroyed when you define a new array called @status_label by doing: @status_label = ( "abc", "def" );"

    The magical thing is more the fact that the ones which use a reference to a scalar can be defined before the scalar has been populated with data.

    The references are setting aside the promise that by the time MainLoop comes around to pack the components of the window together there will be some data sitting in the memory locations referred to by \$entry12, \$entry21 & \$entry22 for MainLoop to pick up and use as values.

      Hi Serf,

      yes, that does make it work, but I'm using textvariable so that I can change the text dynamically. Assigning textvariable to a reference to a scalar makes for very messy programming. Is there a way to reference it to an array element that will work?

      I find that if I define @status_label up top, the value of $status_label[0] still doesn't update in the label.

      Perhaps this is a subtlety with MainLoop?

      Peter

Re: why won't Tk textvariable take a reference to an array element?
by runrig (Abbot) on Jan 24, 2006 at 19:21 UTC
    When you assign the array, the old $status_label[0] is gone (the entire array is reassigned), the previous reference to it no longer references the array. An alternative to the previous solution (which is only slightly wrong in its reasoning) is an array slice assignment (which assigns to existing elements):
    @status_label[0,1] = ("abc","def");
    Updated: at the request of serf.
      Thanks, runrig and serf.

      The array slice works well. I was pulling my hair out for days on this one and the ugliness of my hack code was getting to me!

      Peter