4

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

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)

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

4

1 回答 1

7

要回答主要问题,您可能没有安装好的 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($_);
}
于 2013-03-13T15:15:33.893 回答