IO::Pipely(3pm)

πŸ“¦
version 0.006 Β· 6 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

IO::Pipely β€” portably create pipe() or pipe-like handles, one way or another. 🫢


✨ VERSION ✨

version 0.006 β€” she's little, she's fierce, she's stable.


🌸 SYNOPSIS 🌸

bestie pls read DESCRIPTION for the detailed semantics and caveats, i'm begging πŸ™

use IO::Pipely qw(pipely socketpairly);

# Create a one-directional pipe() or pipe-like thing
# the best conduit type available.

my ($read, $write) = pipely();

# Create a one-directional pipe-like thing using an
# INET socket specifically.  Other types are available.

my ($read, $write) = pipely(type => 'inet');

# Create a bidirectional pipe-like thing using
# the best conduit type available.

my (
  $side_a_read, $side_a_write,
  $side_b_read, $side_b_write,
) = socketpairly();

# Create a bidirectional pipe-like thing using an INET socket
# specifically.

my (
  $side_a_read, $side_a_write,
  $side_b_read, $side_b_write,
) = socketpairly(type => 'inet');

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so pipes? troublesome beasts. 😭 genuinely. there are a few different, incompatible ways to create them, not all platforms support all the ways, and some platforms have hidden difficulties like incomplete or buggy support. it's giving chaos.

IO::Pipely hands you a couple functions that portably create one- and two-way pipes and pipe-like socket pairs. she acknowledges and works around known platform issues so you don't have to. main character energy fr fr πŸ‘‘

on the other hand β€” and this is important β€” she does not work around unknown issues. so please report any problems early and often. bestie. do it for her. πŸ’…

IO::Pipely currently understands pipe(), UNIX-domain socketpair() and regular IPv4 localhost sockets. that covers every platform tested so far, but it's hardly complete. please help support other mechanisms, such as INET-domain socketpair() and IPv6 localhost sockets. πŸ₯Ί

IO::Pipely will use different kinds of pipes or sockets depending on the operating system's capabilities and the number of directions requested. the autodetection may be overridden by specifying a particular pipe type. periodt.


🌈 pipely

pipely() creates a one-directional pipe() or socket. it's modeled after Perl's built-in pipe() function, but it creates and returns handles rather than opening ones given to it.

on success, pipely() returns two file handles β€” the first to read from the pipe, and the second writes into the pipe. it returns nothing on failure.

use IO::Pipely qw(pipely);
my ($a_read, $b_write) = pipely();
die "pipely() failed: $!" unless $a_read;

when given a choice, she will prefer to use leaner pipe() calls instead of socketpair() and socket().

pipely()'s choice can be forced using an optional named "type" parameter. see PIPE TYPES for the types that can be used.

my ($a_read, $b_write) = pipely(
  type => 'pipe',
);

on most systems, pipely() will prefer to open a pipe() first. it will fall back to a UNIX socketpair() or two localhost Internet sockets, in that order.

on Windows (ActiveState and Strawberry Perl), pipely() prefers two localhost Internet sockets. it will fall back to socketpair() and pipe(), both of which will probably fail. πŸ’€

Cygwin Perl prefers pipe() first, localhost Internet sockets, and then socketpair(). socketpair() has been known to have problems on Cygwin. πŸ™ˆ

MacPerl (MacOS 9 and earlier) has similar capaibilities to Windows.


🧚 socketpairly

socketpairly() creates a two-directional socket pair. it's modeled after Perl's built-in socketpair(), but it creates and returns handles rather than opening ones given to it.

on success, socketpairly() returns four file handles β€” read and write for one end, and read and write for the other. on failure, it returns nothing.

use IO::Pipely qw(socketpairly);
my ($a_read, $a_write, $b_read, $b_write) = socketpairly();
die "socketpairly() failed: $!" unless $a_read;

socketpairly() returns two extra "writer" handles. they exist for the fallback case where two pipe() calls are needed instead of one socket pair. the extra handles can be ignored whenever pipe() will never be used. for example:

use IO::Pipely qw(socketpairly);
my ($side_a, undef, $side_b, undef) = socketpairly( type => 'socketpair' );
die "socketpairly() failed: $!" unless $side_a;

when given a choice, she will prefer bidirectional sockets instead of pipe() calls.

socketpairly()'s choice can be forced using an optional named "type" parameter. see PIPE TYPES for the types that can be used. in this example, two unidirectional pipes wil be used instead of a more efficient pair of sockets:

my ($a_read, $a_write, $b_read, $b_write) = socketpairly(
  type => 'pipe',
);

on most systems, socketpairly() will try to open a UNIX socketpair() first. it will then fall back to a pair of localhost Internet sockets, and finally it will try a pair of pipe() calls.

