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

I am a bioinformatics student at the federal university of rio de janeiro - Brazil. I have your book on Perl/TK and Im a perl lover. In one of my projects, I constructed a GUI with perl/tk, and this gui contains a shut-down button that gracefully ends the aplication and sends a signal to the server. Unfortunately, some people press the X button for closing the application located in the upper right hand corner (Windows XP). When this happens, the aplication is closed without sending the signal to the server. How can I over ride this function to gracefully end my soft even if the user closes it with the X button. I have alreadu tried to capture some signals with the %SIG, but it doesnt work.

Replies are listed 'Best First'.
Re: Ending an application gracefully
by PodMaster (Abbot) on Jul 23, 2003 at 04:10 UTC
    I run the following code, and when I click the little X, I get "the main loop has ended normally at - line 4.".
    use Tk; tkinit; MainLoop; die "the main loop has ended normally"; __END__
    You can run whatever code you want after MainLoop returns (the user exits the gui).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Ending an application gracefully
by chromatic (Archbishop) on Jul 23, 2003 at 03:10 UTC

    I think you can register a callback for the WM_DELETE_WINDOW property that shuts down gracefully. I don't have my example code with me, but it should be in the Perl/Tk book. Something like:

    $toplevel->protocol( WM_DELETE_WINDOW, \&end_gracefully );
Re: Ending an application gracefully
by inelukii (Sexton) on Jul 23, 2003 at 13:50 UTC
    You are looking for

    $main_window->OnDestroy( sub { ExitProgram() } );

    You can register whatever exit function you need in the OnDestroy call. OnDestroy will be called when you end your application.
Re: Ending an application gracefully
by waswas-fng (Curate) on Jul 23, 2003 at 02:36 UTC
    Have you looked at the END Block?

    -Waswas