|
|
1 | NAME |
|
|
2 | AnyEvent - provide framework for multiple event loops |
|
|
3 | |
|
|
4 | Event, Coro, Glib, Tk - various supported event loops |
|
|
5 | |
|
|
6 | SYNOPSIS |
|
|
7 | use AnyEvent; |
|
|
8 | |
|
|
9 | my $w = AnyEvent->timer (fh => ..., poll => "[rw]+", cb => sub { |
|
|
10 | my ($poll_got) = @_; |
|
|
11 | ... |
|
|
12 | }); |
|
|
13 | my $w = AnyEvent->io (after => $seconds, cb => sub { |
|
|
14 | ... |
|
|
15 | }); |
|
|
16 | |
|
|
17 | # watchers get canceled whenever $w is destroyed |
|
|
18 | # only one watcher per $fh and $poll type is allowed |
|
|
19 | # (i.e. on a socket you cna have one r + one w or one rw |
|
|
20 | # watcher, not any more. |
|
|
21 | # timers can only be used once |
|
|
22 | |
|
|
23 | my $w = AnyEvent->condvar; # kind of main loop replacement |
|
|
24 | # can only be used once |
|
|
25 | $w->wait; # enters main loop till $condvar gets ->send |
|
|
26 | $w->broadcast; # wake up waiting and future wait's |
|
|
27 | |
|
|
28 | DESCRIPTION |
|
|
29 | AnyEvent provides an identical interface to multiple event loops. This |
|
|
30 | allows module authors to utilizy an event loop without forcing module |
|
|
31 | users to use the same event loop (as only a single event loop can |
|
|
32 | coexist peacefully at any one time). |
|
|
33 | |
|
|
34 | The interface itself is vaguely similar but not identical to the Event |
|
|
35 | module. |
|
|
36 | |
|
|
37 | On the first call of any method, the module tries to detect the |
|
|
38 | currently loaded event loop by probing wether any of the following |
|
|
39 | modules is loaded: Coro::Event, Event, Glib, Tk. The first one found is |
|
|
40 | used. If none is found, the module tries to load these modules in the |
|
|
41 | order given. The first one that could be successfully loaded will be |
|
|
42 | used. If still none could be found, it will issue an error. |
|
|
43 | |
|
|
44 | EXAMPLE |
|
|
45 | The following program uses an io watcher to read data from stdin, a |
|
|
46 | timer to display a message once per second, and a condvar to exit the |
|
|
47 | program when the user enters quit: |
|
|
48 | |
|
|
49 | use AnyEvent; |
|
|
50 | |
|
|
51 | my $cv = AnyEvent->condvar; |
|
|
52 | |
|
|
53 | my $io_watcher = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
|
|
54 | warn "io event <$_[0]>\n"; # will always output <r> |
|
|
55 | chomp (my $input = <STDIN>); # read a line |
|
|
56 | warn "read: $input\n"; # output what has been read |
|
|
57 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
|
|
58 | }); |
|
|
59 | |
|
|
60 | my $time_watcher; # can only be used once |
|
|
61 | |
|
|
62 | sub new_timer { |
|
|
63 | $timer = AnyEvent->timer (after => 1, cb => sub { |
|
|
64 | warn "timeout\n"; # print 'timeout' about every second |
|
|
65 | &new_timer; # and restart the time |
|
|
66 | }); |
|
|
67 | } |
|
|
68 | |
|
|
69 | new_timer; # create first timer |
|
|
70 | |
|
|
71 | $cv->wait; # wait until user enters /^q/i |
|
|
72 | |
|
|
73 | SEE ALSO |
|
|
74 | Coro::Event, Coro, Event, Glib::Event, Glib, AnyEvent::Impl::Coro, |
|
|
75 | AnyEvent::Impl::Event, AnyEvent::Impl::Glib, AnyEvent::Impl::Tk. |
|
|
76 | |
|
|
77 | |