We Recommend

Learning Perl Learning Perl
In this smooth, carefully paced course, a leading Perl trainer teaches you to program in the language that threatens to make C, sed, awk, and the Unix shell obsolete for many tasks. This book is the "official" guide for both formal (classroom) and informal learning. It is fully accessible to the novice programmer.


Posted By

micmath on 07/27/07


Tagged

perl


Versions (?)


Who likes this?

1 person has marked this snippet as a favorite

mabridge


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.