… | |
… | |
879 | event module detection too early, for example, L<AnyEvent::AIO> creates |
879 | event module detection too early, for example, L<AnyEvent::AIO> creates |
880 | and installs the global L<IO::AIO> watcher in a C<post_detect> block to |
880 | and installs the global L<IO::AIO> watcher in a C<post_detect> block to |
881 | avoid autodetecting the event module at load time. |
881 | avoid autodetecting the event module at load time. |
882 | |
882 | |
883 | If called in scalar or list context, then it creates and returns an object |
883 | If called in scalar or list context, then it creates and returns an object |
884 | that automatically removes the callback again when it is destroyed. See |
884 | that automatically removes the callback again when it is destroyed (or |
|
|
885 | C<undef> when the hook was immediately executed). See L<AnyEvent::AIO> for |
885 | L<Coro::BDB> for a case where this is useful. |
886 | a case where this is useful. |
|
|
887 | |
|
|
888 | Example: Create a watcher for the IO::AIO module and store it in |
|
|
889 | C<$WATCHER>. Only do so after the event loop is initialised, though. |
|
|
890 | |
|
|
891 | our WATCHER; |
|
|
892 | |
|
|
893 | my $guard = AnyEvent::post_detect { |
|
|
894 | $WATCHER = AnyEvent->io (fh => IO::AIO::poll_fileno, poll => 'r', cb => \&IO::AIO::poll_cb); |
|
|
895 | }; |
|
|
896 | |
|
|
897 | # the ||= is important in case post_detect immediately runs the block, |
|
|
898 | # as to not clobber the newly-created watcher. assigning both watcher and |
|
|
899 | # post_detect guard to the same variable has the advantage of users being |
|
|
900 | # able to just C<undef $WATCHER> if the watcher causes them grief. |
|
|
901 | |
|
|
902 | $WATCHER ||= $guard; |
886 | |
903 | |
887 | =item @AnyEvent::post_detect |
904 | =item @AnyEvent::post_detect |
888 | |
905 | |
889 | If there are any code references in this array (you can C<push> to it |
906 | If there are any code references in this array (you can C<push> to it |
890 | before or after loading AnyEvent), then they will called directly after |
907 | before or after loading AnyEvent), then they will called directly after |