Note too the use of the comma operator in the while loop expression to get the prompt output each time through the loop.Not a highly recommended technique, but can be less ugly and more cogent than many of the alternative ways of achieving the same effect.

On *nix I usually set up my prompt outside the while loop along with another string of returns and spaces to erase the prompt once I drop out of the loop. Like this

my $prompt = q{Enter name (<Ctrl-D> to exit) : }; my $dePrompt = qq{\r} . q{ } x (length($prompt) + 2) . qq{\r}; while (1) { print $prompt; last if eof STDIN; chomp(my $response = <STDIN>); # Do something with $response here ... } print $dePrompt;

This has the advantage of cleaning up the last prompt displayed before returning to a shell prompt or some further output from the script. It does not work so well under MS Windows as it seems you have to do <Ctrl-Z><Enter> to signal EOF and that throws a line before you can erase the prompt. Thus, you may as well dispense with the $dePrompt part of it there.

I am not sure why you do defined (my $input = <STDIN>). Will it not always be defined, even with just hitting <Enter> as $input will always contain at least a newline? Perhaps I'm missing something but I would have thought you'd only have to worry about definedness after the chomp.

Cheers,

JohnGG


In reply to Re^3: Problem in printing lastname and firstname by johngg
in thread Problem in printing lastname and firstname 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.