Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Use of IPC::Run rather than system()

by IlyaM (Parson)
on Jul 16, 2003 at 16:50 UTC ( [id://274927]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://274927]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-20 02:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found