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.1 by root, Wed Apr 27 01:26:44 2005 UTC vs.
Revision 1.4 by root, Thu Dec 1 22:04:50 2005 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines