Break out of the sub or loop on keypress
Hi, I have a sub that returns a value and inside it runs a loop that displays something on the screen, say a list of numbered items. Now what I want to do is if the item i'm interested appears on the screen, the user can hit any key and the program will exit the sub and return what ever it is supposed to return. Any idea how to accomplish that? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Break out of the sub or loop on keypress
On Sat, 2008-08-09 at 14:42 -0700, Michael Alipio wrote: > Hi, > > I have a sub that returns a value and inside it runs a loop that displays > something on the screen, say a list of numbered items. Now what I want to do > is if the item i'm interested appears on the screen, the user can hit any key > and the program will exit the sub and return what ever it is supposed to > return. > > Any idea how to accomplish that? > > > > Yes. Fork of a child process, use Term::ReadKey to set STDIN to respond immediately on key press. When that happens, the child process signals the parent, which breaks the loop. See: perldoc perlipc perldoc Term::ReadKey -- Just my 0.0002 million dollars worth, Shawn "Where there's duct tape, there's hope." "Perl is the duct tape of the Internet." Hassan Schroeder, Sun's first webmaster -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/
Re: Break out of the sub or loop on keypress
Michael Alipio wrote: > > I have a sub that returns a value and inside it runs a loop that displays > something on the screen, say a list of numbered items. Now what I want to do > is if the item i'm interested appears on the screen, the user can hit any key > and the program will exit the sub and return what ever it is supposed to > return. > > Any idea how to accomplish that? I'm not clear if the "what ever it is supposed to return" is related to the key press or not. If not then perhaps the subroutine should be broken into two. Anyway, it sounds like what you want to do is to break out of a loop when a key is pressed. The program below prints dots until a key is pressed and then says how many have been printed. Does that help? Rob use strict; use warnings; use Term::ReadKey; my $n = 0; while () { print '.'; last if ReadKey(-1); $n++; } print $n; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/