http://qs321.pair.com?node_id=33566

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (input and output)

What is a good "Press any key to continue" code?

Originally posted as a Categorized Question.

  • Comment on What is a good "Press any key to continue" code?

Replies are listed 'Best First'.
Re: What is a good "Press any key to continue" code?
by tye (Sage) on Sep 22, 2000 at 01:34 UTC
    { local( $| ) = ( 1 ); print "Press <Enter> or <Return> to continue: "; my $resp = <STDIN>; }
      That isn't quite "any key", but that's what I was going to say also...
      print "Press ENTER to continue: "; <STDIN>; # continue...
      Update: Thanks to everyone for pointing out my <> v. <STDIN> flaw. When building quick/easy scripts for myself I tend to get lax and use <> when the correct way to reference a line from STDIN is <STDIN>. A bad habit I guess.
        No. Never use <> when you mean <STDIN>.

        What if you're using the elements of @ARGV to hold keywords or other items?

        I'd fail this one in code review, since it has a trivial fix, could cause significantly odd behavior, and indicates a confused mind in general.

        My code-review rule is:

        if you prompt to STDOUT, you should read from STDIN, not ARGV.

        -- Randal L. Schwartz, Perl hacker

      Note that getting any key to work is hard to do portably, so I just suggest you avoid the issue. Also <> is not the same as <STDIN> if there is anything in @ARGV.

              - tye (but my friends call me "Tye")
Re: What is a good "Press any key to continue" code?
by Anonymous Monk on Sep 22, 2000 at 06:38 UTC
    use Term::ReadKey:
    use Term::ReadKey; ReadMode 4; # Turn off controls keys while (not defined ($key = ReadKey(-1)) { # No key yet } print "Got key $key\n"; ReadMode 0; # Reset tty mode before exiting
Re: What is a good "Press any key to continue" code?
by Anonymous Monk on Nov 14, 2003 at 20:07 UTC
    For windows based systems:

    system( 'pause' );
Re: What is a good "Press any key to continue" code?
by mancz (Initiate) on Mar 21, 2002 at 16:58 UTC
    print 'press [Enter] to continue...'; <>;

    Originally posted as a Categorized Answer.

Re: What is a good
by Anonymous Monk on Nov 18, 2002 at 15:01 UTC
    print "Press Enter to continue "; <STDIN>; #I don't know and neither does these other guys, how to do any key (only enter) -Quais T

    Originally posted as a Categorized Answer.

Re: What is a good
by Anonymous Monk on Nov 18, 2002 at 15:00 UTC
    print "Press Enter to continue "; <STDIN>;

    Originally posted as a Categorized Answer.

Re: What is a good
by Anonymous Monk on Sep 22, 2000 at 06:38 UTC
    use Term::ReadKey; ReadMode 4; # Turn off controls keys while (not defined ($key = ReadKey(-1)) { # No key yet } print "Get key $key\n"; ReadMode 0; # Reset tty mode before exiting

    Originally posted as a Categorized Answer.