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.4 by elmex, Sun Apr 27 20:20:20 2008 UTC

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 seperator 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>
296 my $l = syswrite $self->{fh}, $self->{wbuf}, length $self->{wbuf}; 335 my $l = syswrite $self->{fh}, $self->{wbuf}, length $self->{wbuf};
297 336
298 if (not defined $l) { 337 if (not defined $l) {
299 return if $! == EAGAIN || $! == EINTR; 338 return if $! == EAGAIN || $! == EINTR;
300 delete $self->{write_w}; 339 delete $self->{write_w};
301
302 $self->{on_error}->($self, $!) if $self->{on_error}; 340 $self->{on_error}->($self) if $self->{on_error};
303 341
304 } else { 342 } else {
305 substr $self->{wbuf}, 0, $l, ''; 343 substr $self->{wbuf}, 0, $l, '';
306 344
307 if (length ($self->{wbuf}) == 0) { 345 if (length ($self->{wbuf}) == 0) {
321 359
322=head1 AUTHOR 360=head1 AUTHOR
323 361
324Robin Redeker, C<< <elmex at ta-sa.org> >> 362Robin Redeker, C<< <elmex at ta-sa.org> >>
325 363
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 364=cut
372 365
3731; # End of AnyEvent::Handle 3661; # End of AnyEvent::Handle

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines