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

I'm having a problem updating my label variables in a ptk app. i have a number of frames and sub frames, depending on a configuration file. After reading the config file, the app draws however many frames as needed. I use a foreach loop to create all my widgets. now I'm having a problem updating these widgets from hash variables pulled from a dbm file. Its supposed to just basically monitor a set of dbm files from another app i have running. below is the basic framework of the code (unneeded sections removed for brevity):

$mw = MainWindow->new; $pane = $mw->Scrolled("Pane", -scrollbars => 'oe', -sticky=>'new')->pack(-side=>top, -expand=>1, -fill=>'both', -anchor=>n); foreach my $host(@host_list) { $frame="frame_$host"; $$frame = $pane->Frame(-borderwidth=>5, -relief=>raised, -label=>"$host")->pack(-side=>top, -expand=>1, -fill=>'x'); foreach my $db(@$host) { $dbframe = "dbframe_$db"; $dbprogressbar = "progressbar_$db"; $$dbframe = $$frame->Frame(-borderwidth=>5, -relief=>groove, -height=>50, -width=>800)->pack(-side=>top, -anchor=>w, -expand=>1, -fill=>'x'); $$dbframe->Label(-text=>"$db",-font=>"courier20bold",-relief=>fl +at)->grid(-column=>0,-row=>0,-sticky=>'nsew'); $$dbframe->Label(-text=>"Phase:",-relief=>flat)->grid(-column=>1 +,-row=>1,-sticky=>'ew'); $$dbframe->Label(-text=>"Mode:",-relief=>flat)->grid(-column=>3, +-row=>0,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{phase},-relief=>sunken)->g +rid(-column=>2,-row=>1,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{mode},-relief=>sunken)->gr +id(-column=>4,-row=>0,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{state},-relief=>flat,-back +ground=>green)->grid(-column=>0,-row=>1,-sticky=>'nsew'); $$dbframe->Label(-text=>'Date:',-relief=>flat)->grid(-column=>3, +-row=>1,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{date},-relief=>sunken)->gr +id(-column=>4,-row=>1,-sticky=>'ew'); $$dbframe->Label(-text=>'Group:',-relief=>flat)->grid(-column=>5 +,-row=>0,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{group},-relief=>sunken)->g +rid(-column=>6,-row=>0,-sticky=>'ew'); $$dbframe->Label(-text=>'Events:',-relief=>flat)->grid(-column=> +5,-row=>1,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{events},-relief=>sunken)-> +grid(-column=>6,-row=>1,-sticky=>'ew'); $$dbframe->Label(-text=>'Errors:',-relief=>flat)->grid(-column=> +7,-row=>1,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{hard},-relief=>sunken)->gr +id(-column=>8,-row=>1,-sticky=>'ew'); $$dbframe->Label(-text=>'StartTime:',-relief=>flat)->grid(-colum +n=>7,-row=>0,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$dbstart,-relief=>sunken)->grid +(-column=>8,-row=>0,-sticky=>'ew'); $$dbframe->Label(-text=>'CycleTime:',-relief=>flat)->grid(-colum +n=>9,-row=>0,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$dbcycle,-relief=>sunken)->grid +(-column=>10,-row=>0,-sticky=>'ew'); $$dbframe->Label(-text=>'EndTime:',-relief=>flat)->grid(-column= +>11,-row=>0,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$dbend,-relief=>sunken)->grid(- +column=>12,-row=>0,-sticky=>'ew'); $$dbframe->Label(-text=>'LastUpdate:',-relief=>flat)->grid(-colu +mn=>11,-row=>1,-sticky=>'ew'); $$dbframe->Label(-textvariable=>\$$db{update},-relief=>sunken)-> +grid(-column=>12,-row=>1,-sticky=>'ew'); } } while(1) { foreach my $host(@host_list) { foreach my $db(@$host) { $datafile="$datadir/$host/$db"; $dbframe = "dbframe_$db"; dbmopen(%DB, "$datafile", undef); %$db = %DB; $mw->update; dbmclose(%DB); } } $mw->after(1000); }

Many Thanks in advance.

Replies are listed 'Best First'.
Re: tk loop updating
by BrowserUk (Patriarch) on Aug 05, 2004 at 07:46 UTC

    There are so many levels of symbolic reference indirection going on in this snippet, that I am surprised that the compiler can keep track of them, let alone you.

    I know I certainly cannot.

    With 90% of the variable names in the program being constructed from contents of other variables. And the contents of those being constructed from others, who's content is read in from a combination of bits from any number (unknowable) of files, your problem could be anywhere.

    A duplication of a word at any point in the hierarchy of files that is used to derive your variable names and you will be overwriting one label with another, or one set of labels with another, or entire frames, one with another.

    There is simply no way for us, or you to know. Without the contents of the files this program is entirely unreadable.

    And as the contents of the files can be changed, you, nor anyone else can never know whether this program will ever be correct.

    If I ever saw this program work. I would shut down the system involved, and make a back-up copy of all the files that contribute to the naming of the variables in it, onto a CD.

    Because running the entire system from read-only media, is the only way that will be able to guarentee that the program will ever run correctly again.

    Oh. And please make a second copy of the CD for me. I'll pay for the shipping. That would be like having a picture of a blue moon, a meteor falling to earth, or finding an Archaeopteryx fossil.

    Good luck! (You may find these 1. 2, 3 references useful).


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
      Just wanted to say thanks for your 'perl' of wisdom. You've put me on to good form, and I appreciate it. I'm very much a newbie when it comed to perl and programming form in general. I'm usually one to say "as long as it works...". When I first wrote this code, I wasn't sure about using variable variables, but my mentor seemed to think it was ok. The thing was, the code worked, i just couldnt get the label variables update working. I've cleaned up alot of the code, and its working nicely. I'll post the reformed code if anyone is interested. Thanks again!
Re: tk loop updating
by quantumsuperposition (Initiate) on Aug 05, 2004 at 08:41 UTC
    Nevermind. I realized my variables were all screwd up, trying to double evaluate and all. I didnt realize you could use hash values to store tk objects. (first tk app, sorry.) sheesh.