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.150 by root, Sat May 31 13:06:45 2008 UTC vs.
Revision 1.151 by root, Sat May 31 13:38:01 2008 UTC

138Many watchers either are used with "recursion" (repeating timers for 138Many watchers either are used with "recursion" (repeating timers for
139example), or need to refer to their watcher object in other ways. 139example), or need to refer to their watcher object in other ways.
140 140
141An any way to achieve that is this pattern: 141An any way to achieve that is this pattern:
142 142
143 my $w; $w = AnyEvent->type (arg => value ..., cb => sub { 143 my $w; $w = AnyEvent->type (arg => value ..., cb => sub {
144 # you can use $w here, for example to undef it 144 # you can use $w here, for example to undef it
145 undef $w; 145 undef $w;
146 }); 146 });
147 147
148Note 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,
149my variables are only visible after the statement in which they are 149my variables are only visible after the statement in which they are
150declared. 150declared.
151 151
352AnyEvent 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
353C<fork> the child (alternatively, you can call C<AnyEvent::detect>). 353C<fork> the child (alternatively, you can call C<AnyEvent::detect>).
354 354
355Example: fork a process and wait for it 355Example: fork a process and wait for it
356 356
357 my $done = AnyEvent->condvar; 357 my $done = AnyEvent->condvar;
358 358
359 my $pid = fork or exit 5; 359 my $pid = fork or exit 5;
360 360
361 my $w = AnyEvent->child ( 361 my $w = AnyEvent->child (
362 pid => $pid, 362 pid => $pid,
363 cb => sub { 363 cb => sub {
364 my ($pid, $status) = @_; 364 my ($pid, $status) = @_;
365 warn "pid $pid exited with status $status"; 365 warn "pid $pid exited with status $status";
366 $done->send; 366 $done->send;
367 }, 367 },
368 ); 368 );
369 369
370 # do something else, then wait for process exit 370 # do something else, then wait for process exit
371 $done->recv; 371 $done->recv;
372 372
373=head2 CONDITION VARIABLES 373=head2 CONDITION VARIABLES
374 374
375If 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
376require you to run some blocking "loop", "run" or similar function that 376require you to run some blocking "loop", "run" or similar function that
1173This functionality might change in future versions. 1173This functionality might change in future versions.
1174 1174
1175For 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
1176could start your program like this: 1176could start your program like this:
1177 1177
1178 PERL_ANYEVENT_MODEL=Perl perl ... 1178 PERL_ANYEVENT_MODEL=Perl perl ...
1179 1179
1180=item C<PERL_ANYEVENT_PROTOCOLS> 1180=item C<PERL_ANYEVENT_PROTOCOLS>
1181 1181
1182Used 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
1183for 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
1658specified in the variable. 1658specified in the variable.
1659 1659
1660You can make AnyEvent completely ignore this variable by deleting it 1660You can make AnyEvent completely ignore this variable by deleting it
1661before 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:
1662 1662
1663 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} } 1663 BEGIN { delete $ENV{PERL_ANYEVENT_MODEL} }
1664 1664
1665 use AnyEvent; 1665 use AnyEvent;
1666 1666
1667Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can 1667Similar considerations apply to $ENV{PERL_ANYEVENT_VERBOSE}, as that can
1668be 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
1669probably even less useful to an attacker than PERL_ANYEVENT_MODEL). 1669probably even less useful to an attacker than PERL_ANYEVENT_MODEL).
1670 1670
1691Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>. 1691Nontrivial usage examples: L<Net::FCP>, L<Net::XMPP2>, L<AnyEvent::DNS>.
1692 1692
1693 1693
1694=head1 AUTHOR 1694=head1 AUTHOR
1695 1695
1696 Marc Lehmann <schmorp@schmorp.de> 1696 Marc Lehmann <schmorp@schmorp.de>
1697 http://home.schmorp.de/ 1697 http://home.schmorp.de/
1698 1698
1699=cut 1699=cut
1700 1700
17011 17011
1702 1702

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines