1 | =head1 NAME |
1 | =head1 NAME |
2 | |
2 | |
3 | AnyEvent - ??? |
3 | AnyEvent - provide framework for multiple event loops |
|
|
4 | |
|
|
5 | Event, Coro, Glib, Tk - various supported event loops |
4 | |
6 | |
5 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
6 | |
8 | |
|
|
9 | use AnyEvent; |
|
|
10 | |
|
|
11 | my $w = AnyEvent->timer (fh => ..., poll => "[rw]+", cb => sub { |
|
|
12 | my ($poll_got) = @_; |
|
|
13 | ... |
|
|
14 | }); |
|
|
15 | my $w = AnyEvent->io (after => $seconds, cb => sub { |
|
|
16 | ... |
|
|
17 | }); |
|
|
18 | |
|
|
19 | # watchers get canceled whenever $w is destroyed |
|
|
20 | # only one watcher per $fh and $poll type is allowed |
|
|
21 | # (i.e. on a socket you cna have one r + one w or one rw |
|
|
22 | # watcher, not any more. |
|
|
23 | # timers can only be used once |
|
|
24 | |
|
|
25 | my $w = AnyEvent->condvar; # kind of main loop replacement |
|
|
26 | # can only be used once |
|
|
27 | $w->wait; # enters main loop till $condvar gets ->send |
|
|
28 | $w->broadcast; # wake up waiting and future wait's |
|
|
29 | |
7 | =head1 DESCRIPTION |
30 | =head1 DESCRIPTION |
|
|
31 | |
|
|
32 | L<AnyEvent> provides an identical interface to multiple event loops. This |
|
|
33 | allows module authors to utilizy an event loop without forcing module |
|
|
34 | users to use the same event loop (as only a single event loop can coexist |
|
|
35 | peacefully at any one time). |
|
|
36 | |
|
|
37 | The interface itself is vaguely similar but not identical to the Event |
|
|
38 | module. |
|
|
39 | |
|
|
40 | On the first call of any method, the module tries to detect the currently |
|
|
41 | loaded event loop by probing wether any of the following modules is |
|
|
42 | loaded: L<Coro::Event>, L<Event>, L<Glib>, L<Tk>. The first one found is |
|
|
43 | used. If none is found, the module tries to load these modules in the |
|
|
44 | order given. The first one that could be successfully loaded will be |
|
|
45 | used. If still none could be found, it will issue an error. |
8 | |
46 | |
9 | =over 4 |
47 | =over 4 |
10 | |
48 | |
11 | =cut |
49 | =cut |
12 | |
50 | |
13 | package AnyEvent; |
51 | package AnyEvent; |
14 | |
52 | |
|
|
53 | no warnings; |
|
|
54 | use strict 'vars'; |
15 | use Carp; |
55 | use Carp; |
16 | |
56 | |
17 | $VERSION = 0.1; |
57 | our $VERSION = 0.1; |
|
|
58 | our $MODEL; |
18 | |
59 | |
19 | no warnings; |
60 | our $AUTOLOAD; |
|
|
61 | our @ISA; |
20 | |
62 | |
21 | my @models = ( |
63 | my @models = ( |
22 | [Coro => Coro::Event::], |
64 | [Coro => Coro::Event::], |
23 | [Event => Event::], |
65 | [Event => Event::], |
24 | [Glib => Glib::], |
66 | [Glib => Glib::], |
… | |
… | |
26 | ); |
68 | ); |
27 | |
69 | |
28 | sub AUTOLOAD { |
70 | sub AUTOLOAD { |
29 | $AUTOLOAD =~ s/.*://; |
71 | $AUTOLOAD =~ s/.*://; |
30 | |
72 | |
|
|
73 | unless ($MODEL) { |
|
|
74 | # check for already loaded models |
31 | for (@models) { |
75 | for (@models) { |
32 | my ($model, $package) = @$_; |
76 | my ($model, $package) = @$_; |
33 | if (defined ${"$package\::VERSION"}) { |
77 | if (scalar keys %{ *{"$package\::"} }) { |
34 | $EVENT = "AnyEvent::Impl::$model"; |
78 | eval "require AnyEvent::Impl::$model" |
35 | eval "require $EVENT"; die if $@; |
79 | or die; |
36 | goto &{"$EVENT\::$AUTOLOAD"}; |
80 | |
|
|
81 | last if $MODEL; |
|
|
82 | } |
|
|
83 | } |
|
|
84 | |
|
|
85 | unless ($MODEL) { |
|
|
86 | # try to load a model |
|
|
87 | |
|
|
88 | for (@models) { |
|
|
89 | my ($model, $package) = @$_; |
|
|
90 | eval "require AnyEvent::Impl::$model" |
|
|
91 | or die; |
|
|
92 | |
|
|
93 | last if $MODEL; |
|
|
94 | } |
|
|
95 | |
|
|
96 | $MODEL |
|
|
97 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
37 | } |
98 | } |
38 | } |
99 | } |
39 | |
100 | |
40 | for (@models) { |
101 | @ISA = $MODEL; |
41 | my ($model, $package) = @$_; |
102 | |
42 | $EVENT = "AnyEvent::Impl::$model"; |
103 | my $class = shift; |
43 | if (eval "require $EVENT") { |
104 | $class->$AUTOLOAD (@_); |
44 | goto &{"$EVENT\::$AUTOLOAD"}; |
105 | } |
|
|
106 | |
|
|
107 | =back |
|
|
108 | |
|
|
109 | =head1 EXAMPLE |
|
|
110 | |
|
|
111 | The following program uses an io watcher to read data from stdin, a timer |
|
|
112 | to display a message once per second, and a condvar to exit the program |
|
|
113 | when the user enters quit: |
|
|
114 | |
|
|
115 | use AnyEvent; |
|
|
116 | |
|
|
117 | my $cv = AnyEvent->condvar; |
|
|
118 | |
|
|
119 | my $io_watcher = AnyEvent->io (fh => \*STDIN, poll => 'r', cb => sub { |
|
|
120 | warn "io event <$_[0]>\n"; # will always output <r> |
|
|
121 | chomp (my $input = <STDIN>); # read a line |
|
|
122 | warn "read: $input\n"; # output what has been read |
|
|
123 | $cv->broadcast if $input =~ /^q/i; # quit program if /^q/i |
|
|
124 | }); |
|
|
125 | |
|
|
126 | my $time_watcher; # can only be used once |
|
|
127 | |
|
|
128 | sub new_timer { |
|
|
129 | $timer = AnyEvent->timer (after => 1, cb => sub { |
|
|
130 | warn "timeout\n"; # print 'timeout' about every second |
|
|
131 | &new_timer; # and restart the time |
45 | } |
132 | }); |
46 | } |
133 | } |
47 | |
134 | |
48 | die "No event module selected for AnyEvent and autodetect failed. Install any of these: Coro, Event, Glib or Tk."; |
135 | new_timer; # create first timer |
49 | } |
|
|
50 | |
136 | |
51 | 1; |
137 | $cv->wait; # wait until user enters /^q/i |
52 | |
138 | |
|
|
139 | =head1 SEE ALSO |
|
|
140 | |
|
|
141 | L<Coro::Event>, L<Coro>, L<Event>, L<Glib::Event>, L<Glib>, |
|
|
142 | L<AnyEvent::Impl::Coro>, |
|
|
143 | L<AnyEvent::Impl::Event>, |
|
|
144 | L<AnyEvent::Impl::Glib>, |
|
|
145 | L<AnyEvent::Impl::Tk>. |
|
|
146 | |
|
|
147 | =head1 |
|
|
148 | |
|
|
149 | =cut |
|
|
150 | |
|
|
151 | 1 |
|
|
152 | |