Perl 如何使用Term::ReadLine检索命令历史记录?

Perl 如何使用Term::ReadLine检索命令历史记录?,perl,readline,arrow-keys,Perl,Readline,Arrow Keys,我有以下脚本,它几乎与文档中概要段落中的示例相同 use strict; use warnings; use Term::ReadLine; my $term = Term::ReadLine->new('My shell'); print $term, "\n"; my $prompt = "-> "; while ( defined ($_ = $term->readline($prompt)) ) { print $_, "\n"; $term->a

我有以下脚本,它几乎与文档中概要段落中的示例相同

use strict;
use warnings;
use Term::ReadLine;

my $term = Term::ReadLine->new('My shell');
print $term, "\n";
my $prompt = "-> ";

while ( defined ($_ = $term->readline($prompt)) ) {
   print $_, "\n";
   $term->addhistory($_);
}
它执行时没有错误,但不幸的是,即使我单击向上箭头,我也只能得到
^[[A
并且没有历史记录。我缺少什么

print$term
语句打印
term::ReadLine::Stub=ARRAY(0x223d2b8)


由于我们在这里,我注意到它打印了带下划线的提示…但是我在文档中找不到任何可以阻止它的东西。有什么方法可以避免它吗?

要回答主要问题,您可能没有安装好的Term::ReadLine库。您需要“perl Term ReadLine perl”或“perl Term ReadLine Gnu”。以下是fedora软件包的名称,但我确信ubuntu/debian的名称是相似的。我相信你也可以从CPAN获得它们,但我还没有测试过。如果你还没有安装软件包,perl会加载一个几乎没有任何功能的虚拟模块。因此,历史不是其中的一部分

下划线是readline所称的装饰的一部分。如果要完全关闭它们,请在适当的位置添加
$term->装饰(0);

我对你剧本的改写如下

#!/usr/bin/perl
use strict;
use warnings;

use Term::ReadLine; # make sure you have the gnu or perl implementation of readline isntalled
# eg: Term::ReadLine::Gnu or Term::ReadLine::Perl
my $term = Term::ReadLine->new('My shell');
my $prompt = "-> ";
$term->ornaments(0);  # disable ornaments.

while ( defined ($_ = $term->readline($prompt)) ) {
   print $_, "\n";
   $term->addhistory($_);
}

它对我有效(Debian上的Perl 5.10)。你检查过你的终端键绑定吗?@Rob,你使用的是哪个Term::ReadLine实现?在我安装Term::ReadLine::Gnu之后,它对我有效,但我认为它应该对存根也有效…@Zagorax你介意选择答案还是提交你自己的答案吗?注意:装饰(0)对我来说不起作用。在BEGIN{}块中设置$ENV{PERL_RL}=“o=0”就行了。奇怪的是