on Windows (ActiveState and Strawberry Perl), socketpairly() prefers a pair of localhost Internet sockets first. it will then fall back to a UNIX socketpair(), and finally a couple of pipe() calls. the fallback options will probably fail, but the code remains hopeful. πŸ₯Ίβœ¨

Cygwin Perl prefers localhost Internet sockets first, followed by a pair of pipe() calls, and finally a UNIX socketpair(). those who know may find this counter-intuitive, but it works around known issues in some versions of Cygwin socketpair().

MacPerl (MacOS 9 and earlier) has similar capaibilities to Windows.


πŸ‘‘ PIPE TYPES

IO::Pipely currently supports three types of pipe and socket. other types are possible, but these three cover all known uses so far. please ask (or send patches) if additional types are needed. 🫢

πŸŽ€ pipe

attempt to establish a one-way pipe using one pipe() filehandle pair (2 file descriptors), or a two-way pipe-like connection using two pipe() pairs (4 file descriptors).

IO::Pipely prefers to use pipe() for one-way pipes and some form of socket pair for two-way pipelike things.

πŸŽ€ socketpair

attempt to establish a one- or two-way pipelike connection using a single socketpair() call. this uses two file descriptors regardless whether the connection is one- or two-way.

IO::Pipely prefers socketpair() for two-way connections, unless the current platform has known issues with the socketpair() call.

socket pairs are UNIX domain only for now. INET domain may be added if it improves compatibility on some platform, or if someone contributes the code. πŸ‘€

πŸŽ€ inet

attempt to establish a one- or two-way pipelike connection using localhost socket() calls. this uses two file descriptors regardless whether the connection is one- or two-way.

localhost INET domain sockets are a last resort for platforms that don't support something better. they are the least secure method of communication since tools like tcpdump and Wireshark can tap into them. on the other hand, this makes them easiest to debug. ✨


πŸ’€ KNOWN ISSUES πŸ’€

these are issues known to the developers at the time of this writing. things change, so check back now and then. πŸ•°οΈ


😭 Cygwin

CygWin seems to have a problem with socketpair() and exec(). when an exec'd process closes, any data on sockets created with socketpair() is not flushed. from irc.perl.org channel #poe:

  <dngnand>   Sounds like a lapse in cygwin's exec implementation.
              It works ok under Unix-ish systems?
  <jdeluise2> yes, it works perfectly
  <jdeluise2> but, if we just use POE::Pipe::TwoWay->new("pipe")
              it always works fine on cygwin
  <jdeluise2> by the way, it looks like the reason is that
              POE::Pipe::OneWay works because it tries to make a
              pipe first instead of a socketpair
  <jdeluise2> this socketpair problem seems like a long-standing
              one with cygwin, according to searches on google,
              but never been fixed.

😭 MacOS 9

IO::Pipely supports MacOS 9 for historical reasons. it's unclear whether anyone still uses MacPerl, but the support is cheap since pipes and sockets there have many of the same caveats as they do on Windows.


😭 Symbol::gensym

IO::Pipely uses Symbol::gensym() instead of autovivifying file handles. the main reasons against gensym() have been stylistic ones so far. meanwhile, gensym() is compatible farther back than handle autovivification.


😭 Windows

ActiveState and Strawberry Perl don't support pipe() or UNIX socketpair(). localhost Internet sockets are used for everything there, including one-way pipes.

for one-way pipes, the unused socket directions are shut down to avoid sending data the wrong way through them. use socketpairly() instead. πŸ’…


πŸ™ˆ BUGS πŸ™ˆ

the functions implemented here die outright upon failure, requiring eval{} around their calls. dramatic? yes. sorry bestie. 😭

the following conduit types are currently unsupported because nobody has needed them so far. please submit a request (and/or a patch) if any of these is needed:

UNIX socket()
INET-domain socketpair()
IPv4-specific localhost sockets
IPv6-specific localhost sockets

IO::Pipely is copyright 2000-2021 by Rocco Caputo. all rights reserved. IO::Pipely is free software; you may redistribute it and/or modify it under the same terms as Perl itself. slay. πŸ‘‘


🌈 HISTORY 🌈

IO::Pipely is a spin-off of the POE project's portable pipes. earlier versions of the code have been tested and used in production systems for over a decade. iconic behavior fr fr. ⭐


okay that's the whole page bestie, and just so you know β€” this was rendered by perl v5.32.1 on 2021-12-18, the IO::Pipely(3pm) era πŸ’– mwah, go make some pipes, you've got this πŸ«Άβœ¨πŸŽ€

πŸ“¦ where this page comes from