Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Piping STDOUT to 'more'

by myocom (Deacon)
on Mar 20, 2002 at 23:39 UTC ( #153171=perlquestion: print w/ replies, xml ) Need Help??
myocom has asked for the wisdom of the Perl Monks concerning the following question:

In trying to come up with a solution to print 'more'., I found myself looking at the results of this one-liner:

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

Can anyone explain what the heck is happening around line 850 or so (the exact line seems to vary depending on your OS and possibly on your window size)? I thought perhaps it had something to do with buffering, but adding a $|++ in there doesn't seem to affect it a-tall.

"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

Comment on Piping STDOUT to 'more'
Select or Download Code
Re: Piping STDOUT to 'more'
by zengargoyle (Deacon) on Mar 20, 2002 at 23:55 UTC
    perl -e '$|++;open STDOUT, "|more";print qq($_\n)for(1..1000);close STDOUT'

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

      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
      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).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://153171]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2014-09-16 21:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (49 votes), past polls