Cari di Perl 
    Perl User Manual
Daftar Isi
(Sebelumnya) Reposition directory pointerExecute an arbitrary system call (Berikutnya)
Input and output functions

Reset default output or do I/O multiplexing

Daftar Isi

  • select FILEHANDLE

  • select

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

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

    1. select(REPORT1);
    2. $^ = 'report1_top';
    3. select(REPORT2);
    4. $^ = 'report2_top';

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

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

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

    1. use IO::Handle;
    2. STDERR->autoflush(1);

    Portability issues: select in perlport.

  • select RBITS,WBITS,EBITS,TIMEOUT

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

    1. $rin = $win = $ein = '';
    2. vec($rin, fileno(STDIN), 1) = 1;
    3. vec($win, fileno(STDOUT), 1) = 1;
    4. $ein = $rin | $win;

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

    1. sub fhbits {
    2. my @fhlist = @_;
    3. my $bits = "";
    4. for my $fh (@fhlist) {
    5. vec($bits, fileno($fh), 1) = 1;
    6. }
    7. return $bits;
    8. }
    9. $rin = fhbits(*STDIN, *TTY, *MYSOCK);

    The usual idiom is:

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

    or to block until something becomes ready just do this

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

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

    Any of the bit masks can also be undef. The timeout, if specified, isin seconds, which may be fractional. Note: not all implementations arecapable 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:

    1. 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 theportability of select.

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

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

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

    WARNING: One should not attempt to mix buffered I/O (like reador <FH>) with select, except as permitted by POSIX, and eventhen only on POSIX systems. You have to use sysread instead.

    Portability issues: select in perlport.

 
Source : perldoc.perl.org - Official documentation for the Perl programming language
Site maintained by Jon Allen (JJ)     See the project page for more details
Documentation maintained by the Perl 5 Porters
(Sebelumnya) Reposition directory pointerExecute an arbitrary system call (Berikutnya)