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


in reply to Re: Use of IPC::Run rather than system()
in thread Use of IPC::Run rather than system()

It doesn't help if you want to capture STDERR and STDOUT in separate scalars.

To answer gmarler's question: if you just want to capture STDERR and STDOUT for an external program then it is rather easy to do with IPC::Run:

use IPC::Run qw(run); my($out, $err); run ['/path/to/prog', 'arg1', 'arg2'], '>', \$out, '2>', \$err;

You can find more complex examples in following nodes:

--
Ilya Martynov, ilya@iponweb.net
CTO IPonWEB (UK) Ltd
Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
Personal website - http://martynov.org

Replies are listed 'Best First'.
Re: Re: Re: Use of IPC::Run rather than system()
by gmarler (Sexton) on Jul 16, 2003 at 18:01 UTC

    Thanks for that - note however that in addition to capturing STDOUT/STDERR, I still need to have the STDIN/STDOUT/STDERR of the external program connected to the terminal where the person is running the script, in case the external program requires input from the user, and so that any output is immediately visible to the user, in addition to being saved in the scalar(s).

    I'll try your recommendation out though - maybe it does give me what I want.

      For STDIN/-ERR, just print the output to the appropriate handles yourself. For STDIN, if you have taken control of the subprocesses STDIN yourself, just copy the user input in there yourself. You can do that "realtime" rather than post mortem by passing closures rather scalar refs, if memory serves.

      Makeshifts last the longest.

        Something like this should work:
        use IPC::Run qw(run); my ($out, $err) = ('', ''); run(['/path/to/prog', @args], '>', sub { $out .= $_[0]; print $_[0] }, '2>', sub { $err .= $_[0]; print $_[0] });

        --
        Ilya Martynov, ilya@iponweb.net
        CTO IPonWEB (UK) Ltd
        Quality Perl Programming and Unix Support UK managed @ offshore prices - http://www.iponweb.net
        Personal website - http://martynov.org