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

This ought to be pretty simple (or at least I thought so). I'm looking at the example in _Perl in a Nutshell_, page 575. It says:
#!/usr/bin/perl -w use Tk; my $mw = MainWindow->new; $mw->Button (-text => "Hello World!", -command => sub {exit})->pack; MainLoop;
And this is fine. The problem is that I seek not to exit the program (the command says 'exit'), but rather to return from 'MainLoop' so I can do something else. It is possible that I could have several instances of code like this, and I'd rather not keep going thru memory with wierd returns from code (shades of 'goto'). If I make the command executed by the button just 'return', I'll just return and await the next event (another button press!). Does Perl/Tk have something that will cause 'MainLoop' to return to its caller. The documentation is a bit vague on this point! Thanks.

Replies are listed 'Best First'.
Re: Perl Tk-How to return from 'MainLoop'
by GrandFather (Saint) on May 04, 2006 at 02:00 UTC

    Destroy the main window:

    use strict; use warnings; use Tk; my $mw = MainWindow->new; $mw->Button (-text => "Hello World!", -command => sub {$mw->destroy()} +)->pack; MainLoop; print "done with the GUI\n";

    Prints:

    done with the GUI

    DWIM is Perl's answer to Gödel
Re: Perl Tk-How to return from 'MainLoop'
by jdtoronto (Prior) on May 04, 2006 at 02:58 UTC
    In Perl Tk the MainLoop is not so much something that is called, but it is the event handler loop that keeps it all going. As someone else pointed out, you can destroy the $mw ( Main window ) object and then continue. But as for a pure return leaving the GUI running? No. The event loop responds to all the GUI events and also permits you to put your own callback's into the queue to be handled by several different stages of the event processing loop. It also has a delay mechanism and a periodical event timer mechanism as well.

    If what you want is to put little dialogs windows in various places and then continue after they are handled, then that is easy. Here is something adapted from some code I am working on right now:

    #!/usr/bin/perl -w use strict; use warnings; use Tk; my $mw = MainWindow->new(); $mw->withdraw(); my $ftp_warn = $mw->messageBox( -title => 'Silly message', -message => "We are displaying a silly message, do you wish to conti +nue?", -type => 'YesNo', -icon => 'question', ); if ( $ftp_warn eq 'No' ) { exit; } else { my $msg2 = $mw->messageBox( -title => 'Really?', -message => "We displayed silly message and you wish to continue?" +, -type => 'OK', -icon => 'question', ); exit; }
    That code will run.

    Now if you were to replace the else clause with a call to a function, then you can execute other code. This is naturally quite trivial, because in a Tk application you will have some sort of user interface that appears in the MinWindow (the $mw that I hid with the $mw->withdraw method call) whioch will direct what is to be done.

    Tk is a very useful GUI tool, there are a number of heavy Tk users here in the Monastery. You will find it explained in the docs or you could look at Mastering Perl/Tk" by Lidie and Walsh published by O'Reilly.

    jdtoronto

Re: Perl Tk-How to return from 'MainLoop'
by strat (Canon) on May 04, 2006 at 07:39 UTC