ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent.pm (file contents):
Revision 1.5 by root, Sun Dec 4 09:44:32 2005 UTC vs.
Revision 1.7 by root, Fri Dec 30 01:28:31 2005 UTC

4 4
5Event, Coro, Glib, Tk - various supported event loops 5Event, Coro, Glib, Tk - various supported event loops
6 6
7=head1 SYNOPSIS 7=head1 SYNOPSIS
8 8
9use 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 17you can have one r + one w or one rw watcher, not any more (limitation by
18watcher, not any more. 18Tk).
19 19
20* the C<$poll_got> passed to the handler needs to be checked by looking
21for single characters (e.g. with a regex), as it can contain more event
22types than were requested (e.g. a 'w' watcher might generate 'rw' events,
23limitation 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,
21the filehandle exists. 26the 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
35operation (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
36a condvar for any "request" or "event" your module might create, C<< 42a 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
38C<< ->wait >> for it. See the examples below. 44C<< ->wait >> for it. See the examples below.
39 45
40=head1 DESCRIPTION 46=head1 DESCRIPTION
62 68
63no warnings; 69no warnings;
64use strict 'vars'; 70use strict 'vars';
65use Carp; 71use Carp;
66 72
67our $VERSION = 0.3; 73our $VERSION = '0.4';
68our $MODEL; 74our $MODEL;
69 75
70our $AUTOLOAD; 76our $AUTOLOAD;
71our @ISA; 77our @ISA;
78
79our $verbose = $ENV{PERL_ANYEVENT_VERBOSE}*1;
72 80
73my @models = ( 81my @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
132The following environment variables are used by this module:
133
134C<PERL_ANYEVENT_VERBOSE> when set to C<2> or higher, reports which event
135model gets used.
119 136
120=head1 EXAMPLE 137=head1 EXAMPLE
121 138
122The following program uses an io watcher to read data from stdin, a timer 139The following program uses an io watcher to read data from stdin, a timer
123to display a message once per second, and a condvar to exit the program 140to display a message once per second, and a condvar to exit the program
183 connect $txn->{fh}, ... 200 connect $txn->{fh}, ...
184 and !$!{EWOULDBLOCK} 201 and !$!{EWOULDBLOCK}
185 and !$!{EINPROGRESS} 202 and !$!{EINPROGRESS}
186 and Carp::croak "unable to connect: $!\n"; 203 and Carp::croak "unable to connect: $!\n";
187 204
188Then it creates a write-watcher which gets called wehnever an error occurs 205Then it creates a write-watcher which gets called whenever an error occurs
189or the connection succeeds: 206or the connection succeeds:
190 207
191 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w }); 208 $txn->{w} = AnyEvent->io (fh => $txn->{fh}, poll => 'w', cb => sub { $txn->fh_ready_w });
192 209
193And returns this transaction object. The C<fh_ready_w> callback gets 210And returns this transaction object. The C<fh_ready_w> callback gets
210 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf}; 227 sysread $txn->{fh}, $txn->{buf}, length $txn->{$buf};
211 228
212 if (end-of-file or data complete) { 229 if (end-of-file or data complete) {
213 $txn->{result} = $txn->{buf}; 230 $txn->{result} = $txn->{buf};
214 $txn->{finished}->broadcast; 231 $txn->{finished}->broadcast;
232 $txb->{cb}->($txn) of $txn->{cb}; # also call callback
215 } 233 }
216 234
217The C<result> method, finally, just waits for the finished signal (if the 235The C<result> method, finally, just waits for the finished signal (if the
218request was already finished, it doesn't wait, of course, and returns the 236request was already finished, it doesn't wait, of course, and returns the
219data: 237data:
220 238
221 $txn->{finished}->wait; 239 $txn->{finished}->wait;
222 return $txn->{buf}; 240 return $txn->{result};
223 241
224The actual code goes further and collects all errors (C<die>s, exceptions) 242The actual code goes further and collects all errors (C<die>s, exceptions)
225that occured during request processing. The C<result> method detects 243that occured during request processing. The C<result> method detects
226wether an exception as thrown (it is stored inside the $txn object) 244wether an exception as thrown (it is stored inside the $txn object)
227and just throws the exception, which means connection errors and other 245and just throws the exception, which means connection errors and other

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines