perl-5.38.0
select FILEHANDLE
select

Returns the currently selected filehandle. If FILEHANDLE is supplied, sets the new current default filehandle for output. This has two effects: first, a write, print, or say without a filehandle default to this FILEHANDLE. Second, references to variables related to output will refer to this output channel.

その時点で、選択されていたファイルハンドルを返します。 FILEHANDLE を指定した場合には、その値を出力のデフォルトファイルハンドルに 設定します。 これには、2 つの効果があります: まず、ファイルハンドルを指定しないで write, print, say を 行なった場合のデフォルトが、この FILEHANDLE になります。 もう一つは、出力関連の変数への参照は、この出力チャネルを 参照するようになります。

For example, to set the top-of-form format for more than one output channel, you might do the following:

例えば、複数の出力チャネルに対して、ページ先頭フォーマットを 設定するには:

    select(REPORT1);
    $^ = 'report1_top';
    select(REPORT2);
    $^ = 'report2_top';

FILEHANDLE may be an expression whose value gives the name of the actual filehandle. Thus:

FILEHANDLE は、実際のファイルハンドル名を示す式でもかまいません。 つまり、以下のようなものです:

    my $oldfh = select(STDERR); $| = 1; select($oldfh);

Some programmers may prefer to think of filehandles as objects with methods, preferring to write the last example as:

ファイルハンドルはメソッドを持ったオブジェクトであると考えることを好む プログラマもいるかもしれません; そのような場合のための最後の例は 以下のようなものです:

    STDERR->autoflush(1);

(Prior to Perl version 5.14, you have to use IO::Handle; explicitly first.)

(Perl バージョン 5.14 以前では、まず明示的に use IO::Handle; とする 必要があります。)

Whilst you can use select to temporarily "capture" the output of print like this:

Whilst you can use select to temporarily "capture" the output of print like this: (TBT)

    {
        my $old_handle = select $new_handle;

        # This goes to $new_handle:
        print "ok 1\n";
        ...

        select $old_handle;
    }

you might find it easier to localize the typeglob instead:

you might find it easier to localize the typeglob instead: (TBT)

    {
        local *STDOUT = $new_handle;

        print "ok 1\n";
        ...
    }

The two are not exactly equivalent, but the latter might be clearer and will restore STDOUT if the wrapped code dies. The difference is that in the former, the original STDOUT can still be accessed by explicitly using it in a print statement (as print STDOUT ...), whereas in the latter the meaning of the STDOUT handle itself has temporarily been changed.

The two are not exactly equivalent, but the latter might be clearer and will restore STDOUT if the wrapped code dies. The difference is that in the former, the original STDOUT can still be accessed by explicitly using it in a print statement (as print STDOUT ...), whereas in the latter the meaning of the STDOUT handle itself has temporarily been changed. (TBT)

Portability issues: "select" in perlport.

移植性の問題: "select" in perlport

select RBITS,WBITS,EBITS,TIMEOUT

This calls the select(2) syscall with the bit masks specified, which can be constructed using fileno and vec, along these lines:

これは、select(2) システムコールを、指定したビットマスクで呼び出します; ビットマスクは、filenovec を使って、以下のようにして作成できます:

    my $rin = my $win = my $ein = '';
    vec($rin, fileno(STDIN),  1) = 1;
    vec($win, fileno(STDOUT), 1) = 1;
    $ein = $rin | $win;

If you want to select on many filehandles, you may wish to write a subroutine like this:

複数のファイルハンドルに select を行ないたいのであれば、 以下のようにします:

    sub fhbits {
        my @fhlist = @_;
        my $bits = "";
        for my $fh (@fhlist) {
            vec($bits, fileno($fh), 1) = 1;
        }
        return $bits;
    }
    my $rin = fhbits(\*STDIN, $tty, $mysock);

The usual idiom is:

通常は、

 my ($nfound, $timeleft) =
   select(my $rout = $rin, my $wout = $win, my $eout = $ein,
                                                          $timeout);

or to block until something becomes ready just do this

のように使い、いずれかの準備が整うまでブロックするには、 以下のようにします。

 my $nfound =
   select(my $rout = $rin, my $wout = $win, my $eout = $ein, undef);

Most systems do not bother to return anything useful in $timeleft, so calling select in scalar context just returns $nfound.

ほとんどのシステムではわざわざ意味のある値を $timeleft に返さないので、 select をスカラコンテキストで 呼び出すと、単に $nfound を返します。

Any of the bit masks can also be undef. The timeout, if specified, is in seconds, which may be fractional. Note: not all implementations are capable of returning the $timeleft. If not, they always return $timeleft equal to the supplied $timeout.

どのビットマスクにも undef を設定することができます。 TIMEOUT を指定するときは、秒数で指定し、小数でかまいません。 注: すべての実装で、$timeleft が返せるものではありません。 その場合、$timeleft には、常に指定した $timeout と同じ値が返されます。

You can effect a sleep of 250 milliseconds this way:

250 ミリ秒の sleep と同じ効果が、以下のようにして得られます。

    select(undef, undef, undef, 0.25);

Note that whether select gets restarted after signals (say, SIGALRM) is implementation-dependent. See also perlport for notes on the portability of select.

select がシグナル (例えば、SIGALRM) の 後に再起動するかどうかは実装依存であることに注意してください。 select の移植性に関する 注意については perlport も参照してください。

On error, select behaves just like select(2): it returns -1 and sets $!.

エラー時は、selectselect(2) のように振舞います: -1 を返し、$! をセットします。

On some Unixes, select(2) may report a socket file descriptor as "ready for reading" even when no data is available, and thus any subsequent read would block. This can be avoided if you always use O_NONBLOCK on the socket. See select(2) and fcntl(2) for further details.

Unix の中には、実際に利用可能なデータがないために引き続く read が ブロックされる場合でも、select(2) が、ソケットファイル記述子が 「読み込み準備中」であると報告するものもあります。 これは、ソケットに対して常に O_NONBLOCK フラグを使うことで回避できます。 さらなる詳細については select(2)fcntl(2) を参照してください。

The standard IO::Select module provides a user-friendlier interface to select, mostly because it does all the bit-mask work for you.

標準の IO::Select モジュールは select へのよりユーザーフレンドリーな インターフェースを提供します; 主な理由はビットマスクの仕事を してくれることです。

WARNING: One should not attempt to mix buffered I/O (like read or readline) with select, except as permitted by POSIX, and even then only on POSIX systems. You have to use sysread instead.

警告: バッファ付き I/O (readreadline) と select を 混ぜて使ってはいけません(例外: POSIX で認められている形で使い、 POSIX システムでだけ動かす場合を除きます)。 代わりに sysread を 使わなければなりません。

Portability issues: "select" in perlport.

移植性の問題: "select" in perlport