用户输入仅在Ctrl D上终止,而不是换行符

时间:2013-08-28 17:50:02

标签: perl input

从我在Debian上运行的脚本中读取用户输入时,我发现用户输入只有在按下Ctrl-D后才能终止,而不是在按下Enter / Return键后终止。

my $userchoice = <>;

对于我的脚本,我要求用户输入文本并使用Return / Enter键终止它。什么可能导致我的脚本?

我的脚本中可以先设置slurp模式与此相关吗?

我看过perdoc,但在那里找不到解释。

sub InteractiveMenu {
    for my $key(0 .. $#desclist) {
        my $value = $desclist[$key];
        printf (" %-3s %-20s -> %-15s -> %-30s\n", $key, $desclist[$key], $iplist[$key], $filelist[$key] );
    }
    print "\nAvailable choices:\n";
    printf " (R)oot key installation [installs your public key to remote servers]\n";
    printf " (S)etup remote logging [sets up user account on remotes]\n\n";
    print "Choose a server to work on:\n"; 
    chomp(my $userchoice = <>);
}
sub ListRemotes {
    print "Listing remote servers from $Confile\n";
    open my $ReadHandle, "<", $Confile or die $!;
    local $/; # enable localized slurp mode
    chomp(my $content = <$ReadHandle>);
    close $ReadHandle;
    my @values = split('zone ', $content);
      foreach my $val (@values) {       
        #print $val."\n-------------------------\n";
        &ListWorker($val);
      }
    InteractiveMenu();  
}

enter image description here

2 个答案:

答案 0 :(得分:2)

  

我的脚本中可以先设置slurp模式与此相关吗?

是。将$/设置为undef会导致readline(又名<>)读取到文件末尾,而不是直到行结束。 Ctrl-D会使您的终端发出EOF信号。

答案 1 :(得分:0)

要重置slurp模式,可以将slurp模式放入范围

open my $ReadHandle, "<", $Confile or die $!;
{
    local $/; # enable localized slurp mode
    chomp(my $content = <$ReadHandle>);
}
close $ReadHandle;