Posted By

micmath on 07/27/07


Tagged

perl


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

mabridge
ipilya


Get user input on the command line.


 / Published in: Perl
 

  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
Posted By: jkramer on January 30, 2008
use IO::Prompt;
my $foo = prompt -p => $prompt, -d => $default;

You need to login to post a comment.