Hi Necos,

Just a quick suggestion (must run off). What I frequently do with Tk apps (and I write lots of them) is to create an anonymous hash to hold all the widgets that I might need to address elsewhere.. Then I can pass that hash around and avoid globals. For example (psuedo code):

my $w; $w->{main} = MainWindow->new; $w->{other_widgets} = $w->{main}->Label->.... # and then later... &subroutine($w); sub subroutine { my $w = shift; # Do stuff with $w here now like normal $w->{main}->configure( -title => "new title" ); }
My personal rule is that if I get the variable used only once warning then I've done something wrong with my design and implementation and I should rework my solution.

Update: demerphq makes a good point. To add to what is said there, I'd like to comment that there are lots of ways to reference a variable without context;

1 if $a; # for example
Another thing that I've done in the past when stuck with these kinds of warnings is to create a special subroutine just to reference them (assuming their global status):
sub unused { warn "sub unused was used!!!\n"; return; 1 if $a; 1 if $b; }
But I really feel that if you are in this situation you may wish to think carefully if there is a better way to accomplish what you are trying to do.

Again, good luck - {NULE} (/Update).

Final Update: Here is sample code that accomplishes what Necos wanted.

#! /usr/bin/perl -w use strict; use Tk; use Tk::Toplevel; ################ # Main Routine # ################ my $w; $w->{main} = MainWindow->new(-title => 'Tk Hash'); $w->{main}->Button(-text => "Click me to open a new toplevel", -comman +d => [ \&new_tl, $w ])->pack; $w->{main}->Button(-text => "Click me to test for other button", -comm +and => [ \&test_tl, $w ])->pack; MainLoop; exit; ############### # Subroutines # ############### sub new_tl { my $w = shift; if ( not Tk::Exists $w->{toplevel} ) { $w->{toplevel} = $w->{main}->Toplevel( -takefocus => 1, -title + => 'Top Level' ); $w->{testbutton} = $w->{toplevel}->Button(-text => 'Null butto +n'); } else { $w->{toplevel}->deiconify(); $w->{toplevel}->raise(); $w->{toplevel}->focusForce(); } } sub test_tl { my $w = shift; if ( Tk::Exists $w->{testbutton} ) { print "testbutton exists\n"; } else { print "testbutton does not exist\n"; } }

{NULE}
--
http://www.nule.org


In reply to Re: -w, strict, and Tk... by {NULE}
in thread -w, strict, and Tk... by Necos

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.