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

Hello great and mighty monks, I am hoping that you will be kind enough to bestow some of your wisdom upon me. I am trying to find a way to mimic the way the less command works to pause text flow until a certain key is pressed, usually space bar, that way the program will wait until the user is done reading the text and ready to move on through the program. Any ideas on how this may be done?

Replies are listed 'Best First'.
Re: Sleep until keypress
by Fletch (Bishop) on Apr 22, 2008 at 19:36 UTC

    See Term::ReadKey, getc, and "How can I read a single character from a file? From the keyboard?" in perlfaq5.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Sleep until keypress
by FunkyMonk (Bishop) on Apr 22, 2008 at 21:08 UTC
Re: Sleep until keypress
by tachyon-II (Chaplain) on Apr 23, 2008 at 00:08 UTC

    More or less what you are after ;-)

    use Term::ReadKey; ReadMode 4; # raw mode END{ ReadMode 0 } # restore mode my @data = map{"$_\tjaph\n"}1..100; my ($page, $line, $key) = (20,0,''); clear_screen(); while( $line < @data ) { if ($line and 0 == $line % $page) { printf "--- More or Less (%d%%) ---\n", int(100*$line/@data); 1 while not defined ($key=ReadKey(0.5)); $line -= 2*$page if $key =~ m/b/i; $line = 0 if $line < 0; exit if $key =~ m/q/i; clear_screen(); } print $data[$line++]; } sub clear_screen { system($^O =~ m/Win32/ ? 'cls' : 'clear') }
Re: Sleep until keypress
by vendion (Scribe) on Apr 23, 2008 at 02:06 UTC
    Thanks for replying to my request, and so soon, the Term::ReadKey and Term::Pager both caught my eye. I'll try them out and see which one works better. Again thanks for the help everyone.
Re: Sleep until keypress
by jeepj (Scribe) on Apr 23, 2008 at 08:17 UTC
    As simple as it sounds, on windows, I'm simply using a system("PAUSE"); command. Perhaps to simple, but for what your looking for, it should do the trick.

      That works OK on Win32 if you have the right keyboard. Less offers more functionality (based on the keypress) than more. If you only use windows you probably don't understand that why less is more!

        If vendion has some more 'advanced' requirements, ReadKey is certainly the way to go, but based on his question, it was not obvious at first sight. The idea behind my reply was that, sometime, installing and using CPAN modules like ReadKey just to pause a script until the user is ready to proceed to the rest of the script, is useless. Sometime the simplest solution is the best.

Re: Sleep until keypress
by zentara (Cardinal) on Apr 23, 2008 at 16:09 UTC