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

Hello monks,

I'm trying to close a local window and then run a sub-routine using the following snippet:

-command => sub {[$message => 'destroy', \&command]
The $message box closes but the command routine never runs.
Any ideas?

Replies are listed 'Best First'.
Re: closing window and running sub-routine
by ptkdb (Monk) on Oct 27, 2003 at 20:34 UTC
    The pertinant man page is actually Tk::Wm which tells you how to communicate with the window manager.

    Example: (Functional for both XWindows and MS-Windows)

    use Tk ; my($mw) = new MainWindow ; my($closeSub) = sub { print "closing\n" ; $mw->destroy ; } ; $mw->protocol('WM_DELETE_WINDOW', $closeSub) ; my($button) = $mw->Button(-command => $closeSub, -text => "Close")->pa +ck ; MainLoop ;
    This should also work for 'Toplevel' window created with $mw->Toplevel ; $topLevel->protocol(...) ;
Re: closing window and running sub-routine
by kvale (Monsignor) on Oct 27, 2003 at 20:16 UTC
    Well, the first problem is that you are missing a final brace on the anonymous sub.

    I'd guess that you are using Perl/Tk? In that case, I'd use a callback of the form

    my($dismiss) = $root->Button ( -text => 'dismiss window', -command => sub{ $message->destroy; command();} );

    -Mark

Re: closing window and running sub-routine
by Anonymous Monk on Oct 27, 2003 at 20:11 UTC
    I think you want to recheck the Tk::callbacks manpage:
    -command => sub{ ... } -command => [ \&subname, args ... ]