[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ale] Redirecting STDOUT in perl to a socket
- Subject: [ale] Redirecting STDOUT in perl to a socket
- From: fletch at phydeaux.org (Fletch)
- Date: Wed May 12 10:13:57 2004
- In-reply-to: <[email protected]> (Christopher Fowler's message of "Wed, 12 May 2004 01:45:03 -0400")
- References: <[email protected]>
>>>>> "Christopher" == Christopher Fowler <cfowler at outpostsentinel.com> writes:
Christopher> I've created a socket from a client usins: $client =
Christopher> $server->accept();
Christopher> I'm trying to redirect 0,1,2 with the following code:
Christopher> *STDIN = $client; *STDOUT = $client; *STDERR =
Christopher> *STDOUT;
You don't just assign handles this way. You need to use open() (which
will actually do a dup2(2) on the fds underneath). The usual idiom
would be something like this:
open( SAVEOUT, "<&STDOUT" ) or warn "can't save stdout: $!\n";
open( STDOUT, "<&", $client ) or warn "can't dup to stdout: $!\n";
##
## ... yadda yadda yadda
##
open( STDOUT, "<&SAVEOUT" ) or warn "can't restore stdout: $!\n";
If you don't have a proper handle you can use "<&=" and the file
descriptor itself (e.g. $fd = fileno( $fh ); open( A, "<&=$fd" )).
See perldoc perlopentut and perldoc -f open for more details. You
might also check out IPC::Run, as I think it provides a shorthand to
run a child with things redirected to specific handles.
--
Fletch | "If you find my answers frightening, __`'/|
fletch at phydeaux.org | Vincent, you should cease askin' \ o.O'
| scary questions." -- Jules =(___)=
| U