> Hi All,
> 
> I want to write a simple perl script to start an endless loop (for
reading
> data from a periphery device, such as a measurement unit), but I also
want
> to be able to stop it anytime. Right now I use "Ctrl-Alt-Del" to do
it.
> There must be a better way. I has been trying to use open, pipe...not
seem
> to understand how to use them.
> 
> ##### uncompleted code snippet #####
> 
> use strict;
> use warnings;
> use Tk;
> use IO::Handle;
> 
> sub msgbox {
>       my $msg=shift;
>       my $mw = MainWindow->new;
>       $mw->title("Attention!");
>       $mw->Label(-text => $msg,
>                       -width => 50,
>                       -height => 8,
>                       -font => "Arial 24 normal") -> pack;
>       $mw->Button(-text => "OK", -font => "Arial 18 normal", -command
=>
> sub {$mw->destroy})->pack;
>       $mw->focusForce;
>       MainLoop;
>       return 1;
> }
> 
> # pop up a message widget and start a endless do loop at the same
time.
> # msgbox("Press OK to stop the program.");
> # start a do loop, until the OK button is pressed
> # do
> # {
> #     print "\nreading...";
> #     #read_temperature_sensor();
> #     sleep(1);
> #
> # }until (OK button is pressed?)
> 
> Any idea how to do it?
> 
> Thanks for help.
> 
> -Eugene
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



Eugene,

I had a loop that took hours to complete and it ran multiple iterations
over several days. I could have pressed Control-C or Control-Break but
that would have killed it instantly with completing the current
iteration. What I wanted was to complete the current iteration and exit
cleanly.

I solved it with the code below. While your loop is running the system
stores any key presses in a buffer. When your loop gets to the code
below it reads all the characters in the buffer and looks for your
terminate sequence. If it finds it any where in the buffer it exits
cleanly. Currently it is looking for the string OK (case insensitive).
If you want it to be case sensitive use the following instead:

   ($keyboard_buffer_contents =~ /OK/)





use strict;

use Term::ReadKey;

my $keyboard_buffer_contents;

while (1) {    # beginning of your process loop

   # your process

   ReadMode ('cbreak');

   my $char;

   while (defined($char = ReadKey(-1))) {

      $keyboard_buffer_contents .= $char;

   }

   ReadMode ('normal');

   if (

        (defined ($keyboard_buffer_contents)) and
        ($keyboard_buffer_contents =~ /OK/i)

      ) {

      exit;
  }

}   # end of your process loop


Happy Scripting,
keith

-- 

Keith R. Watson                        GTRI/ISD
Systems Support Specialist III         Georgia Tech Research Institute
[EMAIL PROTECTED]           Atlanta, GA  30332-0816
404-894-0836

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to