- or download this
while (1) {
print 'Please enter a word: ';
...
last if $word =~ /^\s*$/;
print "You entered '$word'\n";
}
- or download this
# Had to duplicate the prompt, <read>, and chomp.
# Duplication is bad!
...
print 'Please enter a word: ';
chomp($word = <STDIN>);
}
- or download this
# Had to add `not defined $word` to handle the special case
# during the first time through the loop. Very Ugly!
...
print "You entered '$word'\n";
}
}
- or download this
# Instead of using `not defined $word`, I force
# non-whitespace into $word.
...
print "You entered '$word'\n";
}
}
- or download this
# Still had to duplicate code `$word !~ /^\s*$/`
my $word;
...
print "You entered '$word'\n";
}
} while $word !~ /^\s*$/;
- or download this
sub prompt {
my ($prompt_string) = @_;
...
while ( my $word = prompt('Please enter a word: ') ) {
print "You entered '$word'\n";
}
- or download this
use IO::Prompt;
while (my $word=prompt 'Please enter a word: ', -while => qr/\S/) {
print "You entered '$word'\n";
}