Get user input on the command line.


/ Published in: Perl
Save to your folder(s)



Copy this code and paste it in your HTML
  1. sub ask {
  2. my($promptString, $defaultValue) = @_;
  3.  
  4. if ($defaultValue) {
  5. print $promptString, "[", $defaultValue, "]: ";
  6. } else {
  7. print $promptString, ": ";
  8. }
  9.  
  10. $| = 1; # force a flush after our print
  11. $_ = <STDIN>; # get the input from STDIN (presumably the keyboard)
  12.  
  13.  
  14. if ("$defaultValue") {
  15. return defined $_ ? $_ : $defaultValue; # return $_ if it has a value
  16. }
  17. return $_;
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.