1 |
=head1 NAME |
2 |
|
3 |
Coro::Event - do events the coro-way |
4 |
|
5 |
=head1 SYNOPSIS |
6 |
|
7 |
use Coro; |
8 |
use Coro::Event; |
9 |
|
10 |
sub keyboard : Coro { |
11 |
my $w = Coro::Event->io(fd => *STDIN, poll => 'r'); |
12 |
while() { |
13 |
print "cmd> "; |
14 |
my $ev = $w->next; my $cmd = <STDIN>; |
15 |
print "data> "; |
16 |
my $ev = $w->next; my $data = <STDIN>; |
17 |
} |
18 |
} |
19 |
|
20 |
=head1 DESCRIPTION |
21 |
|
22 |
This module enables you to create programs using the powerful Event modell |
23 |
(and module), while retaining the linear style known from simple or |
24 |
threaded programs. |
25 |
|
26 |
This module provides a method and a function for every watcher type |
27 |
(I<flavour>) (see L<Event>). The only difference between these and the |
28 |
watcher constructors from Event is that you do not specify a callback |
29 |
function - it will be managed by this module. |
30 |
|
31 |
Your application should just create all necessary coroutines and then call |
32 |
Coro::Event->main. |
33 |
|
34 |
=over 4 |
35 |
|
36 |
=cut |
37 |
|
38 |
package Coro::Event; |
39 |
|
40 |
no warnings; |
41 |
|
42 |
use Coro::Channel; |
43 |
|
44 |
use base 'Event'; |
45 |
use base 'Exporter'; |
46 |
|
47 |
$VERSION = 0.01; |
48 |
|
49 |
=item Coro::Event->flavour(args...) |
50 |
|
51 |
Create and return a watcher of the given type. |
52 |
|
53 |
Examples: |
54 |
|
55 |
my $reader = Coro::Event->io(fd => $filehandle, poll => 'r'); |
56 |
$reader->next; |
57 |
|
58 |
=cut |
59 |
|
60 |
=item do_flavour(args...) |
61 |
|
62 |
Create a watcher of the given type and immediately call it's next |
63 |
method. This is less efficient then calling the constructor once and the |
64 |
next method often, but it does save typing sometimes. |
65 |
|
66 |
=cut |
67 |
|
68 |
for my $flavour (qw(idle var timer io signal)) { |
69 |
push @EXPORT, "do_$flavour"; |
70 |
my $new = \&{"Event::$flavour"}; |
71 |
my $class = "Coro::Event::$flavour"; |
72 |
@{"${class}::ISA"} = ("Coro::Event", "Event::$flavour"); |
73 |
my $coronew = sub { |
74 |
# how does one do method-call-by-name? |
75 |
# my $w = $class->SUPER::$flavour(@_); |
76 |
|
77 |
my $q = new Coro::Channel 0; |
78 |
my $w; |
79 |
$w = $new->(@_, parked => 1, cb => sub { $w->stop; $q->put($_[0]) }); |
80 |
$w->private($q); # using private as attribute is pretty useless... |
81 |
bless $w, $class; # reblessing due to broken Event |
82 |
}; |
83 |
*{ $flavour } = $coronew; |
84 |
*{"do_$flavour"} = sub { |
85 |
unshift @_, $class; |
86 |
(&$coronew)->next; |
87 |
}; |
88 |
} |
89 |
|
90 |
=item $w->next |
91 |
|
92 |
Return the next event of the event queue of the watcher. |
93 |
|
94 |
=cut |
95 |
|
96 |
sub next { |
97 |
$_[0]->start; |
98 |
$_[0]->private->get; |
99 |
} |
100 |
|
101 |
=item Coro::Event->main |
102 |
|
103 |
=cut |
104 |
|
105 |
sub main { |
106 |
local $Coro::idle = new Coro sub { |
107 |
Event::loop; |
108 |
}; |
109 |
Coro::schedule; |
110 |
} |
111 |
|
112 |
1; |
113 |
|
114 |
=head1 BUGS |
115 |
|
116 |
This module is implemented straightforward using Coro::Channel and thus |
117 |
not as efficient as possible. |
118 |
|
119 |
=head1 AUTHOR |
120 |
|
121 |
Marc Lehmann <pcg@goof.com> |
122 |
http://www.goof.com/pcg/marc/ |
123 |
|
124 |
=cut |
125 |
|