1 | package AnyEvent::Handle; |
1 | package AnyEvent::Handle; |
2 | |
2 | |
3 | use warnings; |
3 | no warnings; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use AnyEvent; |
6 | use AnyEvent; |
7 | use IO::Handle; |
7 | use IO::Handle; |
8 | use Errno qw/EAGAIN EINTR/; |
8 | use 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 | |
67 | The default read block size use for reads via the C<on_read> |
85 | The default read block size use for reads via the C<on_read> |
68 | method. |
86 | method. |
|
|
87 | |
|
|
88 | =item on_read => $cb |
|
|
89 | |
|
|
90 | =item on_eof => $cb |
|
|
91 | |
|
|
92 | =item on_error => $cb |
|
|
93 | |
|
|
94 | These are shortcuts, that will call the corresponding method and set the callback to C<$cb>. |
|
|
95 | |
|
|
96 | =item on_readline => $cb |
|
|
97 | |
|
|
98 | The C<readlines> method is called with the default separated and C<$cb> as callback |
|
|
99 | for 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 | |
153 | Whenever a read or write operation resulted in an error the C<$callback> |
190 | Whenever a read or write operation resulted in an error the C<$callback> |
154 | will be called. |
191 | will be called. |
155 | |
192 | |
156 | The first argument of C<$callback> will be the L<AnyEvent::Handle> object itself |
193 | The first argument of C<$callback> will be the L<AnyEvent::Handle> object itself. |
157 | and the second argument will be the value of C<$!>. |
194 | The error is given as errno in C<$!>. |
158 | |
195 | |
159 | =cut |
196 | =cut |
160 | |
197 | |
161 | sub on_error { |
198 | sub on_error { |
162 | $_[0]->{on_error} = $_[1]; |
199 | $_[0]->{on_error} = $_[1]; |
… | |
… | |
182 | method is used directly. The C<read> and C<readlines> methods will provide |
219 | method is used directly. The C<read> and C<readlines> methods will provide |
183 | the read data to their callbacks. |
220 | the read data to their callbacks. |
184 | |
221 | |
185 | =cut |
222 | =cut |
186 | |
223 | |
187 | sub rbuf : lvalue { $_[0]->{rbuf} } |
224 | sub rbuf : lvalue { |
|
|
225 | $_[0]->{rbuf} |
|
|
226 | } |
188 | |
227 | |
189 | =item B<read ($len, $callback)> |
228 | =item B<read ($len, $callback)> |
190 | |
229 | |
191 | Will read exactly C<$len> bytes from the filehandle and call the C<$callback> |
230 | Will read exactly C<$len> bytes from the filehandle and call the C<$callback> |
192 | if done so. The first argument to the C<$callback> will be the L<AnyEvent::Handle> |
231 | if 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 | |
221 | This method will read lines from the filehandle, seperated by C<$sep> or C<"\n"> |
260 | This method will read lines from the filehandle, separated by C<$sep> or C<"\n"> |
222 | 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" separated. |
223 | a regex itself and won't be quoted! |
|
|
224 | |
262 | |
225 | The C<$callback> will be called when at least one |
263 | The C<$callback> will be called when at least one |
226 | 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> |
227 | 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. |
228 | |
266 | |
229 | 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. |
230 | |
268 | |
231 | =cut |
269 | =cut |
232 | |
270 | |
233 | sub readlines { |
271 | sub 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 | |
324 | Robin Redeker, C<< <elmex at ta-sa.org> >> |
370 | Robin Redeker, C<< <elmex at ta-sa.org> >> |
325 | |
371 | |
326 | =head1 BUGS |
|
|
327 | |
|
|
328 | Please report any bugs or feature requests to |
|
|
329 | C<bug-io-anyevent at rt.cpan.org>, or through the web interface at |
|
|
330 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=IO-AnyEvent>. |
|
|
331 | I will be notified, and then you'll automatically be notified of progress on |
|
|
332 | your bug as I make changes. |
|
|
333 | |
|
|
334 | =head1 SUPPORT |
|
|
335 | |
|
|
336 | You can find documentation for this module with the perldoc command. |
|
|
337 | |
|
|
338 | perldoc AnyEvent::Handle |
|
|
339 | |
|
|
340 | You can also look for information at: |
|
|
341 | |
|
|
342 | =over 4 |
|
|
343 | |
|
|
344 | =item * AnnoCPAN: Annotated CPAN documentation |
|
|
345 | |
|
|
346 | L<http://annocpan.org/dist/IO-AnyEvent> |
|
|
347 | |
|
|
348 | =item * CPAN Ratings |
|
|
349 | |
|
|
350 | L<http://cpanratings.perl.org/d/IO-AnyEvent> |
|
|
351 | |
|
|
352 | =item * RT: CPAN's request tracker |
|
|
353 | |
|
|
354 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=IO-AnyEvent> |
|
|
355 | |
|
|
356 | =item * Search CPAN |
|
|
357 | |
|
|
358 | L<http://search.cpan.org/dist/IO-AnyEvent> |
|
|
359 | |
|
|
360 | =back |
|
|
361 | |
|
|
362 | =head1 ACKNOWLEDGEMENTS |
|
|
363 | |
|
|
364 | =head1 COPYRIGHT & LICENSE |
|
|
365 | |
|
|
366 | Copyright 2008 Robin Redeker, all rights reserved. |
|
|
367 | |
|
|
368 | This program is free software; you can redistribute it and/or modify it |
|
|
369 | under the same terms as Perl itself. |
|
|
370 | |
|
|
371 | =cut |
372 | =cut |
372 | |
373 | |
373 | 1; # End of AnyEvent::Handle |
374 | 1; # End of AnyEvent::Handle |