mark4 has asked for the wisdom of the Perl Monks concerning the following question:

I'll just get right to the problem I am having. If you run the code below, I would expect that clincking on "var1 2" button would produce a "6" in the screen (Mainwindow) where -textvariable => \$var13 is displayed. This is not happening.

More curious, restart the progrem, click "CHG var1 3" button, See \$var13 correctly change to a "2" on the screen widget. Then click the "var1 2" See it not work (it should have changed t a "6"). Click "CHG var1 5", \$var15 is not changing.

Exit the program and re-start the program, See that both the "CHG var1 3" and "CHG var1 5" buttons work as I expected (expected being that the values are changed on the screen (manewindow widget))

Also, See that this all works as I would expect if you click the complementry "$var2" buttons. The only difference being one is "@var1", the other is "$var2"

Could someone tell me why clicking on the "var1 2" button is not producing a "6" in the screen?

use Tk; use strict; my $i; my @var1; my $var2; my $status; my $mw = MainWindow->new; #fill @var1 with zeros... for ($i = 0; $i <= 81; $i++) {$var1[$i] = 0;} #Do a similar thing with $var2 $var2 = 0; my $frame_01 = $mw->Frame(-background=>'grey')->pack(-side=>'top', -fi +ll => 'x'); my $frame_12 = $mw->Frame(-background=>'grey')->pack(-side=>'top', -fi +ll => 'x'); my $frame_13 = $mw->Frame(-background=>'grey')->pack(-side=>'top', -fi +ll => 'x'); my $frame_14 = $mw->Frame(-background=>'grey')->pack(-side=>'top', -fi +ll => 'x'); my $frame_15 = $mw->Frame(-background=>'grey')->pack(-side=>'top', -fi +ll => 'x'); $i = 1; $frame_01->Label(-text => "$i:", -width=>2, -relief => + 'flat') ->pack(-side =>'left'); $frame_01->Entry(-textvariable => \$var1[$i++], -width=>2, -relief => + 'sunken') ->pack(-side =>'left'); $frame_01->Entry(-textvariable => \$var1[$i++], -width=>2, -relief => + 'sunken') ->pack(-side =>'left'); $frame_01->Entry(-textvariable => \$var1[$i++], -width=>2, -relief => + 'sunken') ->pack(-side =>'left'); $frame_01->Label(-text => " ", -width=>1, -relief => + 'raised') ->pack(-side =>'left'); $frame_01->Entry(-textvariable => \$var1[$i++], -width=>2, -relief => + 'sunken') ->pack(-side =>'left'); $frame_01->Entry(-textvariable => \$var1[$i++], -width=>2, -relief => + 'sunken') ->pack(-side =>'left'); $frame_01->Entry(-textvariable => \$var1[$i++], -width=>2, -relief => + 'sunken') ->pack(-side =>'left'); $frame_12->Button(-text => "var1 1", -command => sub { change_var +1_1(@var1); }) ->pack(-side => 'left'); $frame_12->Button(-text => "var1 2", -command => sub { @var1 = ch +ange_var1_2(@var1); }) ->pack(-side => 'left'); $frame_12->Button(-text => "CHG var1 3", -command => sub { $var1[3] = + 2;}) ->pack(-side => 'left'); $frame_12->Button(-text => "CHG var1 5", -command => sub { $var1[5] = + 7;}) ->pack(-side => 'left'); $frame_13->Button(-text => "var2 1", -command => sub { change_var +2_1($var2); }) ->pack(-side => 'left'); $frame_13->Button(-text => "var2 2", -command => sub { $var2 = ch +ange_var2_2($var2); }) ->pack(-side => 'left'); $frame_13->Button(-text => "CHG var2", -command => sub { $var2 = 3; +}) ->pack(-side => 'left'); $frame_14->Label(-text => "var1[3] is:", -relief => 'flat') + ->pack(-side =>'left'); $frame_14->Label(-textvariable => \$var1[3], -relief => 'flat') + ->pack(-side =>'left'); $frame_14->Label(-text => ", var1[5] is:", -relief => 'flat') + ->pack(-side =>'left'); $frame_14->Label(-textvariable => \$var1[5], -relief => 'flat') + ->pack(-side =>'left'); $frame_14->Label(-text => ", var2 is:", -relief => 'flat') + ->pack(-side =>'left'); $frame_14->Label(-textvariable => \$var2, -relief => 'flat') + ->pack(-side =>'left'); $frame_15->Label(-text => "Status", -relief => 'flat') + ->pack(-side =>'left'); $frame_15->Label(-textvariable => \$status, -relief => 'flat') + ->pack(-side =>'left'); MainLoop(); sub change_var1_1 { @var1 = change_var1_2(@var1); $status = "change_var1_1 called $var1[3], $var1[5]"; } sub change_var2_1 { $var2 = change_var2_2($var2); $status = "change_var1_1 called $var2"; } sub change_var1_2 { my @lv_1 = @_; $lv_1[3] = 6; #$var1[3] = 6; # this works if you uncomment it $status = "change_var1_2 called $lv_1[3]"; return(@lv_1); } sub change_var2_2 { my $lv_2 = @_[0]; $lv_2 = 6; $status = "change_var2_2 called $lv_2"; return($lv_2); }

