IO::Select(3perl) | Perl Programmers Reference Guide | IO::Select(3perl) |
IO::Select - OO interface to the select system call
use IO::Select; $s = IO::Select->new(); $s->add(\*STDIN); $s->add($some_handle); @ready = $s->can_read($timeout); @ready = IO::Select->new(@handles)->can_read(0);
The "IO::Select" package implements an object approach to the system "select" function call. It allows the user to see what IO handles, see IO::Handle, are ready for reading, writing or have an exception pending.
Each handle can be an "IO::Handle" object, an integer or an array reference where the first element is an "IO::Handle" or an integer.
If at least one handle is ready for the specified kind of operation, the result will be an array of 3 elements, each a reference to an array which will hold the handles that are ready for reading, writing and have exceptions respectively. Upon timeout, an empty list is returned, with $! unchanged. Upon error, an empty list is returned, with $! set to indicate the error. To distinguish between timeout and error, set $! to zero before calling this method, and check it after an empty list is returned.
Here is a short example which shows how "IO::Select" could be used to write a server which communicates with several sockets while also listening for more connections on a listen socket
use IO::Select; use IO::Socket; $lsn = IO::Socket::INET->new(Listen => 1, LocalPort => 8080); $sel = IO::Select->new( $lsn ); while(@ready = $sel->can_read) { foreach $fh (@ready) { if($fh == $lsn) { # Create a new socket $new = $lsn->accept; $sel->add($new); } else { # Process socket # Maybe we have finished with the socket $sel->remove($fh); $fh->close; } } }
Graham Barr. Currently maintained by the Perl Porters. Please report all bugs at <https://github.com/Perl/perl5/issues>.
Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2023-11-25 | perl v5.36.0 |