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


In reply to Re: Press enter to exit by kcott
in thread Press enter to exit by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.