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.3 by elmex, Sun Apr 27 19:00:33 2008 UTC vs.
Revision 1.5 by elmex, Mon Apr 28 08:01:05 2008 UTC

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
260This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> 260This method will read lines from the filehandle, seperated by C<$sep> or C<"\n">
261if 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" seperator.
262a regex itself and won't be quoted!
263 262
264The C<$callback> will be called when at least one 263The C<$callback> will be called when at least one
265line 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>
266object 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.
267 266
268NOTE: 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.
269 268
270=cut 269=cut
271 270
272sub readlines { 271sub 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
362Robin Redeker, C<< <elmex at ta-sa.org> >> 370Robin Redeker, C<< <elmex at ta-sa.org> >>
363 371
364=head1 SUPPORT
365
366You can find documentation for this module with the perldoc command.
367
368 perldoc AnyEvent::Handle
369
370You can also look for information at:
371
372=head1 COPYRIGHT & LICENSE
373
374Copyright 2008 Robin Redeker, all rights reserved.
375
376This program is free software; you can redistribute it and/or modify it
377under the same terms as Perl itself.
378
379=cut 372=cut
380 373
3811; # End of AnyEvent::Handle 3741; # End of AnyEvent::Handle

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines