Something like:

my $pager= $ENV{PAGER} || "more"; open STDOUT, "| $pager" or warn "Not paging STDOUT: $!\n";
should work even under Windows. I think it'd work under even more OSes so long as you set PAGER or have a version of "more" installed.

But, in testing this, I did find one wrinkle. Under Win32, the process model is rather basic and I couldn't find a way to get the shell to wait for "more" to finish. Here is the closest I came which seems to demonstrate a bug:

perl -le "open STDOUT, '| more'; $|=1; print for 0..50; close(STDOUT) +or warn $!; wait" ... Bad file descriptor at -e line 1. ...
so the close fails which means that "more" never gets EOF and never exits. Note that the wait really shouldn't be there as an explicit close on a piped command does a wait. I just added that when Perl wasn't waiting for "more" to exit [ which made Perl wait forever which made me wonder why close wasn't sending an EOF which made me add the warn which showed me a bug in Perl -- now where is my prize? ;-) ].

So on OSes where Perl isn't broken (: you'd probably want to add:

END { close(STDOUT) }
so that Perl sends "more" an EOF and then waits for "more" to exit.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: Paging STDOUT? by tye
in thread Paging STDOUT? by ziva

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.