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

Hi all... I have been programming a world clock using Perl/Tk. I am fairly new to Perl (and more so to Tk). I am stuck at one point when I have to click a button and this leads to a subroutine. The first thing in that subroutine is that every existing slave, including the calling button, should be unpacked. I originally made all these widgets available to the whole script, so that I did not need to pass them to the subroutine, but now I am trying to prune my code so that so that variable scope is as limited as possible, and that's why I need to pass these widgets (variables) to a separate subroutine. My code is something like this...
{##### block of code ##### my $button2 = $frame2->Button(-text => "Label", -command => \&subroutine2($label2, $frame2, $button1, $button2))->pack +(); ##### block of code ends here ##### } sub subroutine2{ foreach @_{ packForget $_; } ##### rest of subroutine ##### }
I don't know if this is even a legal call. But this does not seem to work even if I do not pass $button2. Can you please suggest a way out of this? Thanks a lot for your help.

the wyrds - learn, learn, learn.

Replies are listed 'Best First'.
Re: passing a button in its own -command option
by pg (Canon) on Oct 07, 2003 at 04:16 UTC
    make it anonymous like this:
    -command => sub {subroutine2(parameters...);}
      Thank you pg!!! It was very helpful. I am feeling very good about this community. I had been tearing my hair out over this, and knew it had to be a simple thing. And before posting the query, I was doubting whether monks would bother to answer -- but this place is great!! you guys rock!! thank you very much...

      the wyrds - learn, learn, learn.