Click here to get back home

IPC::Open3 : Why can't I catch program output here?

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
IPC::Open3 : Why can't I catch program output here? Ronny 05-05-2008
Posted by Darren Dunham on May 6, 2008, 12:01 pm
Please log in for more thread options
> I would normally say 'Use lexical filehandles!' at this point;
> unfortunately, IPC::Open3 was written before they existed and the
> obvious way
>
> my $pid = open3(my $CMD_IN, my $CMD_OUT, my $CMD_ERR, @cmd)...
>
> doesn't work (and can't be made to since undef is already meaningful).

What's wrong with the above?

The only issue I see is that Open3 will not autogenerate a filehandle
for stderr (instead it will combine stdout and stderr and place them
both on the $CMD_OUT filehandle). I actually use that method so I don't
have to select between two filehandles and can just read one at a time.

# capture STDOUT and STDERR, on a single filehandle
use IPC::Open3;
open3(my $cmd_in, my $cmd_out, undef, @cmd);
while (<$cmd_out>)
{
...
}

--
Darren Dunham ddunham@taos.com
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >

Posted by xhoster on May 6, 2008, 12:07 pm
Please log in for more thread options
ddunham@taos.com (Darren Dunham) wrote:
> > I would normally say 'Use lexical filehandles!' at this point;
> > unfortunately, IPC::Open3 was written before they existed and the
> > obvious way
> >
> > my $pid = open3(my $CMD_IN, my $CMD_OUT, my $CMD_ERR, @cmd)...
> >
> > doesn't work (and can't be made to since undef is already meaningful).
>
> What's wrong with the above?

It causes cmd's stderr to go to $CMD_OUT rather than the obviously
intended $CMD_ERR.

>
> The only issue I see is that Open3 will not autogenerate a filehandle
> for stderr (instead it will combine stdout and stderr and place them
> both on the $CMD_OUT filehandle).

And this is obviously a problem if that is not what you want to happen.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.

Posted by Darren Dunham on May 6, 2008, 5:37 pm
Please log in for more thread options
xhoster@gmail.com wrote:
> ddunham@taos.com (Darren Dunham) wrote:
>> > I would normally say 'Use lexical filehandles!' at this point;
>> > unfortunately, IPC::Open3 was written before they existed and the
>> > obvious way
>> >
>> > my $pid = open3(my $CMD_IN, my $CMD_OUT, my $CMD_ERR, @cmd)...
>> >
>> > doesn't work (and can't be made to since undef is already meaningful).
>>
>> What's wrong with the above?
>
> It causes cmd's stderr to go to $CMD_OUT rather than the obviously
> intended $CMD_ERR.

I don't know that it's obviously intended. It took me quite a while to
realize that I even had an option to return output and error on a single
filehandle. I mention that explicitly because the OP might have been
having issues with selecting between the two, when reading them combined
may have been preferable.

However, if you do want them separated, then the other choices offered
seem quite verbose. Instead of them, I would pass in a (defined)
filehandle. That takes one more line of code (two if you count bringing
in the FileHandle module.

my $CMD_ERR = FileHandle->new();
my $pid = open3(my $CMD_IN, my $CMD_OUT, $CMD_ERR, @cmd)...

--
Darren Dunham ddunham@taos.com
Senior Technical Consultant TAOS http://www.taos.com/
Got some Dr Pepper? San Francisco, CA bay area
< This line left intentionally blank to confuse you. >

Posted by Ben Morrow on May 6, 2008, 6:59 pm
Please log in for more thread options

Quoth ddunham@taos.com (Darren Dunham):
> xhoster@gmail.com wrote:
> > ddunham@taos.com (Darren Dunham) wrote:
> >> > I would normally say 'Use lexical filehandles!' at this point;
> >> > unfortunately, IPC::Open3 was written before they existed and the
> >> > obvious way
> >> >
> >> > my $pid = open3(my $CMD_IN, my $CMD_OUT, my $CMD_ERR, @cmd)...
> >> >
> >> > doesn't work (and can't be made to since undef is already meaningful).
> >>
> >> What's wrong with the above?
> >
> > It causes cmd's stderr to go to $CMD_OUT rather than the obviously
> > intended $CMD_ERR.
>
> I don't know that it's obviously intended. It took me quite a while to
> realize that I even had an option to return output and error on a single
> filehandle. I mention that explicitly because the OP might have been
> having issues with selecting between the two, when reading them combined
> may have been preferable.
>
> However, if you do want them separated, then the other choices offered
> seem quite verbose. Instead of them, I would pass in a (defined)
> filehandle. That takes one more line of code (two if you count bringing
> in the FileHandle module.
>
> my $CMD_ERR = FileHandle->new();
> my $pid = open3(my $CMD_IN, my $CMD_OUT, $CMD_ERR, @cmd)...

...which is essentially the same as what I proposed, except that I
prefer to avoid FileHandle (and IO::Handle) and use Symbol::gensym
directly. That's just a matter of taste, of course.

Ben


Similar ThreadsPosted
IPC::Open3 August 24, 2005, 2:23 pm
IPC::Open3 November 11, 2006, 8:10 pm
How do you use IPC::Open3.... January 19, 2008, 5:11 pm
open3 and signals September 20, 2004, 8:43 pm
IPC::Open3 issue August 24, 2005, 3:21 pm
can't get STDERR using open3... December 20, 2006, 3:21 pm
IPC::open3: What goes wrong? August 1, 2007, 1:05 pm
Example for open3 on windows? May 20, 2008, 1:33 am
Open3 with nonblocking reads September 3, 2004, 7:51 am
IPC::Open3 and the error filehandle October 26, 2007, 9:33 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap