in reply to Re^2: Problem in printing lastname and firstname
in thread Problem in printing lastname and firstname
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
|
|---|