The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Sys::Pipe - pipe2() in Perl


    use Fcntl;
    use Sys::Pipe;

    Sys::Pipe::pipe( my $r, my $w, O_NONBLOCK ) or die "pipe: $!";


Ever wish you could create a pipe that starts out non-blocking? Linux and a number of other OSes can do this via a proprietary pipe2() system call; this little library exposes that functionality to Perl.


As shown above, this exposes the ability to create a pipe that starts out non-blocking. If that’s all you need, then the gain here is mostly just tidiness. It is also faster than doing:

    pipe my $r, my $w or die "pipe: $!";

… but the above is already quite fast, so that may not make a real-world difference for you.

In Linux, this also exposes the ability to create a “packet mode” pipe. Other OSes may allow similar and/or other functionality. See your system’s pipe2(2) for more details.


This module is best considered EXPERIMENTAL. If you find a problem, please file a bug report. Thank you!


Perl’s socket() built-in allows similar functionality on the relevant OSes, e.g.:

    use Socket;

    socket( my $s, AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0 ) or do {
        die "socket(): $!";


$success_yn = pipe( READHANDLE, WRITEHANDLE [, FLAGS] )

A drop-in replacement for Perl’s pipe() built-in that optionally accepts a numeric FLAGS argument. See your system’s pipe2(2) documentation for what values you can pass in there.

Note that behavior is currently undefined if FLAGS is nonzero on any system (e.g., macOS) that lacks pipe2(). (As of this writing an exception is thrown; that may change eventually.)

$yn = has_pipe2()

Returns a boolean that indicates whether the underlying system can implement pipe2() mechanics.


Copyright 2020 Gasper Software Consulting. All rights reserved.