Hi all-
I'm trying to figure out how to flush/clear STDIN for a text-menu based perl script. Below is part of the code for my menu pkg--it displays the menu, gets and handles user selections. handleResponse() calls the callback, which may take some time. BUT-if the user starts typing during the callback's processing, my menu will take it as the next input, and start acting funny. Any ideas on how I can clear STDIN so that I only get what's typed after the handleResponse returns? (Or maybe how I can close STDIN before handleResponse and reopen it after?)
# Name: showLoop
# Desc: MainLoop for displaying text menu and handling input
# In: loopMax - max number of looping
# Out: None
sub showLoop {
my ($self,$loopMax) = (@_);
my $loopNum = 0;
while (1) {
# clear screen
clearScreen();
# display menu
print "\n $self->{'title'}\n\n";
foreach my $item (@{$self->{"itemsRef"}}) {
my $label = $item->getLabel();
print " $label\n";
}
print "\n Select an option or quit [q]: ";
# handle user response
my $resp = $self->getResponse();
if ($resp =~ /^[qQ]$/) {
last;
} else {
$self->handleResponse($resp);
}
$loopNum++;
if ((defined $loopMax) && ($loopNum >= $loopMax)) {
last;
}
# flush stdout and stdin
autoflush STDOUT,1;
# how to flush/empty stdin without blocking?
# continue
print "\n Return continues\n";
<STDIN>;
}
}
print(map(lc(chr),split(6,qw/99672682673683684689632658645641610607/)));
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.