[comp.lang.perl] perl equivalent of "pg" or "more"?

jew@rt.sunquest.com (/87336) (05/07/91)

This is a question for you perl gurus... How hard is it to duplicate
the functionality of "pg" or "more" in perl.  Mainly, pagination and
searching for strings both forward and backwards.  Seems like it might
be pretty easy and related to the previous question of how to print
the last N pages of a file.  Any takers?  Thanks in advance.
jew@sunquest.com

Confucious say...
God must love the common man: He made so many of them.

rbj@uunet.UU.NET (Root Boy Jim) (05/08/91)

In <1991May7.092222.625@arizona.edu> jew@rt.sunquest.com (/87336) writes:
>
>How hard is it to duplicate
>the functionality of "pg" or "more" in perl.

Why bother? Use "less". Pg and more are inferior.
-- 
		[rbj@uunet 1] stty sane
		unknown mode: sane

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/25/91)

In article <cTPo21w164w@syzzle.chi.il.us> system@syzzle.chi.il.us (awol) writes:
: jew@rt.sunquest.com (/87336) writes:
: 
: > This is a question for you perl gurus... How hard is it to duplicate
: > the functionality of "pg" or "more" in perl.  Mainly, pagination and
: > searching for strings both forward and backwards.  Seems like it might
: > be pretty easy and related to the previous question of how to print
: > the last N pages of a file.  Any takers?  Thanks in advance.
: 
: Definately not a perl guru......yet, but I did do this simple version.
: What I would like to know though, is how can I get just one key press
: (without waiting for ENTER)? I would like to do something like this, and
: then after one key is pressed backup and erase the prompt. I posted this
: question a short while back, but haven't seen any responses.
: 
: #!/usr/bin/perl
: #
: $lines = 0;
: while (<>) {
: 	$lines++;
: 	print;
: 	next unless $lines == 23;
: 	$lines = 0;
: 	print "==> ";
: 	$answer = getc;
: 	last if $answer eq "q";
: }

Depends on your OS, but you can usually use one of `stty cbreak`
or `stty -icanon`.  You might also get better results using
sysread(STDIN,$answer,1) rather than getc.  To erase the line,
print "\r" and whatever else is appropriate.  You may need to set $|
to make sure the string gets flushed from the buffer.

Larry

system@syzzle.chi.il.us (awol) (05/26/91)

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) writes:
> system@syzzle.chi.il (awol) writes:
>
> : Definately not a perl guru......yet, but I did do this simple version.
> : What I would like to know though, is how can I get just one key press
> : (without waiting for ENTER)? I would like to do something like this, and
> : then after one key is pressed backup and erase the prompt. I posted this
> : question a short while back, but haven't seen any responses.
> : 
> : #!/usr/bin/perl
> : #
> : $lines = 0;
> : while (<>) {
> : 	$lines++;
> : 	print;
> : 	next unless $lines == 23;
> : 	$lines = 0;
> : 	print "==> ";
> : 	$answer = getc;
> : 	last if $answer eq "q";
> : }
> 
> Depends on your OS, but you can usually use one of `stty cbreak`
> or `stty -icanon`.  You might also get better results using
> sysread(STDIN,$answer,1) rather than getc.  To erase the line,
> print "\r" and whatever else is appropriate.  You may need to set $|
> to make sure the string gets flushed from the buffer.
> 
> Larry

I am using MS-DOS. I had tried using read before, and did try sysread,
but everything requires pressing return. Then I can't just back up to
erase the prompt because I'm already on the next line. If it is not
possible to do this (get one keypress) on the dos version, it would be a
nice feature to add. Similiar to the Turbo-C getch function.

+------------------------+-----------------------------------+
|   Al Oomens (awol)     | Inside every LARGE program is     |
| awol@syzzle.chi.il.us  | a small program trying to get out.|
+------------------------+-----------------------------------+

allbery@NCoast.ORG (Brandon S. Allbery KF8NH) (05/29/91)

As quoted from <qy3i31w164w@syzzle.chi.il.us> by system@syzzle.chi.il.us (awol):
+---------------
| I am using MS-DOS. I had tried using read before, and did try sysread,
| but everything requires pressing return. Then I can't just back up to
| erase the prompt because I'm already on the next line. If it is not
| possible to do this (get one keypress) on the dos version, it would be a
| nice feature to add. Similiar to the Turbo-C getch function.
+---------------

MS-DOS has an ioctl() function, and you can get character-at-a-time input by
setting the console to RAW mode.  If you have a DOS programming book of some
kind (preferably assembler level, C books will generally assume access to
functions that Perl can't get at) you can look up the values to pass to ioctl()
to go into/out of RAW mode.

Be warned that in RAW mode, you lose CTRL-BREAK and CTRL-PrtSc, so debug your
program FULLY before adding the RAW mode ioctl's and make SURE it's reset
before the Perl script exits!

++Brandon
-- 
Me: Brandon S. Allbery			 KF8NH: DC to LIGHT!  [44.70.4.88]
Internet: allbery@NCoast.ORG		 Delphi: ALLBERY
uunet!usenet.ins.cwru.edu!ncoast!allbery