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

Comparing EV/EV.pm (file contents):
Revision 1.1 by root, Fri Oct 26 16:50:05 2007 UTC vs.
Revision 1.2 by root, Fri Oct 26 17:24:17 2007 UTC

3EV - perl interface to libevent, monkey.org/~provos/libevent/ 3EV - perl interface to libevent, monkey.org/~provos/libevent/
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use EV; 7 use EV;
8
9 # TIMER
10
11 my $w = EV::timer 2, 0, sub {
12 warn "is called after 2s";
13 };
14
15 my $w = EV::timer 2, 1, sub {
16 warn "is called roughly every 2s (repeat = 1)";
17 };
18
19 undef $w; # destroy event watcher again
20
21 # IO
22
23 my $w = EV::timer_abs 0, 60, sub {
24 warn "is called every minute, on the minute, exactly";
25 };
26
27 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub {
28 my ($w, $events) = @_; # all callbacks get the watcher object and event mask
29 if ($events & EV::TIMEOUT) {
30 warn "nothign received on stdin for 10 seconds, retrying";
31 } else {
32 warn "stdin is readable, you entered: ", <STDIN>;
33 }
34 };
35 $w->timeout (10);
36
37 # MAINLOOP
38 EV::dispatch; # loop as long as watchers are active
39 EV::loop; # the same thing
40 EV::loop EV::LOOP_ONCE;
41 EV::loop EV::LOOP_ONSHOT;
8 42
9=head1 DESCRIPTION 43=head1 DESCRIPTION
10 44
11This module provides an interface to libevent 45This module provides an interface to libevent
12(L<http://monkey.org/~provos/libevent/>). 46(L<http://monkey.org/~provos/libevent/>).
25 59
26=head1 FUNCTIONAL INTERFACE 60=head1 FUNCTIONAL INTERFACE
27 61
28=over 4 62=over 4
29 63
64=item $EV::NPRI
65
66How many priority levels are available.
67
68=item $time = EV::now
69
70Returns the time in (fractional) seconds since the epoch.
71
72=item $version = EV::version
73
74=item $method = EV::method
75
76Return version string and event polling method used.
77
78=item EV::loop $flags # EV::LOOP_ONCE, EV::LOOP_ONESHOT
79
80=item EV::loopexit $after
81
82Exit any active loop or dispatch after C<$after> seconds or immediately if
83C<$after> is missing or zero.
84
85=item EV::dispatch
86
87Same as C<EV::loop 0>.
88
89=item EV::event $callback
90
91Creates a new event watcher waiting for nothing, calling the given callback.
92
93=item my $w = EV::io $fileno_or_fh, $eventmask, $callback
94
95=item my $w = EV::io_ns $fileno_or_fh, $eventmask, $callback
96
97As long as the returned watcher object is alive, call the C<$callback>
98when the events specified in C<$eventmask> happen. Initially, the timeout
99is disabled.
100
101The C<io_ns> variant doesn't add/start the newly created watcher.
102
103Eventmask can be one or more of these constants ORed together:
104
105 EV::READ wait until read() wouldn't block anymore
106 EV::WRITE wait until write() wouldn't block anymore
107 EV::PERSIST stay active after an event occured
108
109=item my $w = EV::timer $after, $repeat, $callback
110
111=item my $w = EV::timer_ns $after, $repeat, $callback
112
113Calls the callback after C<$after> seconds. If C<$repeat> is true, the
114timer will be restarted after the callback returns. This means that the
115callback would be called roughly every C<$after> seconds, prolonged by the
116time the callback takes.
117
118The C<timer_ns> variant doesn't add/start the newly created watcher.
119
120=item my $w = EV::timer_abs $at, $interval, $callback
121
122=item my $w = EV::timer_abs_ns $at, $interval, $callback
123
124Similar to EV::timer, but the time is given as an absolute point in time
125(C<$at>), plus an optional C<$interval>.
126
127If the C<$interval> is zero, then the callback will be called at the time
128C<$at> if that is in the future, or as soon as possible if its in the
129past. It will not automatically repeat.
130
131If the C<$interval> is nonzero, then the watcher will always be scheduled
132to time out at the next C<$at + integer * $interval> time.
133
134This can be used to schedule a callback to run at very regular intervals,
135as long as the processing time is less then the interval (otherwise
136obviously events will be skipped).
137
138The C<timer_abs_ns> variant doesn't add/start the newly created watcher.
139
140=item my $w = EV::signal $signum, $callback
141
142=item my $w = EV::signal_ns $signum, $callback
143
144Call the callback when signal $signum is received.
145
146The C<signal_ns> variant doesn't add/start the newly created watcher.
147
30=back 148=back
31 149
150=head1 THE EV::Event CLASS
32 151
33=head1 OBJECT-ORIENTED INTERFACE 152All EV functions creating an event watcher (designated by C<my $w =>
34 153above) support the following methods on the returned watcher object:
35The object oriented interface lets you configure your own encoding or
36decoding style, within the limits of supported formats.
37 154
38=over 4 155=over 4
39 156
157=item $w->add ($timeout)
158
159Stops and (re-)starts the event watcher, setting the optional timeout to
160the given value, or clearing the timeout if none is given.
161
162=item $w->start
163
164Stops and (re-)starts the event watcher without touching the timeout.
165
166=item $w->del
167
168=item $w->stop
169
170Stop the event watcher if it was started.
171
172=item $current_callback = $w->cb
173
174=item $old_callback = $w->cb ($new_callback)
175
176Return the previously set callback and optionally set a new one.
177
178=item $current_fh = $w->fh
179
180=item $old_fh = $w->fh ($new_fh)
181
182Returns the previously set filehandle and optionally set a new one.
183
184=item $current_eventmask = $w->events
185
186=item $old_eventmask = $w->events ($new_eventmask)
187
188Returns the previously set event mask and optionally set a new one.
189
190=item $w->timeout ($after, $repeat)
191
192Resets the timeout (see C<EV::timer> for details).
193
194=item $w->timeout_abs ($at, $interval)
195
196Resets the timeout (see C<EV::timer_abs> for details).
197
198=item $w->priority_set ($priority)
199
200Set the priority of the watcher to C<$priority> (0 <= $priority < $EV::NPRI).
201
40=back 202=back
41 203
42=head1 BUGS 204=head1 BUGS
205
206Lots. Libevent itself isn't well tested and rather buggy, and this module
207is quite new at the moment.
43 208
44=cut 209=cut
45 210
46our $NPRI = 4; 211our $NPRI = 4;
47our $BASE = init; 212our $BASE = init;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines