--- AnyEvent/lib/AnyEvent.pm 2008/05/10 00:45:18 1.109 +++ AnyEvent/lib/AnyEvent.pm 2008/05/10 01:04:42 1.112 @@ -551,12 +551,16 @@ have created an AnyEvent watcher anyway, that is, as late as possible at runtime. -=item AnyEvent::on_detect { BLOCK } +=item $guard = AnyEvent::post_detect { BLOCK } Arranges for the code block to be executed as soon as the event model is autodetected (or immediately if this has already happened). -=item @AnyEvent::on_detect +If called in scalar or list context, then it creates and returns an object +that automatically removes the callback again when it is destroyed. See +L for a case where this is useful. + +=item @AnyEvent::post_detect If there are any code references in this array (you can C to it before or after loading AnyEvent), then they will called directly after @@ -566,7 +570,7 @@ if it contains a true value then the event loop has already been detected, and the array will be ignored. -Best use C instead. +Best use C instead. =back @@ -718,16 +722,28 @@ our %method = map +($_ => 1), qw(io timer signal child condvar one_event DESTROY); -our @on_detect; +our @post_detect; + +sub post_detect(&) { + my ($cb) = @_; -sub on_detect(&) { if ($MODEL) { - $_[0]->(); + $cb->(); + + 1 } else { - push @on_detect, $_[0]; + push @post_detect, $cb; + + defined wantarray + ? bless \$cb, "AnyEvent::Util::Guard" + : () } } +sub AnyEvent::Util::Guard::DESTROY { + @post_detect = grep $_ != ${$_[0]}, @post_detect; +} + sub detect() { unless ($MODEL) { no strict 'refs'; @@ -777,7 +793,7 @@ unshift @ISA, $MODEL; push @{"$MODEL\::ISA"}, "AnyEvent::Base"; - (shift @on_detect)->() while @on_detect; + (shift @post_detect)->() while @post_detect; } $MODEL