… | |
… | |
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 | |
… | |
… | |
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. The watcher will only trigger once. This works by |
|
|
166 | installing a signal handler for "SIGCHLD". |
|
|
167 | |
|
|
168 | Example: wait for pid 1333 |
|
|
169 | |
|
|
170 | my $w = AnyEvent->child (pid => 1333, cb => sub { warn "exit status $?" }); |
|
|
171 | |
150 | GLOBALS |
172 | GLOBALS |
151 | $AnyEvent::MODEL |
173 | $AnyEvent::MODEL |
152 | Contains "undef" until the first watcher is being created. Then it |
174 | 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 |
175 | 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 |
176 | the Perl class implementing the model. This class is usually one of |
… | |
… | |
161 | AnyEvent::Impl::Event based on Event, also best choice :) |
183 | AnyEvent::Impl::Event based on Event, also best choice :) |
162 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
184 | AnyEvent::Impl::Glib based on Glib, second-best choice. |
163 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
185 | AnyEvent::Impl::Tk based on Tk, very bad choice. |
164 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
186 | AnyEvent::Impl::Perl pure-perl implementation, inefficient. |
165 | |
187 | |
|
|
188 | AnyEvent::detect |
|
|
189 | Returns $AnyEvent::MODEL, forcing autodetection of the event model |
|
|
190 | if necessary. You should only call this function right before you |
|
|
191 | would have created an AnyEvent watcher anyway, that is, very late at |
|
|
192 | runtime. |
|
|
193 | |
166 | WHAT TO DO IN A MODULE |
194 | WHAT TO DO IN A MODULE |
167 | As a module author, you should "use AnyEvent" and call AnyEvent methods |
195 | 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. |
196 | freely, but you should not load a specific event module or rely on it. |
169 | |
197 | |
170 | Be careful when you create watchers in the module body - Anyevent will |
198 | Be careful when you create watchers in the module body - Anyevent will |
… | |
… | |
207 | This tells AnyEvent to (literally) use the "urxvt::anyevent::" |
235 | This tells AnyEvent to (literally) use the "urxvt::anyevent::" |
208 | package/class when it finds the "urxvt" package/module is loaded. When |
236 | 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 |
237 | AnyEvent is loaded and asked to find a suitable event model, it will |
210 | first check for the presence of urxvt. |
238 | first check for the presence of urxvt. |
211 | |
239 | |
212 | The class should prove implementations for all watcher types (see |
240 | The class should provide implementations for all watcher types (see |
213 | AnyEvent::Impl::Event (source code), AnyEvent::Impl::Glib (Source code) |
241 | AnyEvent::Impl::Event (source code), AnyEvent::Impl::Glib (Source code) |
214 | and so on for actual examples, use "perldoc -m AnyEvent::Impl::Glib" to |
242 | and so on for actual examples, use "perldoc -m AnyEvent::Impl::Glib" to |
215 | see the sources). |
243 | see the sources). |
216 | |
244 | |
217 | The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the |
245 | The above isn't fictitious, the *rxvt-unicode* (a.k.a. urxvt) uses the |