=head1 NAME
AnyEvent::Util - various utility functions.
=head1 SYNOPSIS
use AnyEvent::Util;
=head1 DESCRIPTION
This module implements various utility functions, mostly replacing
well-known functions by event-ised counterparts.
All functions documented without C<AnyEvent::Util::> prefix are exported
by default.
=over 4
=cut
package AnyEvent::Util;
no warnings;
use strict;
use Carp ();
use Errno ();
use Socket ();
use AnyEvent ();
use base 'Exporter';
our @EXPORT = qw(fh_nonblocking guard fork_call portable_pipe);
our @EXPORT_OK = qw(AF_INET6 WSAEWOULDBLOCK WSAEINPROGRESS WSAEINVAL WSAWOULDBLOCK);
our $VERSION = 4.151;
BEGIN {
my $posix = 1 * eval { local $SIG{__DIE__}; require POSIX };
eval "sub POSIX() { $posix }";
}
BEGIN {
# TODO remove this once not used anymore
*socket_inet_aton = \&Socket::inet_aton; # take a copy, in case Coro::LWP overrides it
}
BEGIN {
my $af_inet6 = eval { local $SIG{__DIE__}; &Socket::AF_INET6 };
# uhoh
$af_inet6 ||= 10 if $^O =~ /linux/;
$af_inet6 ||= 23 if $^O =~ /cygwin/i;
$af_inet6 ||= 23 if AnyEvent::WIN32;
$af_inet6 ||= 24 if $^O =~ /openbsd|netbsd/;
$af_inet6 ||= 28 if $^O =~ /freebsd/;
$af_inet6 && socket my $ipv6_socket, $af_inet6, &Socket::SOCK_STREAM, 0 # check if they can be created
or $af_inet6 = 0;
eval "sub AF_INET6() { $af_inet6 }"; die if $@;
delete $AnyEvent::PROTOCOL{ipv6} unless $af_inet6;
}
BEGIN {
# broken windows perls use undocumented error codes...
if (AnyEvent::WIN32) {
eval "sub WSAEINVAL() { 10022 }";
eval "sub WSAEWOULDBLOCK() { 10035 }";
eval "sub WSAWOULDBLOCK() { 10035 }"; # TODO remove here ands from @export_ok
eval "sub WSAEINPROGRESS() { 10036 }";
} else {
# these should never match any errno value
eval "sub WSAEINVAL() { -1e99 }";
eval "sub WSAEWOULDBLOCK() { -1e99 }";
eval "sub WSAWOULDBLOCK() { -1e99 }"; # TODO
eval "sub WSAEINPROGRESS() { -1e99 }";
}
}
=item ($r, $w) = portable_pipe
Calling C<pipe> in Perl is portable - except it doesn't really work on
sucky windows platforms (at least not with most perls - cygwin's perl
notably works fine).
On that platform, you actually get two file handles you cannot use select
on.
This function gives you a pipe that actually works even on the broken
Windows platform (by creating a pair of TCP sockets, so do not expect any
speed from that).
Returns the empty list on any errors.
=cut
sub portable_pipe() {
=1= |