|Think about Loose Coupling|
Re: Press enter to exitby kcott (Abbot)
|on Jul 27, 2013 at 08:52 UTC||Need Help??|
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:
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):
For 2: perhaps code more like this (untested):
For 3: your original code is possibly closer to the mark in this scenario.
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
Update: Added chomp; to "For 2:" code.