Lots of djb programs and related/similar utilities (e.g. sslclient) have interfaces that want data written to or read from specific file descriptors. (With sslclient: "sslclient runs prog, with file descriptors 6 and 7 reading from and writing to a child process").

Maybe I'm interpreting things incorrectly, but is there a way to open a file on a specific fd in Perl? I tried the following with open, but it seems to fail if the fd's not already open:

$ perl -lwe 'open STDOUT, ">&=6" or die ">&=6: $!"' >&=6: Bad file descriptor at -e line 1. $ perl -lwe 'open STDOUT, ">&=6" or die ">&=6: $!"' 6>/dev/null

The goal is to provide a similar interface, like:

# Don't use this - DOESN'T WORK open my $to_child, ">&=6" or die ">&=6: $!"; open my $from_child, "<&=7" or die "<&=7: $!"; die "fork:$!" if not defined(my $pid = fork); if ($pid) { print $to_child "data for child\n"; my $ret = <$from_child>; } else { exec { "sub-program" } "sub-program", @args; }

In reply to Open a file on a specific file descriptor? by benizi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.