I would suggest that you make sure that autoflush is turned on for both
STDOUT and STDERR before you run the command.

        select((select(STDOUT), $|=1)[0]);
        select((select(STDERR), $|=1)[0]);

I'm not sure if this will help for a command run via backticks or not, but
I've corrected similar "output order" problems with this trick.


wantor

> -----Original Message-----
> From: Chad Wallace [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, August 10, 2000 1:53 PM
> To: '[EMAIL PROTECTED]'
> Cc: [EMAIL PROTECTED]
> Subject: RE: Capturing output from a child's STDOUT and STDERR
> simultaneou sly
> 
> 
> That doesn't give them in the proper order.  I get all the 
> STDOUT lines
> first, then all the STDERR lines after them.  I might just 
> have to settle
> with that.  It's so uncool, though. :-(
> 
> BTW, I know the output can be received in the proper order 
> because when I
> run a Perl script redirected into EditPlus, and it has warnings, the
> warnings (which are on STDERR) show up intermixed with the 
> regular output,
> not just at the end.  ... I just need to find how it can be 
> done in Perl.
> 
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> Sent: August 10, 2000 11:26
> To: [EMAIL PROTECTED]
> Cc: [EMAIL PROTECTED]
> Subject: RE: Capturing output from a child's STDOUT and STDERR
> simultaneou sly
> 
> 
> Chad,
> 
> Depending on exactly what commands you are running, and how 
> you are running
> them, you might be able to redirect the stderr to the stdout using the
> command prompt command '2>&1'.  I've use this successfully to 
> capture stderr
> output from dos commands executed via the backquotes.  For example,
> 
>       $listing = `dir`;
> 
> would capture the directory listing (if any) in the $listing 
> variable, but
> any errors would still go to the screen.  However,
> 
>       $listing = `dir 2>&1`;
> 
> would capture both the directory listing and any errors in 
> the $listing
> variable.
> 
> 
> HTH,
> 
> wantor
> 
> > -----Original Message-----
> > From: Chad Wallace [mailto:[EMAIL PROTECTED]]
> > Sent: Thursday, August 10, 2000 12:46 PM
> > To: '$Bill Luebkert'
> > Cc: Perl-Win32-Users Mailing List (E-mail)
> > Subject: RE: Capturing output from a child's STDOUT and STDERR
> > simultaneou sly
> > 
> > 
> > Well, I just tried duping STDERR to STDOUT, and it doesn't 
> work.  The
> > child's STDERR output just goes to the screen, and the parent 
> > only receives
> > the child's STDOUT output.  I'm guessing that the child 
> > doesn't inherit the
> > parent's duped STDERR.  It just gets its own.
> > 
> > -----Original Message-----
> > From: $Bill Luebkert [mailto:[EMAIL PROTECTED]]
> > Sent: August 9, 2000 16:05
> > To: Chad Wallace
> > Cc: Perl-Win32-Users Mailing List (E-mail)
> > Subject: Re: Capturing output from a child's STDOUT and STDERR
> > simultaneously
> > 
> > 
> > Chad Wallace wrote:
> > > 
> > > Hi, guys.
> > > 
> > > I'll bet you're scared already, just from reading the 
> > subject line. :-)
> > > 
> > > I want to capture all output that comes from a child--both 
> > to STDOUT and
> > to
> > > STDERR--into a while loop so I can process it.  The child 
> > might be any DOS
> > > command.  I would like the lines to come to me in order.  
> > ie: If I print
> > > line A to STDOUT then line B to STDERR then line C to 
> > STDOUT again, I want
> > > to recieve them as A then B then C, not A then C then B as 
> > IPC::Open3
> > gives
> > > them by default.
> > > 
> > > The Open3 documentation talked about using select() and 
> > sysread() to read
> > > from the open3 handles, but I can't figure those functions 
> > out, and the
> > Perl
> > > documentation on the select function is pretty weird (vec() 
> > and fileno(),
> > > etc), and the select(2) manpage isn't much better.  
> > Needless to say, I
> > > couldn't get it to work this way.  select kept giving 'Bad file
> > descriptor'.
> > > 
> > > Is there a better way to do this?  Or is there some place I 
> > could learn
> > how
> > > select() and sysread() are supposed to be used with IPC::Open3?
> > > 
> > > Thanks...
> > > Chad.
> > > 
> > > FWIW, here is the code that I have come up with so far, 
> > from what I could
> > > glean off of the various perldoc's:
> > > [stdouterrtest.pl -- The tester child]
> > > print STDOUT "out: one\n";
> > > print STDERR "err: one\n";
> > > print STDOUT "out: two\n";
> > > print STDERR "err: two\n";
> > > print STDERR "err: parm1 = $ARGV[0]\n";
> > > print STDOUT "out: parm2 = $ARGV[1]\n";
> > > __END__
> > > 
> > > [open3Test.pl -- The main program]
> > > #!perl -w
> > > use IPC::Open3;
> > > require FileHandle;
> > > use strict;
> > > 
> > > my $command = q/perl c:\buildscripts\stdouterrtest.pl 
> parm1 parm2/;
> > > 
> > > my $in = FileHandle->new;
> > > my $out = FileHandle->new;
> > > my $err = FileHandle->new;
> > > 
> > > open3($in, $out, $err, $command);
> > > 
> > > my ($outbits, $errbits) = ('','');
> > > vec($outbits, fileno($out), 1) = 1;
> > > vec($errbits, fileno($err), 1) = 1;
> > > 
> > > my $go = 1;
> > > while ($go or $!) # Go until sysread returns 0.
> > > {
> > >         my $buf;
> > >         my ($outfound, $errfound);
> > >         $outfound = select($outbits, undef, undef, undef);
> > >         print "OUT: $outfound, $!\n";
> > >         if ($outfound == 1)
> > >         {
> > >                 $go = sysread($out, $buf, 1000);
> > >                 print $buf;
> > >         }
> > > 
> > >         $errfound = select($errbits, undef, undef, undef);
> > >         print "ERR: $errfound, $!\n";
> > >         if ($errfound == 1 and $go)
> > >         {
> > >                 $go = sysread($err, $buf, 1000);
> > >                 print $buf;
> > >         }
> > > }
> > > 
> > > $out->close;
> > > $err->close;
> > > $in->close;
> > > 
> > > print STDOUT "Done!\n";
> > > __END__
> > 
> > Have you tried just duping STDERR to STDOUT and then running 
> > backticks 
> > to the other process?
> > 
> > open STDERR, ">&STDOUT" or die "Can't dup stderr to stdout: $!";
> > @results = `run some command here`;
> > foreach (@results) {
> >     ...
> > }
> > 
> > -- 
> >   ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
> >  (_/   /  )    // //       DBE Collectibles   
> http://www.wgn.net/~dbe/
> >   / ) /--<  o // //      
> Mailto:[EMAIL PROTECTED]   
> > http://dbecoll.webjump.com/
> > -/-' /___/_<_</_</_    
> > http://www.freeyellow.com/members/dbecoll/
> > _______________________________________________
> > Perl-Win32-Users mailing list
> > [EMAIL PROTECTED]
> > http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> > 
> _______________________________________________
> Perl-Win32-Users mailing list
> [EMAIL PROTECTED]
> http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> 
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to