[ale] Redirecting STDOUT in perl to a socket

Fletch fletch at phydeaux.org
Wed May 12 10:13:57 EDT 2004


>>>>> "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



More information about the Ale mailing list