I finally understand what’s happening, thanks for the link to
http://perl.plover.com/FAQs/Buffering.html. Perl’s system command
starts a program’s whose stdout becomes my terminal (and that’s why it
works), other ways of forking commands (backticks, open, open2) set
stdout to a file that is not a terminal. Most programs automatically
check whether they are writing to a terminal or a file and change its
buffering behavior based on that (in perl, terminals get line buffered
and files get buffered by 8K before they are flushed). I can’t control
this behavior in my wrapper script. However, there are ways to fool
the running program into thinking that it is outputting to a pseudo
terminal (Expect and IO::Pty do this).

On Sep 9, 11:08 pm, [EMAIL PROTECTED] (Jake) wrote:
> test.pl cant be modified, im looking for something that will work with
> any program, my own or not.
>
> On Sep 9, 10:11 pm, [EMAIL PROTECTED] (Raymond Wan) wrote:
>
> > Hi,
>
> > Seems like what you need is to do an "autoflush".  Try searching for it
> > with Google...this might be a good start and enough for what you want:
>
> >http://bytes.com/forum/thread603712.html
>
> > Note that stdout tends to be buffered and stderr not (since it's for
> > error messages and they should appear "right away").  So, you may not
> > need to do this with stderr.
>
> > There was another way to flush output, which I forgot...but I think if
> > you search for "flush" or "autoflush", you'll come across it...
>
> > Ray
>
> > doubleHelix wrote:
> > > I am having a problem in capturing the output and exit value from a
> > > system command that prompts the user for input. The following shows a
> > > simplification of the problem. Im trying to get it so that all the
> > > text before the prompt gets output before the script gets stuck at the
> > > prompt.  I’m sure it is possible, but my knowledge of ttys and file
> > > reads and writes is failing me now.
>
> > > test.pl
> > > =====
> > > # the less I can assume about test.pl the better.
> > > # It shouldn’t matter if the prompt is on STDOUT or STDERR, I want to
> > > capture all of it.
>
> > > print "Hello World!\n";
> > > print "Enter something: ";
> > > chomp ($foo = <STDIN>);
> > > print "Thanks for inputting that $foo\n";
>
> > > wraptest.pl
> > > ========
>
> > > $| = 1;
> > > print "in wraptest\n";
> > > open CMD, "perl test.pl 2>&1 |" or die "couldnt fork: $!";
> > > while ( <CMD> ) {
> > >         print "hi $_";
> > > }
> > > close CMD;
> > > print "exit value: ", $?>>8, "\n";
>
> > > output
> > > =====
>
> > > in wraptest
> > > <PROMPT>
> > > hi Hello World!
> > > hi Enter something: Thanks for inputting that $PROMPT
> > > exit value: 0
>
> > > I’d like to modify wraptest.pl so that the output would be
>
> > > in wraptest
> > > hi Hello World!
> > > hi Enter something: <PROMPT>
> > > hi Thanks for inputting that $PROMPT
> > > exit value: 0
>
> > > Thanks


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to