IO::Lambda::Socket(3)
πΈ NAME πΈ
Okay so this is the IO::Lambda::Socket(3) page from the User Contributed Perl Documentation, and the whole vibe is IO::Lambda::Socket β wrapper condition for socket functions. That's the name, bestie, and it's giving exactly what it says on the tin π
β¨
β¨ DESCRIPTION β¨
This module gives you a set of convenient wrapper conditions for sockets that work as sources of asynchronous events. The condition names are the same words as the underlying socket functions: accept, connect, recv, and send β iykyk π
Heads up: this module doesn't handle much of the lower-level socket machinery for you. You, the programmer, are expected to create non-blocking sockets using theIO::SocketorSocketmodules. No cap, that part is yours. π«Ά
π¦ SYNOPSIS π¦
use IO::Socket;
use IO::Lambda qw(:all);
use IO::Lambda::Socket qw(:all);
TCP
my $server = IO::Socket::INET-> new(
Listen => 5,
LocalPort => 10000,
Blocking => 0,
ReuseAddr => 1,
);
die $! unless $server;
my $serv = lambda {
context $server;
accept {
my $conn = shift;
die "error:$conn\n" unless ref($conn);
again;
context getline, $conn, \(my $buf = '');
tail {
next unless defined $_[0];
print "you said: $_[0]";
again;
}}
};
sub connector
{
my $id = shift;
lambda {
my $client = IO::Socket::INET-> new(
PeerAddr => 'localhost',
PeerPort => 10000,
Blocking => 0,
);
context $client;
connect {
die "error:$_[0]\n" if @_;
print $client "hello from $id\n";
}}
}
$serv-> wait_for_all( map { connector($_) } 1..5);
UDP
my $server = IO::Socket::INET-> new(
LocalPort => 10000,
Blocking => 0,
Proto => 'udp',
);
die $! unless $server;
my $serv = lambda {
context $server, 256;
recv {
my ($addr, $msg) = @_;
my ($port, $iaddr) = sockaddr_in($addr);
my $host = inet_ntoa($iaddr);
die "error:$msg\n" unless defined $addr;
print "udp_recv($host:$port): $msg\n";
again;
}
};
sub connector
{
my $id = shift;
lambda {
my $client = IO::Socket::INET-> new(
PeerAddr => 'localhost',
PeerPort => 10000,
Proto => 'udp',
Blocking => 0,
);
context $client, "hello from $id";
send {
die "send error:$_[1]\n" unless $_[0];
}}
}
$serv-> wait_for_all( map { connector($_) } 1..3);
π₯ API π₯
accept($socket, $deadline=undef) -> ($new_socket | undef,$error)β Expects a stream$socketin a non-blocking listening state. Finishes either after a new connection arrives or after$deadlineis expired. On success it returns a new socket serving the new connection; on failure it returnsundefand an error string. That error string is either"timeout"or$!. See also"accept" in perlfunc.connect($socket, $deadline=undef) -> (()|$error)β Expects a stream$socketin a non-blocking connect state. Finishes either after the connection succeeds or after$deadlineis expired. On success it returns no parameters; on failure it returns an error string. That error string is either"timeout"or$!(or$^Eon win32). See also"connect" in perlfunc.recv($socket, $length, $flags=0, $deadline=undef) -> ($addr,$msg | undef,$error)β Expects a non-blocking datagram$socket. After the socket becomes readable, it tries to read$lengthbytes using theCORE::recvcall. On success it returns the remote address (packed) and the received message. On failure it returnsundefand an error string. That error string is either"timeout"or$!. See also"recv" in perlfunc.send($socket, $msg, $flags, $to=undef, $deadline=undef) -> ($nbytes | undef,$error)β Expects a non-blocking datagram$socket. After the socket becomes writable, it tries to write$msgusing theCORE::sendcall. Depending on whether$tois defined or not, the 4-parameter or 3-parameter version ofCORE::sendis used. On success it returns the number of bytes sent. On failure it returnsundefand an error string. That error string is either"timeout"or$!. See also"send" in perlfunc.
π LICENSE AND COPYRIGHT π
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Legal bestie slay. β¨
π AUTHOR π
Dmitry Karasik, <dmitry@karasik.eu.org>. Iconic author behavior. π
That's the whole page, bestie! This was IO::Lambda::Socket(3) from perl v5.42.2, dated 2026-02-15 β now go make those sockets non-blocking and iconic π«Άπβ¨
π¦ where this page comes from
- alpine 3.24: perl-io-lambda-doc 1.34-r1 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms