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

I have a problem with using a hash reference and updating a Label in PerlTk. It has to be something simple I am missing so I need some more eyes on the problem. This is a test program of a much larger one. Any help would be great! </p?

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(); }

Replies are listed 'Best First'.
Re: Perl Tk label references
by Anonymous Monk on Nov 10, 2016 at 21:29 UTC
    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)

      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(); }

        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