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

Hi friends - I want my program to halt before executing a further step until the user inputs anything(may be enter).Is there a way this can be achieved in PERL?

  • Comment on Is there a way to halt the program until an enter is pressed?

Replies are listed 'Best First'.
Re: Is there a way to halt the program until an enter is pressed?
by Tux (Canon) on Mar 12, 2011 at 08:33 UTC
    print STDERR "Hit Enter > "; scalar <STDIN>;

    Enjoy, Have FUN! H.Merijn
Re: Is there a way to halt the program until an enter is pressed?
by GrandFather (Saint) on Mar 12, 2011 at 08:44 UTC

    For the purpose of debugging, or to pause a long report so it can be read in fragments? If you want to pause for debugging use Perl's built in debugger (see perldebug). If on the other hand you wish a pause for UI purposes the following may help:

    print "Read this then press enter"; <>; print "Thanks for that.\n";
    True laziness is hard work

      Basically I want to give the user the option to proceed or exit out of the program.Please advise how can i do that?

        Then why didn't you say so? How about you define the whole problem so we don't have to go through the whole painful question and answer deal then we can give an appropriate answer to the whole problem.

        And just in case you want to think about solving it for yourself you might dig through perlop's 'I/O Operators' section.

        True laziness is hard work
        ... print "Press <Enter> to continue, or 'q' to quit: "; my $input = <STDIN>; exit if $input eq "q\n"; ...

        (Note that <> reads lines, so - strictly speaking - they'd have to enter q<Enter>... In case that could be an issue, you might want to choose a different wording for the prompt.)

        Or tell them to use Ctrl-C.

        And make a subroutine out of the snippet if you need it more than once.

Re: Is there a way to halt the program until an enter is pressed?
by JavaFan (Canon) on Mar 12, 2011 at 08:39 UTC
    <>;