Returns the currently selected filehandle. Sets the current default
filehandle for output, if FILEHANDLE is supplied. This has two
effects: first, a write
or a print
without a filehandle will
default to this FILEHANDLE. Second, references to variables related to
output will refer to this output channel. For example, if you have to
set the top of form format for more than one output channel, you might
do the following:
FILEHANDLE may be an expression whose value gives the name of the actual filehandle. Thus:
Some programmers may prefer to think of filehandles as objects with methods, preferring to write the last example as:
- use IO::Handle;
- STDERR->autoflush(1);
This calls the select(2) system call with the bit masks specified, which
can be constructed using fileno
and vec
, along these lines:
If you want to select on many filehandles you might wish to write a subroutine:
The usual idiom is:
- ($nfound,$timeleft) =
- select($rout=$rin, $wout=$win, $eout=$ein, $timeout);
or to block until something becomes ready just do this
Most systems do not bother to return anything useful in $timeleft, so calling select() in scalar context just returns $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.
You can effect a sleep of 250 milliseconds this way:
Note that whether select
gets restarted after signals (say, SIGALRM)
is implementation-dependent. See also perlport for notes on the
portability of select
.
On error, select
behaves like the select(2) system call : it returns
-1 and sets $!
.
Note: on some Unixes, the select(2) system call may report a socket file descriptor as "ready for reading", when actually no data is available, thus a subsequent read blocks. It can be avoided using always the O_NONBLOCK flag on the socket. See select(2) and fcntl(2) for further details.
WARNING: One should not attempt to mix buffered I/O (like read
or <FH>) with select
, except as permitted by POSIX, and even
then only on POSIX systems. You have to use sysread
instead.