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.17 by root, Tue Jun 3 09:02:13 2008 UTC vs.
Revision 1.18 by root, Fri Jun 6 07:29:45 2008 UTC

124forgetting about the watcher, for example, by C<undef>'ing the variable it 124forgetting about the watcher, for example, by C<undef>'ing the variable it
125is stored in. AnyEvent will automatically clean up the watcher if it is no 125is stored in. AnyEvent will automatically clean up the watcher if it is no
126longer used, much like Perl closes your file handles if you no longer use 126longer used, much like Perl closes your file handles if you no longer use
127them anywhere. 127them anywhere.
128 128
129=head3 A short note on callbacks
130
131A common issue that hits people is the problem of passing parameters
132to callbacks. Programmers used to languages such as C or C++ are often
133used to a style where one passes the address of a function (a function
134reference) and some data value, e.g.:
135
136 sub callback {
137 my ($arg) = @_;
138
139 $arg->method;
140 }
141
142 my $arg = ...;
143
144 call_me_back_later \&callback, $arg;
145
146This is clumsy, as the place where behaviour is specified (when the
147callback is registered) is often far away from the place where behaviour
148is implemented. It also doesn't use Perl syntax to invoke the code. There
149is also an abstraction penalty to pay as one has to I<name> the callback,
150which often is unnecessary and leads to nonsensical or duplicated names.
151
152In Perl, one can specify behaviour much more directly by using
153I<closures>. Closures are code blocks that take a reference to the
154enclosing scope(s) when they are created. This means lexical variables in scope at the time
155of creating the closure can simply be used inside the closure:
156
157 my $arg = ...;
158
159 call_me_back_later sub { $arg->method };
160
161Under most circumstances, closures are faster, use less resources and
162result in much clearer code then the traditional approach. Faster,
163because parameter passing and storing them in local variables in Perl
164is relatively slow. Less resources, because closures take references to
165existing variables without having to create new ones, and clearer code
166because it is immediately obvious that the second example calls the
167C<method> method when the callback is invoked.
168
169Apart from these, the strongest argument for using closures with AnyEvent
170is that AnyEvent does not allow passing parameters to the callback, so
171closures are the only way to achieve that in most cases :->
172
173
129=head2 Condition Variables 174=head2 Condition Variables
130 175
131However, the above is not a fully working program, and will not work 176Back to the I/O watcher example: The code not yet a fully working program,
132as-is. The reason is that your callback will not be invoked out of the 177and will not work as-is. The reason is that your callback will not be
133blue, you have to run the event loop. Also, event-based programs sometimes 178invoked out of the blue, you have to run the event loop. Also, event-based
134have to block, too, as when there simply is nothing else to do and 179programs sometimes have to block, too, as when there simply is nothing
135everything waits for some events, it needs to block the process as well. 180else to do and everything waits for some events, it needs to block the
181process as well.
136 182
137In AnyEvent, this is done using condition variables. Condition variables 183In AnyEvent, this is done using condition variables. Condition variables
138are named "condition variables" because they represent a condition that is 184are named "condition variables" because they represent a condition that is
139initially false and needs to be fulfilled. 185initially false and needs to be fulfilled.
140 186
142or even callbacks and many other things (and they are often called like 188or even callbacks and many other things (and they are often called like
143this in other frameworks). The important point is that you can create them 189this in other frameworks). The important point is that you can create them
144freely and later wait for them to become true. 190freely and later wait for them to become true.
145 191
146Condition variables have two sides - one side is the "producer" of the 192Condition variables have two sides - one side is the "producer" of the
147condition (whatever code detects the condition), the other side is the 193condition (whatever code detects and flags the condition), the other side
148"consumer" (the code that waits for that condition). 194is the "consumer" (the code that waits for that condition).
149 195
150In our example in the previous section, the producer is the event callback 196In our example in the previous section, the producer is the event callback
151and there is no consumer yet - let's change that now: 197and there is no consumer yet - let's change that right now:
152 198
153 use AnyEvent; 199 use AnyEvent;
154 200
155 $| = 1; print "enter your name> "; 201 $| = 1; print "enter your name> ";
156 202

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines