如何在cygwin下的Perl脚本中处理箭头键?

Nag*_*ran 6 windows perl cygwin

我在cygwin下运行Perl脚本,它从输入中获取输入<STDIN>并连续处理请求.

#!/usr/bin/perl
print "Enter Input:";
while(<STDIN>) {
    print "Recieved Input: $_";
    print "Enter Input:";
}



    $perl testPerl.pl        
    Enter input:input1
    Recieved input:input1
    Enter input:inpt2
    Recieved input:input2
    Enter input:
Run Code Online (Sandbox Code Playgroud)

现在,我想在当前提示符处的向上箭头:"输入输入:"以获取先前的输入,即"input2","input1"

在windows enivronment(cmd.exe)下运行时它的行为与预期的一样.
但是cygwin下的问题是向上箭头字面上将光标向上移动1行,即它采用"接收输入:input2"行

请分享您对此的看法.

gor*_*lla 4

查看 Term::Readline 模块。这将接管您的程序的输入,并处理历史记录,这就是我认为您正在谈论的内容。

这将是您的程序直接转换为使用 Term::ReadLine:

 use Term::ReadLine;
 my $term = new Term::ReadLine 'Simple Perl calc';
 my $prompt = "Enter Input: ";
 while ( defined ($_ = $term->readline($prompt)) ) {
   print "Recieved Input:$_\n";
   $term->addhistory($_) if /\S/;
 }