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.145 by root, Thu May 29 03:45:37 2008 UTC vs.
Revision 1.152 by root, Tue Jun 3 09:02:46 2008 UTC

1=head1 => NAME 1=head1 NAME
2 2
3AnyEvent - provide framework for multiple event loops 3AnyEvent - provide framework for multiple event loops
4 4
5EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops 5EV, Event, Glib, Tk, Perl, Event::Lib, Qt, POE - various supported event loops
6 6
17 }); 17 });
18 18
19 my $w = AnyEvent->condvar; # stores whether a condition was flagged 19 my $w = AnyEvent->condvar; # stores whether a condition was flagged
20 $w->send; # wake up current and all future recv's 20 $w->send; # wake up current and all future recv's
21 $w->recv; # enters "main loop" till $condvar gets ->send 21 $w->recv; # enters "main loop" till $condvar gets ->send
22
23=head1 INTRODUCTION/TUTORIAL
24
25This manpage is mainly a reference manual. If you are interested
26in a tutorial or some gentle introduction, have a look at the
27L<AnyEvent::Intro> manpage.
22 28
23=head1 WHY YOU SHOULD USE THIS MODULE (OR NOT) 29=head1 WHY YOU SHOULD USE THIS MODULE (OR NOT)
24 30
25Glib, POE, IO::Async, Event... CPAN offers event models by the dozen 31Glib, POE, IO::Async, Event... CPAN offers event models by the dozen
26nowadays. So what is different about AnyEvent? 32nowadays. So what is different about AnyEvent?
132Many watchers either are used with "recursion" (repeating timers for 138Many watchers either are used with "recursion" (repeating timers for
133example), or need to refer to their watcher object in other ways. 139example), or need to refer to their watcher object in other ways.
134 140
135An any way to achieve that is this pattern: 141An any way to achieve that is this pattern:
136 142
137 my $w; $w = AnyEvent->type (arg => value ..., cb => sub { 143 my $w; $w = AnyEvent->type (arg => value ..., cb => sub {
138 # you can use $w here, for example to undef it 144 # you can use $w here, for example to undef it
139 undef $w; 145 undef $w;
140 }); 146 });
141 147
142Note that C<my $w; $w => combination. This is necessary because in Perl, 148Note that C<my $w; $w => combination. This is necessary because in Perl,
143my variables are only visible after the statement in which they are 149my variables are only visible after the statement in which they are
144declared. 150declared.
145 151
346AnyEvent program, you I<have> to create at least one watcher before you 352AnyEvent program, you I<have> to create at least one watcher before you
347C<fork> the child (alternatively, you can call C<AnyEvent::detect>). 353C<fork> the child (alternatively, you can call C<AnyEvent::detect>).
348 354
349Example: fork a process and wait for it 355Example: fork a process and wait for it
350 356
351 my $done = AnyEvent->condvar; 357 my $done = AnyEvent->condvar;
352 358
353 my $pid = fork or exit 5; 359 my $pid = fork or exit 5;
354 360
355 my $w = AnyEvent->child ( 361 my $w = AnyEvent->child (
356 pid => $pid, 362 pid => $pid,
357 cb => sub { 363 cb => sub {
358 my ($pid, $status) = @_; 364 my ($pid, $status) = @_;
359 warn "pid $pid exited with status $status"; 365 warn "pid $pid exited with status $status";
360 $done->send; 366 $done->send;
361 }, 367 },
362 ); 368 );
363 369
364 # do something else, then wait for process exit 370 # do something else, then wait for process exit
365 $done->recv; 371 $done->recv;
366 372
367=head2 CONDITION VARIABLES 373=head2 CONDITION VARIABLES
368 374
369If you are familiar with some event loops you will know that all of them 375If you are familiar with some event loops you will know that all of them
370require you to run some blocking "loop", "run" or similar function that 376require you to run some blocking "loop", "run" or similar function that
591 597
592This is a mutator function that returns the callback set and optionally 598This is a mutator function that returns the callback set and optionally
593replaces it before doing so. 599replaces it before doing so.
594 600
595The callback will be called when the condition becomes "true", i.e. when 601The callback will be called when the condition becomes "true", i.e. when
596C<send> or C<croak> are called. Calling C<recv> inside the callback 602C<send> or C<croak> are called, with the only argument being the condition
597or at any later time is guaranteed not to block. 603variable itself. Calling C<recv> inside the callback or at any later time
604is guaranteed not to block.
598 605
599=back 606=back
600 607
601=head1 GLOBAL VARIABLES AND FUNCTIONS 608=head1 GLOBAL VARIABLES AND FUNCTIONS
602 609
800no warnings; 807no warnings;
801use strict; 808use strict;
802 809
803use Carp; 810use Carp;
804 811
805our $VERSION = '4.1'; 812our $VERSION = 4.12;
806our $MODEL; 813our $MODEL;
807 814
808our $AUTOLOAD; 815our $AUTOLOAD;
809our @ISA; 816our @ISA;
810 817
1166This functionality might change in future versions. 1173This functionality might change in future versions.
1167 1174
1168For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you 1175For example, to force the pure perl model (L<AnyEvent::Impl::Perl>) you
1169could start your program like this: 1176could start your program like this:
1170 1177
1171 PERL_ANYEVENT_MODEL=Perl perl ... 1178 PERL_ANYEVENT_MODEL=Perl perl ...
1172 1179
1173=item C<PERL_ANYEVENT_PROTOCOLS> 1180=item C<PERL_ANYEVENT_PROTOCOLS>
1174 1181
1175Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences 1182Used by both L<AnyEvent::DNS> and L<AnyEvent::Socket> to determine preferences
1176for IPv4 or IPv6. The default is unspecified (and might change, or be the result 1183for IPv4 or IPv6. The default is unspecified (and might change, or be the result
1651specified in the variable. 1658specified in the variable.
1652 1659
1653You can make AnyEvent completely ignore this variable by deleting it 1660You can make AnyEvent completely ignore this variable by deleting it
1654before the first watcher gets created, e.g. with a C<BEGIN> block: 1661before the first watcher gets created, e.g. with a C<BEGIN> block:
1655 1662
1656 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } 1663 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
1657 1664
1658 use AnyEvent; 1665 use AnyEvent;
1659 1666
1660Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1667Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1661be used to probe what backend is used and gain other information (which is 1668be used to probe what backend is used and gain other information (which is
1662probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1669probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1663 1670
1684Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. 1691Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>.
1685 1692
1686 1693
1687=head1 AUTHOR 1694=head1 AUTHOR
1688 1695
1689 Marc Lehmann <schmorp@schmorp.de> 1696 Marc Lehmann <schmorp@schmorp.de>
1690 http://home.schmorp.de/ 1697 http://home.schmorp.de/
1691 1698
1692=cut 1699=cut
1693 1700
16941 17011
1695 1702

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines