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

Zag*_*rax 3 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->addhistory($_);
}
Run Code Online (Sandbox Code Playgroud)

它执行时没有错误,但不幸的是,即使我单击向上箭头,我也只能得到^[[A并且没有历史记录.我错过了什么?

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

因为我们在这里,我注意到它打印出下划线的提示...但我在文档中找不到任何可能阻止它的东西.有什么办法可以避免吗?

Mob*_*ius 5

要回答主要问题,您可能没有安装好的Term :: ReadLine库.你会想要'perl-Term-ReadLine-Perl'或'perl-Term-ReadLine-Gnu'.这些是fedora软件包名称,但我确信ubuntu/debian名称会相似.我相信你也可以从CPAN获得它们,但我没有测试过.如果您尚未安装该软件包,则perl会加载几乎没有任何功能的虚拟模块.因此,历史不是它的一部分.

下划线是readline调用饰品的一部分.如果你想完全关闭它们,请添加$term->ornaments(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($_);
}
Run Code Online (Sandbox Code Playgroud)