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

Comparing AnyEvent/lib/AnyEvent/Intro.pod (file contents):
Revision 1.24 by root, Fri Jul 24 23:43:59 2009 UTC vs.
Revision 1.25 by root, Sat Jul 25 02:46:37 2009 UTC

5=head1 Introduction to AnyEvent 5=head1 Introduction to AnyEvent
6 6
7This is a tutorial that will introduce you to the features of AnyEvent. 7This is a tutorial that will introduce you to the features of AnyEvent.
8 8
9The first part introduces the core AnyEvent module (after swamping you a 9The first part introduces the core AnyEvent module (after swamping you a
10bit in evangelism), which might already provide all you ever need. If you 10bit in evangelism), which might already provide all you ever need: If you
11are only interested in AnyEvent's event handling capabilities, read no 11are only interested in AnyEvent's event handling capabilities, read no
12further. 12further.
13 13
14The second part focuses on network programming using sockets, for which 14The second part focuses on network programming using sockets, for which
15AnyEvent offers a lot of support you can use, and a lot of workarounds 15AnyEvent offers a lot of support you can use, and a lot of workarounds
23AnyEvent is first of all just a framework to do event-based 23AnyEvent is first of all just a framework to do event-based
24programming. Typically such frameworks are an all-or-nothing thing: If you 24programming. Typically such frameworks are an all-or-nothing thing: If you
25use one such framework, you can't (easily, or even at all) use another in 25use one such framework, you can't (easily, or even at all) use another in
26the same program. 26the same program.
27 27
28AnyEvent is different - it is a thin abstraction layer above all kinds 28AnyEvent is different - it is a thin abstraction layer on top of other of
29event loops, just like DBI is an abstraction of many different database
29of event loops. Its main purpose is to move the choice of the underlying 30APIs. Its main purpose is to move the choice of the underlying framework
30framework (the event loop) from the module author to the program author 31(the event loop) from the module author to the program author using the
31using the module. 32module.
32 33
33That means you can write code that uses events to control what it 34That means you can write code that uses events to control what it
34does, without forcing other code in the same program to use the same 35does, without forcing other code in the same program to use the same
35underlying framework as you do - i.e. you can create a Perl module 36underlying framework as you do - i.e. you can create a Perl module
36that is event-based using AnyEvent, and users of that module can still 37that is event-based using AnyEvent, and users of that module can still
37choose between using L<Gtk2>, L<Tk>, L<Event> or no event loop at 38choose between using L<Gtk2>, L<Tk>, L<Event> (or run inside Irssi or
38all: AnyEvent comes with its own event loop implementation, so your 39rxvt-unicode) or any other supported event loop. AnyEvent even comes with
39code works regardless of other modules that might or might not be 40its own pure-perl event loop implementation, so your code works regardless
40installed. The latter is important, as AnyEvent does not have any 41of other modules that might or might not be installed. The latter is
41dependencies to other modules, which makes it easy to install, for 42important, as AnyEvent does not have any hard dependencies to other
42example, when you lack a C compiler. 43modules, which makes it easy to install, for example, when you lack a C
44compiler. No mater what environment, AnyEvent will just cope with it.
43 45
44A typical problem with Perl modules such as L<Net::IRC> is that they 46A typical limitation of existing Perl modules such as L<Net::IRC> is that
45come with their own event loop: In L<Net::IRC>, the program who uses it 47they come with their own event loop: In L<Net::IRC>, the program who uses
46needs to start the event loop of L<Net::IRC>. That means that one cannot 48it needs to start the event loop of L<Net::IRC>. That means that one
47integrate this module into a L<Gtk2> GUI for instance, as that module, 49cannot integrate this module into a L<Gtk2> GUI for instance, as that
48too, enforces the use of its own event loop (namely L<Glib>). 50module, too, enforces the use of its own event loop (namely L<Glib>).
49 51
50Another example is L<LWP>: it provides no event interface at all. It's a 52Another example is L<LWP>: it provides no event interface at all. It's
51pure blocking HTTP (and FTP etc.) client library, which usually means that 53a pure blocking HTTP (and FTP etc.) client library, which usually means
52you either have to start a thread or have to fork for a HTTP request, or 54that you either have to start another process or have to fork for a HTTP
53use L<Coro::LWP>, if you want to do something else while waiting for the 55request, or use threads (e.g. L<Coro::LWP>), if you want to do something
54request to finish. 56else while waiting for the request to finish.
55 57
56The motivation behind these designs is often that a module doesn't want to 58The motivation behind these designs is often that a module doesn't want
57depend on some complicated XS-module (Net::IRC), or that it doesn't want 59to depend on some complicated XS-module (Net::IRC), or that it doesn't
58to force the user to use some specific event loop at all (LWP). 60want to force the user to use some specific event loop at all (LWP), out
61of fear of severly limiting the usefulness of the module: If your module
62requires Glib, it will not run in a Tk program.
59 63
60L<AnyEvent> solves this dilemma, by B<not> forcing module authors to either 64L<AnyEvent> solves this dilemma, by B<not> forcing module authors to
65either:
61 66
62=over 4 67=over 4
63 68
64=item - write their own event loop (because guarantees to offer one 69=item - write their own event loop (because it guarantees the availability
65everywhere - even on windows). 70of an event loop everywhere - even on windows with no extra modules
71installed).
66 72
67=item - choose one fixed event loop (because AnyEvent works with all 73=item - choose one specific event loop (because AnyEvent works with most
68important event loops available for Perl, and adding others is trivial). 74event loops available for Perl).
69 75
70=back 76=back
71 77
72If the module author uses L<AnyEvent> for all his event needs (IO events, 78If the module author uses L<AnyEvent> for all his (or her) event needs
73timers, signals, ...) then all other modules can just use his module and 79(IO events, timers, signals, ...) then all other modules can just use
74don't have to choose an event loop or adapt to his event loop. The choice 80his module and don't have to choose an event loop or adapt to his event
75of the event loop is ultimately made by the program author who uses all 81loop. The choice of the event loop is ultimately made by the program
76the modules and writes the main program. And even there he doesn't have to 82author who uses all the modules and writes the main program. And even
77choose, he can just let L<AnyEvent> choose the best available event loop 83there he doesn't have to choose, he can just let L<AnyEvent> choose the
78for him. 84most efficient event loop available on the system.
79 85
80Read more about this in the main documentation of the L<AnyEvent> module. 86Read more about this in the main documentation of the L<AnyEvent> module.
81 87
82 88
83=head1 Introduction to Event-Based Programming 89=head1 Introduction to Event-Based Programming

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines