in reply to Piping STDOUT to 'more'

perl -e '$|++;open STDOUT, "|more";print qq($_\n)for(1..1000);close STDOUT'

Works for me. I don't really know why...

Replies are listed 'Best First'.
Re: Re: Piping STDOUT to 'more'
by ariels (Curate) on Mar 21, 2002 at 10:20 UTC
    The manpage for close says:
    Closing a pipe also waits for the process executing on the pipe to complete.
    This explains what's going on. If you don't close STDOUT, your process exits immediately when it finishes putting data into the queue. If you do, Perl waits for the more process to terminate (so it can provide an exit status).

    If you add a wait after the loop (instead of close STDOUT), the more process runs "correctly". But if you page your way all the way through the file, you deadlock on Perl waiting for more to terminate, and more waiting for more data or end of file to terminate. (Typing <samp>q</samp> to exit more exits both programs, of course).

Re: Re: Piping STDOUT to 'more'
by myocom (Deacon) on Mar 21, 2002 at 01:07 UTC

    Interesting. Even explicitly closing STDOUT doesn't fix the problem for me (Perl 5.6.1 on WinXP).

    The speculation around the office is that the perl.exe process is going away before the more process.

    "One word of warning: if you meet a bunch of Perl programmers on the bus or something, don't look them in the eye. They've been known to try to convert the young into Perl monks." - Frank Willison