As a general rule, keep your GUI construction code lexically separated from your GUI processing. This will (usually) prevent you from running into these sorts of problems. It will also help in avoiding monolithic GUI scripts where global variables and action-at-a-distance become the bane of your existence: you could, for instance, put your subroutines in modules where they were tested independently and perhaps reused by any number of other scripts.

Both ++AnomalousMonk and ++Marshall have made excellent points[1, 2] regarding the use of references. As a guiding principle, whenever you use the -command option with a function that requires arguments, pass those arguments as references.

In the code below, I've reduced your "simple script" to a very bare-bones version. Notice the anonymous block that lexically isolates %db_data from the subroutines: they have no knowledge of it; can't directly access it; and wouldn't know the difference if you passed \%a_different_set_of_db_data as an argument.

#!/usr/bin/env perl -l use strict; use warnings; use Tk; { my %db_data = qw{1 term1 2 term2 3 term3}; my $mw = MainWindow::->new; $mw->Entry(-textvariable => \$db_data{$_})->pack for sort keys %db +_data; $mw->Button(-text => 'Print', -command => [\&print_vals, \%db_data +])->pack; $mw->Button(-text => 'Load', -command => [\&load_vals, \%db_data +])->pack; } MainLoop; sub print_vals { print "@{$_[0]}{1..3}" } sub load_vals { @{$_[0]}{1..3} = qw{X Y Z} }

I tested by: printing the initial values; changing the values of 1 then 3 then 2 (intentionally out of order) and printing after each individual change; loading new values and printing; and, finally, changing all values first and then printing once. Here's the output:

term1 term2 term3 A term2 term3 A term2 C A B C X Y Z D E F

— Ken


In reply to Re: Updating Tk Entries with Hash by kcott
in thread Updating Tk Entries with Hash by Anonymous Monk

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.