IO::LineBufferedSet (Chapter 13) This module works hand-in-hand with IO::LineBufferedSessionData to provide line-oriented reading in a nonblocking multiplexed application. It inherits from IO::SessionSet, which is listed in Chapter 13. 0 package IO::LineBufferedSet; 1 # file: IO/LineBufferedSet.pm 2 use strict; 3 use Carp; 4 use IO::SessionSet; 5 use IO::LineBufferedSessionData; 6 use vars '@ISA','$VERSION'; 7 @ISA = 'IO::SessionSet'; 8 $VERSION = '1.00'; 9 # override SessionDataClass so that we create an IO::LineBufferedSessionData 10 # rather than an IO::SessionData. 11 sub SessionDataClass { return 'IO::LineBufferedSessionData'; } 12 # override wait() in order to return sessions with pending data immediately. 13 sub wait { 14 my $self = shift; 15 # look for old buffered data first 16 my @sessions = grep {$_->has_buffered_data} $self->sessions; 17 return @sessions if @sessions; 18 return $self->SUPER::wait(@_); 19 } 20 1; 21 =head1 NAME 22 IO::LineBufferedSet - Handling of nonblocking line-buffered I/O 23 =head1 SYNOPSIS 24 use IO::LineBufferedSet; 25 my $set = IO::LineBufferedSet->new(); 26 $set->add($_) foreach ($handle1,$handle2,$handle3); 27 my $line; 28 while ($set->sessions) { 29 my @ready = $set->wait; 30 for my $h (@ready) { 31 unless (my $bytes = $h->getline($line)) { # fetch a line 32 $h->close; # EOF or an error 33 next; 34 } 35 next unless $bytes > 0; # skip zero-length line 36 my $result = process_data($line); # do some processing on the line 37 $line->write($result); # write result to handle 38 } 39 } 40 =head1 DESCRIPTION 41 This package provides support for sets of nonblocking handles for use 42 in multiplexed applications. 43 =head1 CONSTRUCTOR 44 =over 4 45 =item $set = IO::LineBufferedSet->new([$listen_sock]) 46 The new() method constructs a new IO::LineBufferedSet. If a listening 47 IO::Socket object is provided in C<$listen_sock>, then the wait() 48 method (see later) calls accept() on this socket whenever an 49 incoming connection is received, and the resulting connected socket 50 is added to the set. 51 =back 52 =head1 OBJECT METHODS 53 =over 4 54 =item $result = $set->add($handle [,$writeonly]) 55 The add() method adds the handle indicated in C<$handle> to the 56 set of handles to be monitored. It accepts an ordinary filehandle or 57 an IO::Handle (including IO::Socket). The handle will be made 58 nonblocking and wrapped inside an IO::LineBufferedSessionData object, 59 hereafter called "sessions". 60 C<$writeonly>, if provided, is a flag indicating that the filehandle 61 is write only. This is appropriate when adding handles such as 62 STDOUT. 63 If successful, add() returns a true result. 64 =item @sessions = $set->sessions 65 The sessions() method returns a list of IO::LineBufferedSessionData 66 objects, each one corresponding to a handle added either manually with 67 add() or automatically by wait(). 68 =item $result = $set->delete($handle) 69 This method deletes the indicated handle from the monitored set. You 70 may use either the handle itself, or the corresponding 71 IO::LineBufferedSessionData. 72 =item @ready = $set->wait([$timeout]) 73 The wait() method returns the list of IO::LineBufferedSessionData 74 objects that are ready for reading. Internally, the wait() method 75 calls accept() on the listening socket, if one was provided to the 76 new() method, and attempts to complete pending writes on 77 sessions. If a timeout is provided, the method returns an empty 78 list if the specified time expires without a session becoming ready 79 for reading. Otherwise, it blocks indefinitely. 80 Sessions are always ready for writing, since they are nonblocking. 81 =back 82 =head1 SEE ALSO 83 L<IO::LineBufferedSessionData>, L<IO::SessionData>, L<IO::SessionSet >, 84 L<perl> 85 =head1 AUTHOR 86 Lincoln Stein <lstein@cshl.org> 87 =head1 COPYRIGHT 88 Copyright (c) 2000 Lincoln Stein. All rights reserved. This program is 89 free software; you can redistribute it and/or modify it under the same 90 terms as Perl itself. 91 =cut |