perl-5.38.0
getc FILEHANDLE
getc

Returns the next character from the input file attached to FILEHANDLE, or the undefined value at end of file or if there was an error (in the latter case $! is set). If FILEHANDLE is omitted, reads from STDIN. This is not particularly efficient. However, it cannot be used by itself to fetch single characters without waiting for the user to hit enter. For that, try something more like:

FILEHANDLE につながれている入力ファイルから、次の一文字を返します; ファイルの最後、またはエラーが発生した場合は、未定義値を返します (後者の場合は $! がセットされます)。 FILEHANDLE が省略された場合には、STDIN から読み込みを行ないます。 これは特に効率的ではありません。 しかし、これはユーザーがリターンキーを押すのを待つことなく 一文字を読み込む用途には使えません。 そのような場合には、以下のようなものを試して見てください:

    if ($BSD_STYLE) {
        system "stty cbreak </dev/tty >/dev/tty 2>&1";
    }
    else {
        system "stty", '-icanon', 'eol', "\001";
    }

    my $key = getc(STDIN);

    if ($BSD_STYLE) {
        system "stty -cbreak </dev/tty >/dev/tty 2>&1";
    }
    else {
        system 'stty', 'icanon', 'eol', '^@'; # ASCII NUL
    }
    print "\n";

Determination of whether $BSD_STYLE should be set is left as an exercise to the reader.

$BSD_STYLE をセットするべきかどうかを決定する方法については 読者への宿題として残しておきます。

The POSIX::getattr function can do this more portably on systems purporting POSIX compliance. See also the Term::ReadKey module on CPAN.

POSIX::getattr 関数は POSIX 準拠を主張するシステムで これをより移植性のある形で行います。 CPAN にある Term::ReadKey モジュールも 参照してください。