Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

working with files by number

by flipper (Beadle)
on Mar 24, 2009 at 14:18 UTC ( [id://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...

Thanks!

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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-24 12:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found