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

Hi Monks,
I have my perl program printing something and I would like it to pipe to 'more'.
This will be very easy with 'program.pl | more'. The glitch is that this is interactive program and I cannot do just mentioned. The data flushes to screen. I know my option to print to a file and then view it, but I want to give option to see the output on the screen too. I can manipulate the print statements and ask for the user input (key press) at every 30 or so line but that will be terminal dependent and dirty tricks.

Your input will be valuable.

Thanks,
Artist.

Replies are listed 'Best First'.
Re: print 'more'.
by tachyon (Chancellor) on Mar 20, 2002 at 23:15 UTC

    Prsumably you have two things happening : 1) you print screen fulls of data; 2) You pause for user input. Obviously the printing will cease when you get to the $answer = <STDIN>;. Thus you problem is how to simulate more when you print the other stuff. You can do this easily by defining a custom print function.

    sub print_more; # declare a proto my $more = 20; # twenty lines then stop my $wrap = 80; # assume that we will wrap at 80 cols print_more $some_big_string, @big_array; { my $line_count = 0; # declare a closure to remember lines printed sub print_more { my $data = join'', @_; my @data_lines = split "\n", $data; for my $line (@data_lines) { # add one line if less than $wrap long (no wrap) # or more for long lines that will wrap $line_count += int ((length($line))/$wrap) + 1; if ($line_count < $more ) { print $line, "\n"; } else { print "[Hit Enter key to continue]"; <STDIN>; $line_count = int ((length($line))/$wrap) + 1; print $line, "\n"; } } } }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: print 'more'.
by FoxtrotUniform (Prior) on Mar 20, 2002 at 23:16 UTC

    Use '-t' to see if you're on a TTY:

    #! /usr/bin/perl -w if(-t STDOUT) { print "We're on a tty!\n"; } else { print "We're not on a tty!\n"; }

    --
    :wq

Re: print 'more'.
by Sidhekin (Priest) on Mar 21, 2002 at 15:41 UTC

    Take a look at perldoc -- it does paging while running, using a temporary file. The meat of the thing is this little sub:

    sub page { my ($tmp, $no_tty, @pagers) = @_; if ($no_tty) { open(TMP,"<", $tmp) or die "Can't open $tmp: $!"; local $_; while (<TMP>) { print or die "Can't print to stdout: $!"; } close TMP or die "Can't close while $tmp: $!"; } else { foreach my $pager (@pagers) { last if system("$pager $tmp") == 0; } } }

    The good perldoc tries to use an array of pagers, depending on system type (Win32, VMS, DOS, OS2, and of course *N*X), and even has a flexible idea of what a pager _is_ (notepad is one option for Win32).

    Now, if there is a module doing the same kind of thing, or if someone were to make one -- I would like to know :-)

    If I understand your problem right, you should be able to find what you need in the perldoc source. How is that for irony? perldoc teaching you programming techniques! :-)

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"