ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Handle.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Handle.pm (file contents):
Revision 1.1 by elmex, Sun Apr 27 16:56:17 2008 UTC vs.
Revision 1.7 by root, Thu May 1 16:35:40 2008 UTC

1package AnyEvent::Handle; 1package AnyEvent::Handle;
2 2
3use warnings; 3no warnings;
4use strict; 4use strict;
5 5
6use AnyEvent; 6use AnyEvent;
7use IO::Handle; 7use IO::Handle;
8use Errno qw/EAGAIN EINTR/; 8use Errno qw/EAGAIN EINTR/;
25 use AnyEvent::Handle; 25 use AnyEvent::Handle;
26 26
27 my $cv = AnyEvent->condvar; 27 my $cv = AnyEvent->condvar;
28 28
29 my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN); 29 my $ae_fh = AnyEvent::Handle->new (fh => \*STDIN);
30
31 $ae_fh->on_eof (sub { $cv->broadcast });
30 32
31 $ae_fh->readlines (sub { 33 $ae_fh->readlines (sub {
32 my ($ae_fh, @lines) = @_; 34 my ($ae_fh, @lines) = @_;
33 for (@lines) { 35 for (@lines) {
34 chomp; 36 chomp;
35 print "Line: $_"; 37 print "Line: $_";
36 } 38 }
37 $cv->broadcast;
38 }); 39 });
40
41 # or use the constructor to pass the callback:
42
43 my $ae_fh2 =
44 AnyEvent::Handle->new (
45 fh => \*STDIN,
46 on_eof => sub {
47 $cv->broadcast;
48 },
49 on_readline => sub {
50 my ($ae_fh, @lines) = @_;
51 for (@lines) {
52 chomp;
53 print "Line: $_";
54 }
55 }
56 );
39 57
40 $cv->wait; 58 $cv->wait;
41 59
42=head1 DESCRIPTION 60=head1 DESCRIPTION
43 61
64 82
65=item read_block_size => $size 83=item read_block_size => $size
66 84
67The default read block size use for reads via the C<on_read> 85The default read block size use for reads via the C<on_read>
68method. 86method.
87
88=item on_read => $cb
89
90=item on_eof => $cb
91
92=item on_error => $cb
93
94These are shortcuts, that will call the corresponding method and set the callback to C<$cb>.
95
96=item on_readline => $cb
97
98The C<readlines> method is called with the default separated and C<$cb> as callback
99for you.
69 100
70=back 101=back
71 102
72=cut 103=cut
73 104
86 if ($self->{on_read}) { 117 if ($self->{on_read}) {
87 $self->on_read ($self->{on_read}); 118 $self->on_read ($self->{on_read});
88 119
89 } elsif ($self->{on_readline}) { 120 } elsif ($self->{on_readline}) {
90 $self->readlines ($self->{on_readline}); 121 $self->readlines ($self->{on_readline});
122
123 } elsif ($self->{on_eof}) {
124 $self->on_eof ($self->{on_eof});
125
126 } elsif ($self->{on_error}) {
127 $self->on_eof ($self->{on_error});
91 } 128 }
92 129
93 return $self 130 return $self
94} 131}
95 132
133 } 170 }
134 #d# warn "READL $l [$self->{rbuf}]\n"; 171 #d# warn "READL $l [$self->{rbuf}]\n";
135 172
136 if (not defined $l) { 173 if (not defined $l) {
137 return if $! == EAGAIN || $! == EINTR; 174 return if $! == EAGAIN || $! == EINTR;
138 $self->{on_error}->($self, $!) if $self->{on_error}; 175 $self->{on_error}->($self) if $self->{on_error};
139 delete $self->{on_read_w}; 176 delete $self->{on_read_w};
140 177
141 } elsif ($l == 0) { 178 } elsif ($l == 0) {
142 $self->{on_eof}->($self) if $self->{on_eof}; 179 $self->{on_eof}->($self) if $self->{on_eof};
143 delete $self->{on_read_w}; 180 delete $self->{on_read_w};
151=item B<on_error ($callback)> 188=item B<on_error ($callback)>
152 189
153Whenever a read or write operation resulted in an error the C<$callback> 190Whenever a read or write operation resulted in an error the C<$callback>
154will be called. 191will be called.
155 192
156The first argument of C<$callback> will be the L<AnyEvent::Handle> object itself 193The first argument of C<$callback> will be the L<AnyEvent::Handle> object itself.
157and the second argument will be the value of C<$!>. 194The error is given as errno in C<$!>.
158 195
159=cut 196=cut
160 197
161sub on_error { 198sub on_error {
162 $_[0]->{on_error} = $_[1]; 199 $_[0]->{on_error} = $_[1];
182method is used directly. The C<read> and C<readlines> methods will provide 219method is used directly. The C<read> and C<readlines> methods will provide
183the read data to their callbacks. 220the read data to their callbacks.
184 221
185=cut 222=cut
186 223
187sub rbuf : lvalue { $_[0]->{rbuf} } 224sub rbuf : lvalue {
225 $_[0]->{rbuf}
226}
188 227
189=item B<read ($len, $callback)> 228=item B<read ($len, $callback)>
190 229
191Will read exactly C<$len> bytes from the filehandle and call the C<$callback> 230Will read exactly C<$len> bytes from the filehandle and call the C<$callback>
192if done so. The first argument to the C<$callback> will be the L<AnyEvent::Handle> 231if done so. The first argument to the C<$callback> will be the L<AnyEvent::Handle>
216 255
217=item B<readlines ($callback)> 256=item B<readlines ($callback)>
218 257
219=item B<readlines ($sep, $callback)> 258=item B<readlines ($sep, $callback)>
220 259
221This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> 260This method will read lines from the filehandle, separated by C<$sep> or C<"\n">
222if C<$sep> is not provided. C<$sep> will be used as part of a regex, so it can be 261if C<$sep> is not provided. C<$sep> will be used as "line" separated.
223a regex itself and won't be quoted!
224 262
225The C<$callback> will be called when at least one 263The C<$callback> will be called when at least one
226line could be read. The first argument to the C<$callback> will be the L<AnyEvent::Handle> 264line could be read. The first argument to the C<$callback> will be the L<AnyEvent::Handle>
227object itself and the rest of the arguments will be the read lines. 265object itself and the rest of the arguments will be the read lines.
228 266
229NOTE: This method will override any callbacks installed via the C<on_read> method. 267NOTE: This method will override any callbacks installed via the C<on_read> method.
230 268
231=cut 269=cut
232 270
233sub readlines { 271sub readlines {
234 my ($self, $NL, $cb) = @_; 272 my ($self, $sep, $cb) = @_;
235 273
236 if (ref $NL) { 274 if (ref $sep) {
237 $cb = $NL; 275 $cb = $sep;
238 $NL = "\n"; 276 $sep = "\n";
277
278 } elsif (not defined $sep) {
279 $sep = "\n";
239 } 280 }
281
282 my $sep_len = length $sep;
240 283
241 $self->{on_readline} = $cb; 284 $self->{on_readline} = $cb;
242 285
243 $self->on_read (sub { 286 $self->on_read (sub {
244 my @lines; 287 my @lines;
245 push @lines, $1 while $_[0]->{rbuf} =~ s/(.*)$NL//; 288 my $rb = \$_[0]->{rbuf};
289 my $pos;
290 while (($pos = index ($$rb, $sep)) >= 0) {
291 push @lines, substr $$rb, 0, $pos + $sep_len, '';
292 }
246 $self->{on_readline}->($_[0], @lines); 293 $self->{on_readline}->($_[0], @lines);
247 }); 294 });
248} 295}
249 296
250=item B<write ($data)> 297=item B<write ($data)>
296 my $l = syswrite $self->{fh}, $self->{wbuf}, length $self->{wbuf}; 343 my $l = syswrite $self->{fh}, $self->{wbuf}, length $self->{wbuf};
297 344
298 if (not defined $l) { 345 if (not defined $l) {
299 return if $! == EAGAIN || $! == EINTR; 346 return if $! == EAGAIN || $! == EINTR;
300 delete $self->{write_w}; 347 delete $self->{write_w};
301
302 $self->{on_error}->($self, $!) if $self->{on_error}; 348 $self->{on_error}->($self) if $self->{on_error};
303 349
304 } else { 350 } else {
305 substr $self->{wbuf}, 0, $l, ''; 351 substr $self->{wbuf}, 0, $l, '';
306 352
307 if (length ($self->{wbuf}) == 0) { 353 if (length ($self->{wbuf}) == 0) {
321 367
322=head1 AUTHOR 368=head1 AUTHOR
323 369
324Robin Redeker, C<< <elmex at ta-sa.org> >> 370Robin Redeker, C<< <elmex at ta-sa.org> >>
325 371
326=head1 BUGS
327
328Please report any bugs or feature requests to
329C<bug-io-anyevent at rt.cpan.org>, or through the web interface at
330L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-AnyEvent>.
331I will be notified, and then you'll automatically be notified of progress on
332your bug as I make changes.
333
334=head1 SUPPORT
335
336You can find documentation for this module with the perldoc command.
337
338 perldoc AnyEvent::Handle
339
340You can also look for information at:
341
342=over 4
343
344=item * AnnoCPAN: Annotated CPAN documentation
345
346L<http://annocpan.org/dist/IO-AnyEvent>
347
348=item * CPAN Ratings
349
350L<http://cpanratings.perl.org/d/IO-AnyEvent>
351
352=item * RT: CPAN's request tracker
353
354L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-AnyEvent>
355
356=item * Search CPAN
357
358L<http://search.cpan.org/dist/IO-AnyEvent>
359
360=back
361
362=head1 ACKNOWLEDGEMENTS
363
364=head1 COPYRIGHT & LICENSE
365
366Copyright 2008 Robin Redeker, all rights reserved.
367
368This program is free software; you can redistribute it and/or modify it
369under the same terms as Perl itself.
370
371=cut 372=cut
372 373
3731; # End of AnyEvent::Handle 3741; # End of AnyEvent::Handle

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines