… | |
… | |
256 | =item B<readlines ($callback)> |
256 | =item B<readlines ($callback)> |
257 | |
257 | |
258 | =item B<readlines ($sep, $callback)> |
258 | =item B<readlines ($sep, $callback)> |
259 | |
259 | |
260 | This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> |
260 | This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> |
261 | if C<$sep> is not provided. C<$sep> will be used as part of a regex, so it can be |
261 | if C<$sep> is not provided. C<$sep> will be used as "line" seperator. |
262 | a regex itself and won't be quoted! |
|
|
263 | |
262 | |
264 | The C<$callback> will be called when at least one |
263 | The C<$callback> will be called when at least one |
265 | line could be read. The first argument to the C<$callback> will be the L<AnyEvent::Handle> |
264 | line could be read. The first argument to the C<$callback> will be the L<AnyEvent::Handle> |
266 | object itself and the rest of the arguments will be the read lines. |
265 | object itself and the rest of the arguments will be the read lines. |
267 | |
266 | |
268 | NOTE: This method will override any callbacks installed via the C<on_read> method. |
267 | NOTE: This method will override any callbacks installed via the C<on_read> method. |
269 | |
268 | |
270 | =cut |
269 | =cut |
271 | |
270 | |
272 | sub readlines { |
271 | sub readlines { |
273 | my ($self, $NL, $cb) = @_; |
272 | my ($self, $sep, $cb) = @_; |
274 | |
273 | |
275 | if (ref $NL) { |
274 | if (ref $sep) { |
276 | $cb = $NL; |
275 | $cb = $sep; |
277 | $NL = "\n"; |
276 | $sep = "\n"; |
|
|
277 | |
|
|
278 | } elsif (not defined $sep) { |
|
|
279 | $sep = "\n"; |
278 | } |
280 | } |
|
|
281 | |
|
|
282 | my $sep_len = length $sep; |
279 | |
283 | |
280 | $self->{on_readline} = $cb; |
284 | $self->{on_readline} = $cb; |
281 | |
285 | |
282 | $self->on_read (sub { |
286 | $self->on_read (sub { |
283 | my @lines; |
287 | my @lines; |
284 | 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 | } |
285 | $self->{on_readline}->($_[0], @lines); |
293 | $self->{on_readline}->($_[0], @lines); |
286 | }); |
294 | }); |
287 | } |
295 | } |
288 | |
296 | |
289 | =item B<write ($data)> |
297 | =item B<write ($data)> |
… | |
… | |
359 | |
367 | |
360 | =head1 AUTHOR |
368 | =head1 AUTHOR |
361 | |
369 | |
362 | Robin Redeker, C<< <elmex at ta-sa.org> >> |
370 | Robin Redeker, C<< <elmex at ta-sa.org> >> |
363 | |
371 | |
364 | =head1 BUGS |
|
|
365 | |
|
|
366 | Please report any bugs or feature requests to |
|
|
367 | C<bug-io-anyevent at rt.cpan.org>, or through the web interface at |
|
|
368 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-AnyEvent>. |
|
|
369 | I will be notified, and then you'll automatically be notified of progress on |
|
|
370 | your bug as I make changes. |
|
|
371 | |
|
|
372 | =head1 SUPPORT |
|
|
373 | |
|
|
374 | You can find documentation for this module with the perldoc command. |
|
|
375 | |
|
|
376 | perldoc AnyEvent::Handle |
|
|
377 | |
|
|
378 | You can also look for information at: |
|
|
379 | |
|
|
380 | =over 4 |
|
|
381 | |
|
|
382 | =item * AnnoCPAN: Annotated CPAN documentation |
|
|
383 | |
|
|
384 | L<http://annocpan.org/dist/IO-AnyEvent> |
|
|
385 | |
|
|
386 | =item * CPAN Ratings |
|
|
387 | |
|
|
388 | L<http://cpanratings.perl.org/d/IO-AnyEvent> |
|
|
389 | |
|
|
390 | =item * RT: CPAN's request tracker |
|
|
391 | |
|
|
392 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-AnyEvent> |
|
|
393 | |
|
|
394 | =item * Search CPAN |
|
|
395 | |
|
|
396 | L<http://search.cpan.org/dist/IO-AnyEvent> |
|
|
397 | |
|
|
398 | =back |
|
|
399 | |
|
|
400 | =head1 ACKNOWLEDGEMENTS |
|
|
401 | |
|
|
402 | =head1 COPYRIGHT & LICENSE |
|
|
403 | |
|
|
404 | Copyright 2008 Robin Redeker, all rights reserved. |
|
|
405 | |
|
|
406 | This program is free software; you can redistribute it and/or modify it |
|
|
407 | under the same terms as Perl itself. |
|
|
408 | |
|
|
409 | =cut |
372 | =cut |
410 | |
373 | |
411 | 1; # End of AnyEvent::Handle |
374 | 1; # End of AnyEvent::Handle |