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