Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Press enter to exit

by kcott (Chancellor)
on Jul 27, 2013 at 08:52 UTC ( #1046642=note: print w/replies, xml ) Need Help??

in reply to Press enter to exit

I see ++McA has provided pointers to problems with your code: all good advice and probably resolves your current issues.

While it's good that you've shown a minimal example for describing your problem, I wonder if it's a little too minimal in the sense that it masks what you're really trying to do. In the context of your actual application, is "press enter to quit" really prompting the user with something closer to one of these:

  1. "when you're finished reading the output displayed, press enter to quit (which will remove the window with the output and return you to the commandline)"
  2. "enter more input or, when ready to process your supplied data, press enter to quit data entry"
  3. "press enter and no other key: the program is trying to determine if your enter key works properly"

Obviously, those are somewhat wordy, probably not the actual text you'd use and really only meant to highlight context and intent. Also, there may be other reasons for this prompting of the user.

For 1: you really don't care what the user enters and something as minimal as this is probably fine (untested):

print 'Hit enter when done: '; <>; exit;

For 2: perhaps code more like this (untested):

print "Type input (hit just enter when done):\n"; while (<>) { chomp; last unless length; # validate/store input here }

For 3: your original code is possibly closer to the mark in this scenario.

See also: IO::Prompt::Tiny (for simple prompting) and IO::Prompt::Hooked (for prompting with validation).

Finally, in the two instances of printing the prompt in your code, you don't want a newline at the end of the prompt string.

With print "Prompt:\n"; the user sees


With print 'Prompt: '; the user sees

Prompt: _

Update: Added chomp; to "For 2:" code.

-- Ken

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046642]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-05-26 05:26 GMT
Find Nodes?
    Voting Booth?