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

At a certain part of my program, I call an error_check function to see if any errors have occured during some events. An example would be as follows:
@reducedProtein = initializeStructure ##some operations(\@residueArr +ay,\@secondaryStructureArray); if ($ERRORCHECK ==1) #if global errorcheck is 1 {$response = confirmExit(@ERRORTYPE);} #call error function if($response==1){ simulate(@reducedProtein);} #if not, go to calculations etc
Now the confirmExit is a function that calls a perlTK widget to create a window. It has two buttons. One is an exit button and exits the program if it is pressed. Other is an ignore button that returns a $response telling that the program can go on. My question is, how can I cause the program to stop until the user presses one of the buttons? I want it so that the program has to wait for the return value from the confirmExit to continue but I know perlTK works in a strange way. I think that it will just create the window with the buttons and return an empty value without waiting for the user to press a button. The skeleton of the confirmExit function is :
sub confirmExit { *create a window* my @ERRORTYPE=@_; *write something on the window according to errortype values* *create button exit, which exits the program when pressed* *create button ignore, which sets the value of $response to 0 if pres +sed* return $reponse;

Replies are listed 'Best First'.
Re: Stopping the Program
by dirving (Friar) on Jun 21, 2008 at 01:17 UTC

    What you want is a modal dialog. In Tk, you can achieve this using Tk::Dialog (text and buttons only) or Tk::DialogBox (allows you to add any widgets you want).

    -- David Irving
Re: Stopping the Program
by pc88mxer (Vicar) on Jun 21, 2008 at 01:09 UTC
    You can execute the dialog in another process and just wait for that process to finish:
    sub check_errors { return unless ($ERRORCHECK == 1); my $st = system("ask-yes-no", "There were errors. Do you want to con +tinue?") >> 8; if ($st != 0) { # exit the program } }
    And in your program:
    ...do stuff... check_errors(); ...do more stuff... check_errors(); ... etc.

    Update: See this article: CLI Magic: Creating basic front ends with dialog and Xdialog for more info on this approach. You may not even have to write the dialog application.

Re: Stopping the Program
by zentara (Cardinal) on Jun 21, 2008 at 11:33 UTC
    This ought to show you a way.
    #!/usr/bin/perl use warnings; use strict; use Tk; require Tk::Dialog; my $result = 'Starting'; my $we_top = new MainWindow; my $message = "Starting Ok ?\n"; my $count = 1; $we_top->Label ( -text => $message)->pack(); my $count_lab = $we_top->Label ( -textvariable => \$count)->pack(); my $okay_button = $we_top->Button( -text => 'Get Response', -command => [\&get_response, $result] )->pack(); my $exit_button = $we_top->Button( -text => 'Exit', -command => sub {exit} )->pack(); $we_top->MainLoop; ########################################## sub analyze_response { my $answer = shift; if( $answer eq 'Yes' ){ $result = "Continue at count $count ?"; } if( $answer eq 'No' ){ $result = "Rerun ?"; } if( $answer eq 'Cancel' ){ $result = "Cancelled ?"; } } ########################################### sub get_response { my $answer = do_dialog(); $count++; print "$answer\n"; &analyze_response( $answer ); } sub do_dialog { my $dlg = $we_top->Dialog( -title=>"Here is a question for you.", -buttons => ["Cancel", "No", "Yes"], -default_button => "No", -text => $result, -font => "Helvetica" ); my $return = $dlg->Show(); return $return; }

    I'm not really a human, but I play one on earth CandyGram for Mongo