… | |
… | |
4 | |
4 | |
5 | Event, Coro, Glib, Tk - various supported event loops |
5 | Event, Coro, Glib, Tk - various supported event loops |
6 | |
6 | |
7 | =head1 SYNOPSIS |
7 | =head1 SYNOPSIS |
8 | |
8 | |
9 | use AnyEvent; |
9 | use AnyEvent; |
10 | |
10 | |
11 | my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { |
11 | my $w = AnyEvent->io (fh => ..., poll => "[rw]+", cb => sub { |
12 | my ($poll_got) = @_; |
12 | my ($poll_got) = @_; |
13 | ... |
13 | ... |
14 | }); |
14 | }); |
15 | |
15 | |
16 | - only one io watcher per $fh and $poll type is allowed |
16 | * only one io watcher per $fh and $poll type is allowed (i.e. on a socket |
17 | (i.e. on a socket you can have one r + one w or one rw |
17 | you can have one r + one w or one rw watcher, not any more (limitation by |
18 | watcher, not any more. |
18 | Tk). |
19 | |
19 | |
|
|
20 | * the C<$poll_got> passed to the handler needs to be checked by looking |
|
|
21 | for single characters (e.g. with a regex), as it can contain more event |
|
|
22 | types than were requested (e.g. a 'w' watcher might generate 'rw' events, |
|
|
23 | limitation by Glib). |
|
|
24 | |
20 | - AnyEvent will keep filehandles alive, so as long as the watcher exists, |
25 | * AnyEvent will keep filehandles alive, so as long as the watcher exists, |
21 | the filehandle exists. |
26 | the filehandle exists. |
22 | |
27 | |
23 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
28 | my $w = AnyEvent->timer (after => $seconds, cb => sub { |
24 | ... |
29 | ... |
25 | }); |
30 | }); |
26 | |
31 | |
27 | - io and time watchers get canceled whenever $w is destroyed, so keep a copy |
32 | * io and time watchers get canceled whenever $w is destroyed, so keep a copy |
28 | |
33 | |
29 | - timers can only be used once and must be recreated for repeated operation |
34 | * timers can only be used once and must be recreated for repeated |
|
|
35 | operation (limitation by Glib and Tk). |
30 | |
36 | |
31 | my $w = AnyEvent->condvar; # kind of main loop replacement |
37 | my $w = AnyEvent->condvar; # kind of main loop replacement |
32 | $w->wait; # enters main loop till $condvar gets ->broadcast |
38 | $w->wait; # enters main loop till $condvar gets ->broadcast |
33 | $w->broadcast; # wake up current and all future wait's |
39 | $w->broadcast; # wake up current and all future wait's |
34 | |
40 | |
35 | - condvars are used to give blocking behaviour when neccessary. Create |
41 | * condvars are used to give blocking behaviour when neccessary. Create |
36 | a condvar for any "request" or "event" your module might create, C<< |
42 | a condvar for any "request" or "event" your module might create, C<< |
37 | ->broadcast >> it when the event happens and provide a function that calls |
43 | ->broadcast >> it when the event happens and provide a function that calls |
38 | C<< ->wait >> for it. See the examples below. |
44 | C<< ->wait >> for it. See the examples below. |
39 | |
45 | |
40 | =head1 DESCRIPTION |
46 | =head1 DESCRIPTION |
… | |
… | |
62 | |
68 | |
63 | no warnings; |
69 | no warnings; |
64 | use strict 'vars'; |
70 | use strict 'vars'; |
65 | use Carp; |
71 | use Carp; |
66 | |
72 | |
67 | our $VERSION = 0.3; |
73 | our $VERSION = '0.4'; |
68 | our $MODEL; |
74 | our $MODEL; |
69 | |
75 | |
70 | our $AUTOLOAD; |
76 | our $AUTOLOAD; |
71 | our @ISA; |
77 | our @ISA; |
|
|
78 | |
|
|
79 | our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1; |
72 | |
80 | |
73 | my @models = ( |
81 | my @models = ( |
74 | [Coro => Coro::Event::], |
82 | [Coro => Coro::Event::], |
75 | [Event => Event::], |
83 | [Event => Event::], |
76 | [Glib => Glib::], |
84 | [Glib => Glib::], |
… | |
… | |
87 | |
95 | |
88 | unless ($MODEL) { |
96 | unless ($MODEL) { |
89 | # check for already loaded models |
97 | # check for already loaded models |
90 | for (@models) { |
98 | for (@models) { |
91 | my ($model, $package) = @$_; |
99 | my ($model, $package) = @$_; |
92 | if (scalar keys %{ *{"$package\::"} }) { |
100 | if (${"$package\::VERSION"} > 0) { |
93 | eval "require AnyEvent::Impl::$model"; |
101 | eval "require AnyEvent::Impl::$model"; |
|
|
102 | warn "AnyEvent: found model '$model', using it.\n" if $MODEL && $verbose > 1; |
94 | last if $MODEL; |
103 | last if $MODEL; |
95 | } |
104 | } |
96 | } |
105 | } |
97 | |
106 | |
98 | unless ($MODEL) { |
107 | unless ($MODEL) { |
99 | # try to load a model |
108 | # try to load a model |
100 | |
109 | |
101 | for (@models) { |
110 | for (@models) { |
102 | my ($model, $package) = @$_; |
111 | my ($model, $package) = @$_; |
103 | eval "require AnyEvent::Impl::$model"; |
112 | eval "require AnyEvent::Impl::$model"; |
|
|
113 | warn "AnyEvent: autprobed and loaded model '$model', using it.\n" if $MODEL && $verbose > 1; |
104 | last if $MODEL; |
114 | last if $MODEL; |
105 | } |
115 | } |
106 | |
116 | |
107 | $MODEL |
117 | $MODEL |
108 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
118 | or die "No event module selected for AnyEvent and autodetect failed. Install any one of these modules: Coro, Event, Glib or Tk."; |
… | |
… | |
114 | my $class = shift; |
124 | my $class = shift; |
115 | $class->$AUTOLOAD (@_); |
125 | $class->$AUTOLOAD (@_); |
116 | } |
126 | } |
117 | |
127 | |
118 | =back |
128 | =back |
|
|
129 | |
|
|
130 | =head1 ENVIRONMENT VARIABLES |
|
|
131 | |
|
|
132 | The following environment variables are used by this module: |
|
|
133 | |
|
|
134 | C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event |
|
|
135 | model gets used. |
119 | |
136 | |
120 | =head1 EXAMPLE |
137 | =head1 EXAMPLE |
121 | |
138 | |
122 | The following program uses an io watcher to read data from stdin, a timer |
139 | The following program uses an io watcher to read data from stdin, a timer |
123 | to display a message once per second, and a condvar to exit the program |
140 | to display a message once per second, and a condvar to exit the program |