Help for this page

Select Code to Download


  1. or download this
    while (1) {
       print 'Please enter a word: ';
    ...
       last if $word =~ /^\s*$/;
       print "You entered '$word'\n";
    }
    
  2. or download this
    # Had to duplicate the prompt, <read>, and chomp.
    # Duplication is bad!
    ...
       print 'Please enter a word: ';
       chomp($word = <STDIN>);
    }
    
  3. 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";
       }
    }
    
  4. or download this
    # Instead of using `not defined $word`, I force
    # non-whitespace into $word.
    ...
           print "You entered '$word'\n";
       }
    }
    
  5. or download this
    # Still had to duplicate code `$word !~ /^\s*$/`
    my $word;
    ...
           print "You entered '$word'\n";
       }
    } while $word !~ /^\s*$/;
    
  6. or download this
    sub prompt {
        my ($prompt_string) = @_;
    ...
    while ( my $word = prompt('Please enter a word: ') ) {
        print "You entered '$word'\n";
    }
    
  7. or download this
    use IO::Prompt;
    while (my $word=prompt 'Please enter a word: ', -while => qr/\S/) {
        print "You entered '$word'\n";
    }