We are out of contest in here. I know how to run open3, but I don't know how to test it. Repeating
use strict; use warnings; use IPC::Open3; use IO::Handle; use Test::More; use Test::Trap; sub shell_run { my ($stdin, $stdout, $stderr) = map {IO::Handle->new} (0..2); print "YYYY"; open3($stdin, $stdout, $stderr, @_); foreach my $line (<$stdout>, <$stderr>) { print "read: $line"; } print "ZZZZ"; } trap {shell_run('perl', '-E', 'print "TEXT IN"')}; #is( $trap->stdout, "YYYYZZZZ"); is( $trap->stdout, "YYYYTEXT INZZZZ"); done_testing(); How do I make that test pass? or this: trap {shell_run('perl', '-E', 'print STDERR "TEXT IN"')}; is( $trap->stdout, "YYYYTEXT INZZZZ"); Thanks Marcos On Mon, Aug 30, 2010 at 6:32 AM, C.DeRykus <dery...@gmail.com> wrote: > On Aug 28, 10:42 pm, ole...@gmail.com (marcos rebelo) wrote: >> the idea is to process the STDOUT ad the STDERR. >> >> open don't do it >> > > I was afraid you'd say that... > > open3 is very liable to deadlock since you're trying to read from > both stderr and stdout. You'll very likely want to use IO::Select > to marshal when the read's occur and search for some sample > code. > >> >> On Sun, Aug 29, 2010 at 6:08 AM, John W. Krahn <jwkr...@shaw.ca> wrote: >> >> >> >> > C.DeRykus wrote: >> >> >> Since you mention simplifying the code, do you actually >> >> need IPC::Open3 ? In your sample code, you're only >> >> reading process output. >> >> >> If you don't need IPC::Open3 complexity, you could just >> >> use magic open to read output : >> >> >> sub shell_run >> >> { >> >> print "YYYY"; >> >> my $pid = open( my $fh, qq{ @_ | } ) or die "open: $!"; >> >> > Probably better as: >> >> > my $pid = open my $fh, '-|', @_ or die "open: $!"; > > > In general yes but here I don't think there's a benefit. > I believe perl will bypass the shell in both cases since > a list is passed. And, IIRC, perl launches a shell only > after parsing to see if it's absolutely required. > > >> >> > And you don't use $pid anywhere so why create it? > > Agreed, I was trying to guage the OP's intention more > than cleaning up all the code. > >> >> >> print for<$fh>; >> >> > Probably better as: >> >> > print while <$fh>; > > Hm, I'd prefer 'while' as well but, though it may look odd, I > don't think there's been the penalty of 'for <$fh>' creating > and then iterating through a potentially large list for some > time. > >> >> >> close $fh or die "close: ", $? || $!; >> >> > Probably better as: >> >> > close $fh or die $! ? "Error closing pipe: $!" >> > : "Exit status $? from $_[0]"; > > Definitely better. A child error might mask an > error for a full disk. > > -- > Charles DeRykus > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > > -- Marcos Rebelo http://oleber.freehostia.com Milan Perl Mongers leader http://milan.pm.org Webmaster of http://sites.google.com/site/oleberperlrecipes/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/