Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Press enter to exit

by kcott (Abbot)
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

Prompt:
_

With print 'Prompt: '; the user sees

Prompt: _

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

-- Ken


Comment on Re: Press enter to exit
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046642]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-09-17 18:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (93 votes), past polls