469,284 Members | 2,492 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,284 developers. It's quick & easy.

Reading stdout and stderr separately


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?


Jul 19 '05 #1
3 8374
ladygrinningsoul wrote:
There has got to be a better way.


1) Look into IPC::Open3.
2) Next time, post to comp.lang.perl.misc (not comp.lang.perl).
Jul 19 '05 #2
ladygrinningsoul wrote:
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
Jul 19 '05 #3
ladygrinningsoul wrote:

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().

--
Alexey 'Kaa the Snake' Kiritchun
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tsai Li Ming | last post: by
7 posts views Thread by Andre | last post: by
21 posts views Thread by EdUarDo | last post: by
reply views Thread by Christoph Haas | last post: by
1 post views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
2 posts views Thread by Guillaume Dargaud | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.