Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

working with files by number

by flipper (Beadle)
on Mar 24, 2009 at 14:18 UTC ( #752882=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks,

I'm using fork() exec() and socketpair() to start a child and capture its output, something like
socketpair($r_stdout, $w_stdout, AF_UNIX, SOCK_STREAM, PF_UNSPEC); socketpair($r_stderr, $w_stderr, AF_UNIX, SOCK_STREAM, PF_UNSPEC); if(fork()){ close $w_stdout; #read from $r_stdout etc }else{ my $so_fd=fileno($w_stdout); open STDOUT, ">&$so_fd"; exec('something') or die; }

The problem I have is I have to exec and capture the output of a script which writes to FD3, something like perl -e 'open STDOUT, ">&3";print  "hello, world\n"'
so I'd like to open fd3 to one half of a socketpair - is this possible? The loose equivalent of bash exec 3>/tmp/z

I guess I'd have to be careful not to clobber any of my socketpairs...


Replies are listed 'Best First'.
Re: working with files by number
by flipper (Beadle) on Mar 24, 2009 at 16:42 UTC
    got it...
    if(fork()){ #read }else{ my $so_fd=fileno($w_stdout); my $se_fd=fileno($w_stderr); my $si_fd=fileno($r_stdin); # # fd3 must be $w_fd3, so backup current fd3, and reopen 3 to $w_fd3 # my $old3=dup(3); my $new3=dup2(fileno($w_fd3), 3); # # then if any of the other fd's we were going to use was 3, change it +to the backup fd # for my $i ($so_fd, $se_fd, $si_fd){ if ($i == 3){ $i=$old3; } } open STDOUT, ">&$so_fd"; open STDERR, ">&$se_fd"; open STDIN, "<&$si_fd";
      For the interested, dup and dup2 are located in POSIX.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://752882]
Approved by Corion
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2020-02-22 22:33 GMT
Find Nodes?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?

    Results (102 votes). Check out past polls.