getc

getc FILEHANDLE
getcThis function returns the next character from the input file
attached to FILEHANDLE. It returns undef at end-of-file or if an I/O error was
encountered. If FILEHANDLE is omitted, the
function reads from STDIN.
This function is somewhat slow, but it’s occasionally useful for single-character input from the keyboard—provided you manage to get your keyboard input unbuffered. This function requests unbuffered input from the standard I/O library. Unfortunately, the standard I/O library is not so standard as to provide a portable way to tell the underlying operating system to supply unbuffered keyboard input to the standard I/O system. To do that, you have to be slightly more clever, and in an operating-system-dependent fashion. Under Unix you might say this:
if ($BSD_STYLE) {
system "stty cbreak </dev/tty >/dev/tty 2>&1";
} else {
system "stty", "–icanon", "eol", "\001";
}
$key = getc;
if ($BSD_STYLE) {
system "stty –cbreak </dev/tty >/dev/tty 2>&1";
} else {
system "stty", "icanon", "eol", "^@"; # ASCII NUL
}
print "\n";This code puts the next character typed on the terminal in the
string $key. If your
stty program has options like cbreak, you’ll need to use the code where
$BSD_STYLE is true. Otherwise, you’ll need to use the code where it is false. Determining ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access