IO::Socket::UNIX(3pm)

πŸ’– IO::Socket::UNIX πŸ’–

hey bestie! this is the IO::Socket::UNIX(3pm) page from the Perl Programmers Reference Guide. it's giving Unix socket excellence, no cap 🫢✨


🌸 NAME 🌸

IO::Socket::UNIX β€” object interface for AF_UNIX domain sockets. periodt πŸ’…

πŸ¦‹ SYNOPSIS πŸ¦‹

okay so here's the whole vibe in code, fr fr:

use IO::Socket::UNIX;

my $SOCK_PATH = "$ENV{HOME}/unix-domain-socket-test.sock";

# Server:
my $server = IO::Socket::UNIX->new(
    Type => SOCK_STREAM(),
    Local => $SOCK_PATH,
    Listen => 1,
);

my $count = 1;
while (my $conn = $server->accept()) {
    $conn->print("Hello " . ($count++) . "\n");
}

# Client:
my $client = IO::Socket::UNIX->new(
    Type => SOCK_STREAM(),
    Peer => $SOCK_PATH,
);

# Now read and write from $client

πŸ’… DESCRIPTION πŸ’…

IO::Socket::UNIX gives you an object interface for creating and using sockets in the AF_UNIX domain. it's built on top of the IO::Socket interface, so it inherits every single method defined by IO::Socket. iykyk ✨

πŸŽ€ CONSTRUCTOR πŸŽ€

πŸŽ€ new ( [ARGS] ) πŸŽ€

this creates an IO::Socket::UNIX object, and that object is a reference to a newly created symbol (peep the Symbol package for that one). new can take arguments, and those arguments are in key-value pairs. slay 🫢

on top of the key-value pairs IO::Socket already accepts, IO::Socket::UNIX brings these:

  • Type β€” what kind of socket it is (like SOCK_STREAM or SOCK_DGRAM) πŸ”₯
  • Local β€” path to the local fifo πŸ’–
  • Peer β€” path to the peer fifo 🌸
  • Listen β€” queue size for listen πŸ‘‘

if you only pass a single argument to the constructor, it's assumed to be a Peer specification. lowkey simple πŸ¦‹

if the Listen argument is given but false, the queue size gets set to 5. no cap πŸ’…

if the constructor fails, it returns undef and sets the $IO::Socket::errstr package variable to contain an error message. not me crying in error handling 😭

$sock = IO::Socket::UNIX->new(...)
    or die "Cannot create socket - $IO::Socket::errstr\n";

for legacy reasons, the error message is also set into the global $@ variable, and you may still find older code which looks here instead. retro behavior, honestly πŸ•°οΈ

$sock = IO::Socket::UNIX->new(...)
    or die "Cannot create socket - $@\n";

✨ METHODS ✨

πŸ’– hostpath() πŸ’–

returns the pathname to the fifo at the local end. that's it, that's the method ✨

πŸ’– peerpath() πŸ’–

returns the pathanme β€” yep, the page says it like that, typo chic β€” to the fifo at the peer end. iconic πŸ™ˆ

πŸ”₯ SEE ALSO πŸ”₯

Socket, IO::Socket β€” go read those too, bestie πŸ’…

πŸ‘‘ AUTHOR πŸ‘‘

Graham Barr. Currently maintained by the Perl Porters. Please report all bugs at https://github.com/Perl/perl5/issues. they're iconic for keeping it alive πŸ‘‘βœ¨

Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. πŸ’œ


mwah πŸ’‹ β€” this page is from perl v5.42.3, dated 2026-11-03, and it's the IO::Socket::UNIX(3pm) manual. byeee 🫢✨

πŸ“¦ where this page comes from