while (1) { print 'Please enter a word: '; chomp(my $word = ); last if $word =~ /^\s*$/; print "You entered '$word'\n"; } #### # Had to duplicate the prompt, , and chomp. # Duplication is bad! # Use the DRY principle: [D]on't [R]epeat [Y]ourself! my $word; print 'Please enter a word: '; chomp($word = ); while ( $word !~ /^\s*$/ ) { print "You entered '$word'\n"; print 'Please enter a word: '; chomp($word = ); } #### # Had to add `not defined $word` to handle the special case # during the first time through the loop. Very Ugly! # Had to duplicate code `$word !~ /^\s*$/` to prevent the # final ENTER from printing "You entered ''" as it exits # the loop. my $word; while ( ( not defined $word ) or ( $word !~ /^\s*$/ ) ) { print 'Please enter a word: '; chomp($word = ); if ( $word !~ /^\s*$/ ) { print "You entered '$word'\n"; } } #### # Instead of using `not defined $word`, I force # non-whitespace into $word. # Still very ugly. # Still had to duplicate code `$word !~ /^\s*$/` my $word = 'JunkToPreventFailingTheFirstLoop'; while ( $word !~ /^\s*$/ ) { print 'Please enter a word: '; chomp($word = ); if ( $word !~ /^\s*$/ ) { print "You entered '$word'\n"; } } #### # Still had to duplicate code `$word !~ /^\s*$/` my $word; do { print 'Please enter a word: '; chomp( $word = ); if ( $word !~ /^\s*$/ ) { print "You entered '$word'\n"; } } while $word !~ /^\s*$/; #### sub prompt { my ($prompt_string) = @_; print $prompt_string; my $input = ; chomp $input; return $input; } # Success! while ( my $word = prompt('Please enter a word: ') ) { print "You entered '$word'\n"; } #### use IO::Prompt; while (my $word=prompt 'Please enter a word: ', -while => qr/\S/) { print "You entered '$word'\n"; }