… | |
… | |
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)> |