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

Given the following code:
package SR::Dtest; use strict; use warnings; use Tk; use Tk::widgets; use base qw(Tk::Frame); Construct Tk::Widget 'Dtest'; sub Populate { my($self, $args) = @_; $self->SUPER::Populate($args); $self->ConfigSpecs(-target => ['PASSIVE','target','Target',4]); my $optnum = $self->optionGet('Target','Dtest'); $self->Label(-text=>"var= $optnum")->grid(); $self->pack; }
If done right, I would expect the label to display 'var= 4' (as long as I don't override -target). Instead, I get an error message indicating I'm using an unreferenced variable. What am I doing wrong?

Replies are listed 'Best First'.
Re: How to access options to Tk widgets
by graff (Chancellor) on May 10, 2002 at 02:30 UTC
    Error messages under Tk usually identify a line number and a stack trace -- were you able to determine if the line that triggered the error was in Populate(), and if so, which variable caused the problem?

    It is entirely possible (and very helpful) to do "perl -d" on a script that uses Tk, put a break point in a given callback, and see what the variables really contain when that callback runs.

      Here's the error message: Use of uninitialized value in concatenation (.) or string at SR/Dtest.pm line 16 (#1) That points to this line:
      $self->Label(-text=>"var= $optnum")->pack();
      If I'm understanding correctly, it indicates $optnum is undefined. If that is so, then the problem must lie with this line:
      my $optnum = $self->optionGet('Target','Dtest');
      I'll crack the books and check into the debugger ASAP.
        I'm far from an expert in Tk, but I've just finished a couple of programs in it for an advanced perl class, so here goes my two cents. :)

        Is there a reason you have

        $self->Label(-text=>"var= $optnum")->pack();

        instead of

        $self->Label(-text=>$optnum)->pack();

        If you're actually trying to make your text label $optnum, I believe that you need -text=>$optnum.

        Hope that helps!

        p.s.

        If you find something to debug Tk, let me know would you? I had a HELL of a time debugging my Tk stuff beyond the print a line type of debugging.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to access options to Tk widgets
by Spookymonster (Initiate) on May 10, 2002 at 14:33 UTC
    Found something interesting while testing this morning. Here's a sample wrapper for this widget:
    #! Perl script use strict; use warnings; use Tk; use SR::Dtest; my $main = MainWindow->new; $main->Dtest; #$main->pack(); MainLoop;
    When invoked this way, I get the unreferenced value error. However, if I do the following:
    $main->Dtest(-target=>5);
    ... I get the desired result (a label with the string "var=5"). According to Tk::configSpecs, the format is:
    configSpecs(-option => [where,dbName,dbClass,default])
    Why isn't ConfigSpecs assigning a default value of 4 to -target?
      Got it (I think). Found the clues here. Looks like the options table isn't built until after the Populate() method has completed. This is why I get an error when I don't pass a value for the -target option to new(). This also explains why everything works as expected when I DO pass it a value. So basically, the default value is only good for parms that DON'T get used in the Populate() method itself. For parms that ARE used in Populate(), you'll have to test for the options and set default values. Anyone care to add to this?