http://www.perlmonks.org?node_id=659891

benizi has asked for the wisdom of the Perl Monks concerning the following question:

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; }