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.362 by root, Sun Aug 14 01:57:18 2011 UTC vs.
Revision 1.367 by root, Wed Aug 17 02:14:17 2011 UTC

1049It should use C<postpone>: 1049It should use C<postpone>:
1050 1050
1051 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later 1051 AnyEvent::postpone { $cb->(undef) }, return # signal error to callback, later
1052 if $some_error_condition; 1052 if $some_error_condition;
1053 1053
1054=item AnyEvent::log $level, $msg[, @args]
1055
1056Log the given C<$msg> at the given C<$level>.
1057
1058Loads AnyEvent::Log on first use and calls C<AnyEvent::Log::log> -
1059consequently, look at the L<AnyEvent::Log> documentation for details.
1060
1061If you want to sprinkle loads of logging calls around your code, consider
1062creating a logger callback with the C<AnyEvent::Log::logger< function.
1063
1054=back 1064=back
1055 1065
1056=head1 WHAT TO DO IN A MODULE 1066=head1 WHAT TO DO IN A MODULE
1057 1067
1058As a module author, you should C<use AnyEvent> and call AnyEvent methods 1068As a module author, you should C<use AnyEvent> and call AnyEvent methods
1224 1234
1225 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV} 1235 delete @ENV{grep /^PERL_ANYEVENT_/, keys %ENV}
1226 if ${^TAINT}; 1236 if ${^TAINT};
1227 1237
1228 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1; 1238 $VERBOSE = $ENV{PERL_ANYEVENT_VERBOSE}*1;
1229
1230} 1239}
1231 1240
1232our $MAX_SIGNAL_LATENCY = 10; 1241our $MAX_SIGNAL_LATENCY = 10;
1233 1242
1234our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred 1243our %PROTOCOL; # (ipv4|ipv6) => (1|2), higher numbers are preferred
1270 push @POSTPONE, shift; 1279 push @POSTPONE, shift;
1271 1280
1272 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec); 1281 $POSTPONE_W ||= AE::timer (0, 0, \&_postpone_exec);
1273 1282
1274 () 1283 ()
1284}
1285
1286sub log($$;@) {
1287 require AnyEvent::Log;
1288 # AnyEvent::Log overwrites this function
1289 goto &log;
1275} 1290}
1276 1291
1277our @models = ( 1292our @models = (
1278 [EV:: => AnyEvent::Impl::EV:: , 1], 1293 [EV:: => AnyEvent::Impl::EV:: , 1],
1279 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1], 1294 [AnyEvent::Loop:: => AnyEvent::Impl::Perl:: , 1],
1318# all autoloaded methods reserve the complete glob, not just the method slot. 1333# all autoloaded methods reserve the complete glob, not just the method slot.
1319# due to bugs in perls method cache implementation. 1334# due to bugs in perls method cache implementation.
1320our @methods = qw(io timer time now now_update signal child idle condvar); 1335our @methods = qw(io timer time now now_update signal child idle condvar);
1321 1336
1322sub detect() { 1337sub detect() {
1338 return $MODEL if $MODEL; # some programs keep references to detect
1339
1323 local $!; # for good measure 1340 local $!; # for good measure
1324 local $SIG{__DIE__}; # we use eval 1341 local $SIG{__DIE__}; # we use eval
1325 1342
1326 # free some memory 1343 # free some memory
1327 *detect = sub () { $MODEL }; 1344 *detect = sub () { $MODEL };
1336 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) { 1353 if ($ENV{PERL_ANYEVENT_MODEL} =~ /^([a-zA-Z0-9:]+)$/) {
1337 my $model = $1; 1354 my $model = $1;
1338 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//; 1355 $model = "AnyEvent::Impl::$model" unless $model =~ s/::$//;
1339 if (eval "require $model") { 1356 if (eval "require $model") {
1340 $MODEL = $model; 1357 $MODEL = $model;
1341 warn "AnyEvent: loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it.\n" if $VERBOSE >= 2; 1358 AnyEvent::log 7 => "loaded model '$model' (forced by \$ENV{PERL_ANYEVENT_MODEL}), using it."
1359 if $VERBOSE >= 7;
1342 } else { 1360 } else {
1343 warn "AnyEvent: unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@" if $VERBOSE; 1361 AnyEvent::log warn => "unable to load model '$model' (from \$ENV{PERL_ANYEVENT_MODEL}):\n$@";
1344 } 1362 }
1345 } 1363 }
1346 1364
1347 # check for already loaded models 1365 # check for already loaded models
1348 unless ($MODEL) { 1366 unless ($MODEL) {
1349 for (@REGISTRY, @models) { 1367 for (@REGISTRY, @models) {
1350 my ($package, $model) = @$_; 1368 my ($package, $model) = @$_;
1351 if (${"$package\::VERSION"} > 0) { 1369 if (${"$package\::VERSION"} > 0) {
1352 if (eval "require $model") { 1370 if (eval "require $model") {
1353 $MODEL = $model; 1371 $MODEL = $model;
1354 warn "AnyEvent: autodetected model '$model', using it.\n" if $VERBOSE >= 2; 1372 AnyEvent::log 7 => "autodetected model '$model', using it."
1373 if $VERBOSE >= 7;
1355 last; 1374 last;
1356 } 1375 }
1357 } 1376 }
1358 } 1377 }
1359 1378
1366 and eval "require $package" 1385 and eval "require $package"
1367 and ${"$package\::VERSION"} > 0 1386 and ${"$package\::VERSION"} > 0
1368 and eval "require $model" 1387 and eval "require $model"
1369 ) { 1388 ) {
1370 $MODEL = $model; 1389 $MODEL = $model;
1371 warn "AnyEvent: autoloaded model '$model', using it.\n" if $VERBOSE >= 2; 1390 AnyEvent::log 7 => "autoloaded model '$model', using it."
1391 if $VERBOSE >= 7;
1372 last; 1392 last;
1373 } 1393 }
1374 } 1394 }
1375 1395
1376 $MODEL 1396 $MODEL
1377 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?\n"; 1397 or die "AnyEvent: backend autodetection failed - did you properly install AnyEvent?";
1378 } 1398 }
1379 } 1399 }
1380 1400
1381 # free memory only needed for probing 1401 # free memory only needed for probing
1382 undef @models; 1402 undef @models;
1391 if defined &{"$MODEL\::$_"}; 1411 if defined &{"$MODEL\::$_"};
1392 } 1412 }
1393 1413
1394 _isa_set; 1414 _isa_set;
1395 1415
1416 # we're officially open!
1417
1396 if ($ENV{PERL_ANYEVENT_STRICT}) { 1418 if ($ENV{PERL_ANYEVENT_STRICT}) {
1397 require AnyEvent::Strict; 1419 require AnyEvent::Strict;
1398 } 1420 }
1399 1421
1400 if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) { 1422 if ($ENV{PERL_ANYEVENT_DEBUG_WRAP}) {
1401 require AnyEvent::Debug; 1423 require AnyEvent::Debug;
1402 AnyEvent::Debug::wrap ($ENV{PERL_ANYEVENT_DEBUG_WRAP}); 1424 AnyEvent::Debug::wrap ($ENV{PERL_ANYEVENT_DEBUG_WRAP});
1403 } 1425 }
1404 1426
1405 if (exists $ENV{PERL_ANYEVENT_DEBUG_SHELL}) { 1427 if (length $ENV{PERL_ANYEVENT_DEBUG_SHELL}) {
1406 require AnyEvent::Socket; 1428 require AnyEvent::Socket;
1407 require AnyEvent::Debug; 1429 require AnyEvent::Debug;
1408 1430
1409 my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL}; 1431 my $shell = $ENV{PERL_ANYEVENT_DEBUG_SHELL};
1410 $shell =~ s/\$\$/$$/g; 1432 $shell =~ s/\$\$/$$/g;
1411 1433
1412 my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell); 1434 my ($host, $service) = AnyEvent::Socket::parse_hostport ($shell);
1413 $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service); 1435 $AnyEvent::Debug::SHELL = AnyEvent::Debug::shell ($host, $service);
1414 } 1436 }
1437
1438 # now the anyevent environment is set up as the user told us to, so
1439 # call the actual user code - post detects
1415 1440
1416 (shift @post_detect)->() while @post_detect; 1441 (shift @post_detect)->() while @post_detect;
1417 undef @post_detect; 1442 undef @post_detect;
1418 1443
1419 *post_detect = sub(&) { 1444 *post_detect = sub(&) {
1506 sub time() { 1531 sub time() {
1507 AnyEvent->time 1532 AnyEvent->time
1508 } 1533 }
1509 1534
1510 *postpone = \&AnyEvent::postpone; 1535 *postpone = \&AnyEvent::postpone;
1536 *log = \&AnyEvent::log;
1511 }; 1537 };
1512 die if $@; 1538 die if $@;
1513} 1539}
1514 1540
1515BEGIN { _reset } 1541BEGIN { _reset }
1520 1546
1521sub time { 1547sub time {
1522 eval q{ # poor man's autoloading {} 1548 eval q{ # poor man's autoloading {}
1523 # probe for availability of Time::HiRes 1549 # probe for availability of Time::HiRes
1524 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") { 1550 if (eval "use Time::HiRes (); Time::HiRes::time (); 1") {
1525 warn "AnyEvent: using Time::HiRes for sub-second timing accuracy.\n" if $VERBOSE >= 8; 1551 AnyEvent::log 8 => "AnyEvent: using Time::HiRes for sub-second timing accuracy."
1552 if $AnyEvent::VERBOSE >= 8;
1526 *time = sub { Time::HiRes::time () }; 1553 *time = sub { Time::HiRes::time () };
1527 *AE::time = \& Time::HiRes::time ; 1554 *AE::time = \& Time::HiRes::time ;
1528 # if (eval "use POSIX (); (POSIX::times())... 1555 # if (eval "use POSIX (); (POSIX::times())...
1529 } else { 1556 } else {
1530 warn "AnyEvent: using built-in time(), WARNING, no sub-second resolution!\n" if $VERBOSE; 1557 AnyEvent::log critical => "using built-in time(), WARNING, no sub-second resolution!";
1531 *time = sub { CORE::time }; 1558 *time = sub { CORE::time };
1532 *AE::time = sub (){ CORE::time }; 1559 *AE::time = sub (){ CORE::time };
1533 } 1560 }
1534 1561
1535 *now = \&time; 1562 *now = \&time;
1633 1660
1634sub signal { 1661sub signal {
1635 eval q{ # poor man's autoloading {} 1662 eval q{ # poor man's autoloading {}
1636 # probe for availability of Async::Interrupt 1663 # probe for availability of Async::Interrupt
1637 if (_have_async_interrupt) { 1664 if (_have_async_interrupt) {
1638 warn "AnyEvent: using Async::Interrupt for race-free signal handling.\n" if $VERBOSE >= 8; 1665 AnyEvent::log 8 => "using Async::Interrupt for race-free signal handling."
1666 if $AnyEvent::VERBOSE >= 8;
1639 1667
1640 $SIGPIPE_R = new Async::Interrupt::EventPipe; 1668 $SIGPIPE_R = new Async::Interrupt::EventPipe;
1641 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec; 1669 $SIG_IO = AE::io $SIGPIPE_R->fileno, 0, \&_signal_exec;
1642 1670
1643 } else { 1671 } else {
1644 warn "AnyEvent: using emulated perl signal handling with latency timer.\n" if $VERBOSE >= 8; 1672 AnyEvent::log 8 => "using emulated perl signal handling with latency timer."
1673 if $AnyEvent::VERBOSE >= 8;
1645 1674
1646 if (AnyEvent::WIN32) { 1675 if (AnyEvent::WIN32) {
1647 require AnyEvent::Util; 1676 require AnyEvent::Util;
1648 1677
1649 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe (); 1678 ($SIGPIPE_R, $SIGPIPE_W) = AnyEvent::Util::portable_pipe ();
1958 1987
1959By default, AnyEvent will be completely silent except in fatal 1988By default, AnyEvent will be completely silent except in fatal
1960conditions. You can set this environment variable to make AnyEvent more 1989conditions. You can set this environment variable to make AnyEvent more
1961talkative. 1990talkative.
1962 1991
1963When set to C<1> or higher, causes AnyEvent to warn about unexpected 1992When set to C<5> or higher, causes AnyEvent to warn about unexpected
1964conditions, such as not being able to load the event model specified by 1993conditions, such as not being able to load the event model specified by
1965C<PERL_ANYEVENT_MODEL>. 1994C<PERL_ANYEVENT_MODEL>.
1966 1995
1967When set to C<2> or higher, cause AnyEvent to report to STDERR which event 1996When set to C<7> or higher, cause AnyEvent to report to STDERR which event
1968model it chooses. 1997model it chooses.
1969 1998
1970When set to C<8> or higher, then AnyEvent will report extra information on 1999When set to C<8> or higher, then AnyEvent will report extra information on
1971which optional modules it loads and how it implements certain features. 2000which optional modules it loads and how it implements certain features.
1972 2001
1995This takes place when the first watcher is created. 2024This takes place when the first watcher is created.
1996 2025
1997For example, to bind a debug shell on a unix domain socket in 2026For example, to bind a debug shell on a unix domain socket in
1998F<< /tmp/debug<pid>.sock >>, you could use this: 2027F<< /tmp/debug<pid>.sock >>, you could use this:
1999 2028
2000 PERL_ANYEVENT_DEBUG_SHELL=unix/:/tmp/debug\$\$.sock perlprog 2029 PERL_ANYEVENT_DEBUG_SHELL=/tmp/debug\$\$.sock perlprog
2001 2030
2002Note that creating sockets in F</tmp> is very unsafe on multiuser 2031Note that creating sockets in F</tmp> is very unsafe on multiuser
2003systems. 2032systems.
2004 2033
2005=item C<PERL_ANYEVENT_DEBUG_WRAP> 2034=item C<PERL_ANYEVENT_DEBUG_WRAP>
2805 2834
2806Tutorial/Introduction: L<AnyEvent::Intro>. 2835Tutorial/Introduction: L<AnyEvent::Intro>.
2807 2836
2808FAQ: L<AnyEvent::FAQ>. 2837FAQ: L<AnyEvent::FAQ>.
2809 2838
2810Utility functions: L<AnyEvent::Util>. 2839Utility functions: L<AnyEvent::Util> (misc. grab-bag), L<AnyEvent::Log>
2840(simply logging).
2811 2841
2842Development/Debugging: L<AnyEvent::Strict> (stricter checking),
2843L<AnyEvent::Debug> (interactive shell, watcher tracing).
2844
2812Event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>, L<Glib::EV>, 2845Supported event modules: L<AnyEvent::Loop>, L<EV>, L<EV::Glib>,
2813L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>, L<Qt>, L<POE>. 2846L<Glib::EV>, L<Event>, L<Glib::Event>, L<Glib>, L<Tk>, L<Event::Lib>,
2847L<Qt>, L<POE>, L<FLTK>.
2814 2848
2815Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>, 2849Implementations: L<AnyEvent::Impl::EV>, L<AnyEvent::Impl::Event>,
2816L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>, 2850L<AnyEvent::Impl::Glib>, L<AnyEvent::Impl::Tk>, L<AnyEvent::Impl::Perl>,
2817L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>, 2851L<AnyEvent::Impl::EventLib>, L<AnyEvent::Impl::Qt>,
2818L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>. 2852L<AnyEvent::Impl::POE>, L<AnyEvent::Impl::IOAsync>, L<Anyevent::Impl::Irssi>,
2853L<AnyEvent::Impl::FLTK>.
2819 2854
2820Non-blocking file handles, sockets, TCP clients and 2855Non-blocking handles, pipes, stream sockets, TCP clients and
2821servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>. 2856servers: L<AnyEvent::Handle>, L<AnyEvent::Socket>, L<AnyEvent::TLS>.
2822 2857
2823Asynchronous DNS: L<AnyEvent::DNS>. 2858Asynchronous DNS: L<AnyEvent::DNS>.
2824 2859
2825Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>. 2860Thread support: L<Coro>, L<Coro::AnyEvent>, L<Coro::EV>, L<Coro::Event>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines