Getting input one character at a time doesn't involve using
select and non-blocking I/O (at least not the easiest way).
The way that the input is processed is set by the user's
terminal. The default setting is known as 'canonical mode',
which processes line-by-line. I also must correct my
original statement, for there is indeed a way with the
standard perl modules to do what you want. For example:
use POSIX qw(:termios_h);
my $termios = new POSIX::Termios;
$termios->setlflag(~ICANON); # Turn off canonical mode
$termios->setcc(VMIN, 1); # Read a min of 1 character
$termios->setcc(VTIME, 0); # No time-out on reads
$termios->setattr(0, TCSANOW); # Apply settings to STDIN
while(read(STDIN, $key, 1)) {
print "Got: $key\r\n";
}
or something similar will work. The flags and function
calls used by the POSIX::Termios module are nearly identical
to the corresponding ones in C.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|