在询问<stdin> </stdin>时建议一个值

时间:2013-11-19 16:22:29

标签: perl input

我希望用户只需按Enter键即可继续执行perl脚本。 例如:

# in the line below the 'y' should be given as a suggestion, 
# thus only pressing "enter" to continue.

print "Are you over 18? (y/n): "; 
chomp(my $allow = <STDIN>);

2 个答案:

答案 0 :(得分:3)

print "Are you over 18? (Y/n): "; 
chomp(my $allow = <STDIN>);
$allow ||= "y";

if (lc($allow) ne "y") {
  die "you're not allowed\n";
}

答案 1 :(得分:2)

您可以在没有换行符的情况下打印回车符。这会将插入符号移动到行的开头,然后使任何后续文本覆盖它:

print "Are you over 18? (y/n):\ny\r";
chomp(my $allow = <STDIN>);
$allow ||= "y"; # Accept default if nothing supplied