Reading stdout and stderr separately

L

ladygrinningsoul

I have a Perl program which runs a number of external programs. I want to
be able to read both stdout and stderr into separate variables. The 'Perl
Cookbook' states (I quote):

To read both a command's STDOUT and its STDERR separately, it's easiest and
safest to redirect them separately to files, and then read from those files
when the program is done: system("program args 1>/tmp/program.stdout
2>/tmp/program.stderr");


There has got to be a better way. Anyone?
 
J

Jürgen Exner

ladygrinningsoul said:
I have a Perl program which runs a number of external programs. I
want to be able to read both stdout and stderr into separate
variables. The 'Perl Cookbook' states (I quote):

To read both a command's STDOUT and its STDERR separately, it's
easiest and safest to redirect them separately to files, and then
read from those files when the program is done: system("program args
1>/tmp/program.stdout 2>/tmp/program.stderr");


There has got to be a better way. Anyone?

Well, it's the same method as recommended by the FAQ. You did check the FAQ,
didn't you?

jue
 
A

Alexey A. Kiritchun

ladygrinningsoul said:
I have a Perl program which runs a number of external programs. I want to
be able to read both stdout and stderr into separate variables. The 'Perl
Cookbook' states (I quote):
There has got to be a better way. Anyone?

You should have read the book a little further. See recipe 16.9, (or at
least it is under that number in my copy), or jump straight to perldoc
IPC::Open3.

Using it is rather prone to deadlock, though.

If you are doing it as an excercise only, it is also posible to do
everything via fork(), exec(), and POSIX::dup().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top