pwr has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm currently writing a perl-wrapper for Debians aptitude. The wrapper should just color some parts of the output. If I want to install a package, aptitude usually asks 'Do you want to continue? [Y/n/?]' without appending a \n after the question. The problem is, that my wrapper does not display this question until I enter something at the keyboard. This looks as follows:
... Need to get 667kB of archives. After unpacking 3965kB will be used. y Do you want to continue? [Y/n/?] Writing extended state information... ...
The actual question is printed after my input. Currently, the executed code in the wrapper is very simple:
print $_ while (<STDIN>);
To get the whole thing running, I pipe the output of aptitude into the wrapper: sudo aptitude install xyz | ./wrap.pl After playing around for a while now, I know that's a problem of buffering but I'm not sure how to bypass it.

Thanks

Replies are listed 'Best First'.
Re: I/O buffering problem
by pc88mxer (Vicar) on Feb 15, 2008 at 20:17 UTC
    When you use <STDIN>, perl waits for the new line before returning the result. You need to use read or sysread to get data as soon as it is available.
      Ah, that perfectly solves my problem, thank you!
Re: I/O buffering problem
by hipowls (Curate) on Feb 15, 2008 at 20:22 UTC

    You output is buffered. To turn it off set $| to a true value, for example $| = 1

    Update: I answered the wrong question. I thought the OP was printing the prompt, he is trying to read it. read or sysread is the correct answer as given by pc88mxer