… | |
… | |
65 | You can create I/O watcher by calling the "AnyEvent->io" method with the |
65 | You can create I/O watcher by calling the "AnyEvent->io" method with the |
66 | following mandatory arguments: |
66 | following mandatory arguments: |
67 | |
67 | |
68 | "fh" the Perl *filehandle* (not filedescriptor) to watch for events. |
68 | "fh" the Perl *filehandle* (not filedescriptor) to watch for events. |
69 | "poll" must be a string that is either "r" or "w", that creates a |
69 | "poll" must be a string that is either "r" or "w", that creates a |
70 | watcher waiting for "r"eadable or "w"ritable events. "cb" teh callback |
70 | watcher waiting for "r"eadable or "w"ritable events. "cb" the callback |
71 | to invoke everytime the filehandle becomes ready. |
71 | to invoke everytime the filehandle becomes ready. |
72 | |
72 | |
73 | Only one io watcher per "fh" and "poll" combination is allowed (i.e. on |
73 | Only one io watcher per "fh" and "poll" combination is allowed (i.e. on |
74 | a socket you can have one r + one w, not any more (limitation comes from |
74 | a socket you can have one r + one w, not any more (limitation comes from |
75 | Tk - if you are sure you are not using Tk this limitation is gone). |
75 | Tk - if you are sure you are not using Tk this limitation is gone). |
… | |
… | |
84 | chomp (my $input = <STDIN>); |
84 | chomp (my $input = <STDIN>); |
85 | warn "read: $input\n"; |
85 | warn "read: $input\n"; |
86 | undef $w; |
86 | undef $w; |
87 | }); |
87 | }); |
88 | |
88 | |
89 | TIMER WATCHERS |
89 | TIME WATCHERS |
90 | You can create a timer watcher by calling the "AnyEvent->timer" method |
90 | You can create a time watcher by calling the "AnyEvent->timer" method |
91 | with the following mandatory arguments: |
91 | with the following mandatory arguments: |
92 | |
92 | |
93 | "after" after how many seconds (fractions are supported) should the |
93 | "after" after how many seconds (fractions are supported) should the |
94 | timer activate. "cb" the callback to invoke. |
94 | timer activate. "cb" the callback to invoke. |
95 | |
95 | |
… | |
… | |
103 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
103 | my $w = AnyEvent->timer (after => 7.7, cb => sub { |
104 | warn "timeout\n"; |
104 | warn "timeout\n"; |
105 | }); |
105 | }); |
106 | |
106 | |
107 | # to cancel the timer: |
107 | # to cancel the timer: |
108 | undef $w |
108 | undef $w; |
109 | |
109 | |
110 | CONDITION WATCHERS |
110 | CONDITION WATCHERS |
111 | Condition watchers can be created by calling the "AnyEvent->condvar" |
111 | Condition watchers can be created by calling the "AnyEvent->condvar" |
112 | method without any arguments. |
112 | method without any arguments. |
113 | |
113 | |
… | |
… | |
145 | # or socket watcher the calls $result_ready->broadcast |
145 | # or socket watcher the calls $result_ready->broadcast |
146 | # when the "result" is ready. |
146 | # when the "result" is ready. |
147 | |
147 | |
148 | $result_ready->wait; |
148 | $result_ready->wait; |
149 | |
149 | |
|
|
150 | SIGNAL WATCHERS |
|
|
151 | You can listen for signals using a signal watcher, "signal" is the |
|
|
152 | signal *name* without any "SIG" prefix. Multiple signals events can be |
|
|
153 | clumped together into one callback invocation, and callback invocation |
|
|
154 | might or might not be asynchronous. |
|
|
155 | |
|
|
156 | These watchers might use %SIG, so programs overwriting those signals |
|
|
157 | directly will likely not work correctly. |
|
|
158 | |
|
|
159 | Example: exit on SIGINT |
|
|
160 | |
|
|
161 | my $w = AnyEvent->signal (signal => "INT", cb => sub { exit 1 }); |
|
|
162 | |
|
|
163 | CHILD PROCESS WATCHERS |
|
|
164 | You can also listen for the status of a child process specified by the |
|
|
165 | "pid" argument (or any child if the pid argument is 0). The watcher will |
|
|
166 | trigger as often as status change for the child are received. This works |
|
|
167 | by installing a signal handler for "SIGCHLD". The callback will be |
|
|
168 | called with the pid and exit status (as returned by waitpid). |
|
|
169 | |
|
|
170 | Example: wait for pid 1333 |
|
|
171 | |
|
|
172 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
|
|
173 | |
150 | GLOBALS |
174 | GLOBALS |
151 | $AnyEvent::MODEL |
175 | $AnyEvent::MODEL |
152 | Contains "undef" until the first watcher is being created. Then it |
176 | Contains "undef" until the first watcher is being created. Then it |
153 | contains the event model that is being used, which is the name of |
177 | contains the event model that is being used, which is the name of |
154 | the Perl class implementing the model. This class is usually one of |
178 | the Perl class implementing the model. This class is usually one of |
155 | the "AnyEvent::Impl:xxx" modules, but can be any other class in the |
179 | the "AnyEvent::Impl:xxx" modules, but can be any other class in the |
156 | case AnyEvent has been extended at runtime (e.g. in *rxvt-unicode*). |
180 | case AnyEvent has been extended at runtime (e.g. in *rxvt-unicode*). |
157 | |
181 | |
158 | The known classes so far are: |
182 | The known classes so far are: |
159 | |
183 | |
|
|
184 | AnyEvent::Impl::CoroEV based on Coro::EV, best choice. |
|
|
185 | AnyEvent::Impl::EV based on EV (an interface to libev, also best choice). |
160 | AnyEvent::Impl::Coro based on Coro::Event, best choise. |
186 | AnyEvent::Impl::Coro based on Coro::Event, second best choice. |
161 | AnyEvent::Impl::Event based on Event, also best choice :) |
187 | AnyEvent::Impl::Event based on Event, also second best choice :) |
162 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
188 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
163 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
189 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
164 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
190 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
|
|
191 | |
|
|
192 | AnyEvent::detect |
|
|
193 | Returns $AnyEvent::MODEL, forcing autodetection of the event model |
|
|
194 | if necessary. You should only call this function right before you |
|
|
195 | would have created an AnyEvent watcher anyway, that is, very late at |
|
|
196 | runtime. |
165 | |
197 | |
166 | WHAT TO DO IN A MODULE |
198 | WHAT TO DO IN A MODULE |
167 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
199 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
168 | freely, but you should not load a specific event module or rely on it. |
200 | freely, but you should not load a specific event module or rely on it. |
169 | |
201 | |
… | |
… | |
207 | This tells AnyEvent to (literally) use the "urxvt::anyevent::" |
239 | This tells AnyEvent to (literally) use the "urxvt::anyevent::" |
208 | package/class when it finds the "urxvt" package/module is loaded. When |
240 | package/class when it finds the "urxvt" package/module is loaded. When |
209 | AnyEvent is loaded and asked to find a suitable event model, it will |
241 | AnyEvent is loaded and asked to find a suitable event model, it will |
210 | first check for the presence of urxvt. |
242 | first check for the presence of urxvt. |
211 | |
243 | |
212 | The class should prove implementations for all watcher types (see |
244 | The class should provide implementations for all watcher types (see |
213 | AnyEvent::Impl::Event (source code), AnyEvent::Impl::Glib (Source code) |
245 | AnyEvent::Impl::Event (source code), AnyEvent::Impl::Glib (Source code) |
214 | and so on for actual examples, use "perldoc -m AnyEvent::Impl::Glib" to |
246 | and so on for actual examples, use "perldoc -m AnyEvent::Impl::Glib" to |
215 | see the sources). |
247 | see the sources). |
216 | |
248 | |
217 | The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the |
249 | The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the |
… | |
… | |
221 | *rxvt-unicode* distribution. |
253 | *rxvt-unicode* distribution. |
222 | |
254 | |
223 | *rxvt-unicode* also cheats a bit by not providing blocking access to |
255 | *rxvt-unicode* also cheats a bit by not providing blocking access to |
224 | condition variables: code blocking while waiting for a condition will |
256 | condition variables: code blocking while waiting for a condition will |
225 | "die". This still works with most modules/usages, and blocking calls |
257 | "die". This still works with most modules/usages, and blocking calls |
226 | must not be in an interactive appliation, so it makes sense. |
258 | must not be in an interactive application, so it makes sense. |
227 | |
259 | |
228 | ENVIRONMENT VARIABLES |
260 | ENVIRONMENT VARIABLES |
229 | The following environment variables are used by this module: |
261 | The following environment variables are used by this module: |
230 | |
262 | |
231 | "PERL_ANYEVENT_VERBOSE" when set to 2 or higher, reports which event |
263 | "PERL_ANYEVENT_VERBOSE" when set to 2 or higher, reports which event |