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):
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
In reply to Re: Press enter to exit
by kcott
in thread Press enter to exit
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |