in reply to Perl Tk label references

Hi, do you see the problem now?
$ perl -le" $ref = {}; print $ref; $ref = {}; print $ref; $ref = {}; p +rint $ref; " HASH(0x3f9ac4) HASH(0x3f9b94) HASH(0x3f9ba4)

Replies are listed 'Best First'.
Re^2: Perl Tk label references
by thimes (Acolyte) on Nov 10, 2016 at 21:40 UTC

    No, sorry.

      I was able to fix one problem. the Label reference syntax was wrong. The correct way was \$$ref_hash{'VM-1'}. But the clear of the hash when I click on the "clear" button still does not update my Label??

      use Tk; require Tk::Entry; require Tk::Button; my $mw = MainWindow->new(); my %work = ( "VM-1" => "da923", "VM-2" => "dz427"); main_display(\%work); MainLoop; # ----------------------------------------------------- sub main_display { my $ref_hash = shift(@_); my $lab1 = $mw->Label(-textvariable => \$$ref_hash{'VM-1'}, -relief => 'sunken', -width => 10, )->pack(); my $lab2 = $mw->Label(-textvariable => \$$ref_hash{'VM-2'}, -relief => 'sunken', -width => 10, )->pack(); my $but = $mw->Button(-text=>'clear', -command=> [sub{ %{$ref_hash} = (); $mw -> update; }] )->pack(); }

        ++ Good job improving that part on your own...

        Now look at what your original button's sub does: it assigned a new, empty hashref to the $ref_hash variable (as AM pointed out by printing multiple addresses when assigning the variable to multiple empty hashrefs: {}). Your updated one is an improvement, because it's accessing the original memory, rather than creating a new memory space. But you're deleting the keys and their values, rather than keeping the keys and just clearing the values.

        update: strike out something that might not be accurate. But your second code was still an improvement, because you learned you needed to de-reference the hashref, rather than assigning a new empty hashref {}

        You created references to a couple of hash keys when you made the labels. If you later assign the $ref_hash variable to point to a "brand new" empty hash, that won't affect the label text locations. As far as Perl is concerned, those memory locations are still in use and unchanged. You could just change the value of the label keys to "" as shown below.
        use Tk; use strict; use warnings; use Data::Dumper; my $mw = MainWindow->new(); my %work = ( "VM-1" => "da923", "VM-2" => "dz427"); main_display(\%work); MainLoop; # ----------------------------------------------------- sub main_display { my $ref_hash = shift(@_); my $lab1 = $mw->Label(-textvariable => \$ref_hash->{'VM-1'}, -relief => 'sunken', -width => 10, )->pack(); my $lab2 = $mw->Label(-textvariable => \$ref_hash->{'VM-2'}, -relief => 'sunken', -width => 10, )->pack(); my $but = $mw->Button(-text=>'clear', -command=> sub{ $ref_hash->{'VM-1'}=""; $ref_hash->{'VM-2'}=""; $mw -> update; } )->pack(); }

      Ok, try now

      larry HASH(0x3f9ac4)
      curly HASH(0x3f9b94)
      shemp HASH(0x3f9ba4)
      

      $ref is larry , but then its curly, and then its shemp

      changing shemp or curly does not change larry, because larry is not curly or shemp