Replies are listed 'Best First'.
Re: TK "textvariable" not updating with @var but ok with $var
by Anonymous Monk on Jan 26, 2017 at 23:21 UTC

    Hi,

    Yes it is possible to explain the behavior

    This is forgetting all the textvariables , its making the textvariables forget the array, its disconnecting the array values from the textvariables  @var1 = change_var1_2(@var1);

    If you do that then the textvariables are lost, the references no longer point to the array

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd / ; my @f = 0; my $fo = \$f[0]; dd\@f; $$fo++; ## @f is updated dd\@f; @f=(); ## $fo no longer points to @f, $fo and @f disconnected $$fo++; dd\@f; __END__ [0] [1] []

    Also names, like like variable names, for example @var1/$var1[] ; if you read that out loud it is array variable variable one the @ part tells us its a perl variable of the array variety (array for short), so "var" shouldn't be part of the name

    Similarly 1 shouldn't be part of the name, but thats what you have left @1

    I would call it @chg or whatever "chg" stands for, maybe @change, something with meaning

      Thank you! I will try to leave that line out and debug more. (I don't 'completely' understand yet, But I get the basic of what you are saying.) Again Thanks -Mark
        I also see that my sobroutine change_var1_1 has the line  @var1 = change_var1_2(@var1); in it. As I see from debug, this is also disconnecting the array. Perhaps for my (better) understanding you could sugest a different was to assign the @var1 array without having it get disconnected?
Re: TK "textvariable" not updating with @var but ok with $var
by stevieb (Canon) on Jan 26, 2017 at 23:11 UTC

    Please wrap *all* code within code tags. As you can see, the block looks good, but the snippets within the opening sentences are indecipherable.

    Also, even though this is relatively short, it's best to give your variables reasonable names (especially when asking others to debug your software), so that it's easier on the mind and the eyes to spot issues.

    Without yet reading the code, your subject says something... @var is an array, and $var is a scalar which may be an array reference. Again, haven't read the code, but you may be mixing up your own vars here due to naming issues...

      I have no problem downloading my code above. All the code was wraped in the code tags. Are you still having this problem? (Oh, perhaps you are talking about the code in the text. (That would be "all the code., As you stated. lol :). I can and will fix that. BTW, I am using @var2 and $var1 in the Actual code, So the name space is not the issue.
        It wasn't a question of name space, but an issue of using meaningful variable names, i.e. names that make sense to you and to your reader. The only thing that the name $var tells you is that it is probably a variable, but that was already known.

        If you use a more useful name, such as $counter, $nb_of_customers, $time_left_to_ignition, that helps understanding what's going on in the program.