IO::Lambda::Socket(3)

πŸ“¦ version 1.34, in alpine 3.24

🌸 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 the IO::Socket or Socket modules. 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 $socket in a non-blocking listening state. Finishes either after a new connection arrives or after $deadline is expired. On success it returns a new socket serving the new connection; on failure it returns undef and an error string. That error string is either "timeout" or $!. See also "accept" in perlfunc.
  • connect($socket, $deadline=undef) -> (()|$error) β€” Expects a stream $socket in a non-blocking connect state. Finishes either after the connection succeeds or after $deadline is expired. On success it returns no parameters; on failure it returns an error string. That error string is either "timeout" or $! (or $^E on 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 $length bytes using the CORE::recv call. On success it returns the remote address (packed) and the received message. On failure it returns undef and 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 $msg using the CORE::send call. Depending on whether $to is defined or not, the 4-parameter or 3-parameter version of CORE::send is used. On success it returns the number of bytes sent. On failure it returns undef and an error string. That error string is either "timeout" or $!. See also "send" in perlfunc.

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