【问题标题】:How to use Term::ReadLine to retrieve command history?如何使用 Term::ReadLine 检索命令历史记录?
【发布时间】:2013-02-03 11:06:56
【问题描述】:

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

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)

既然我们在这里,我注意到它会打印带下划线的提示...但我在文档中找不到任何可以阻止它的内容。有什么办法可以避免吗?

【问题讨论】:

  • 它适用于我(Debian 上的 Perl 5.10)。你检查过你的终端键绑定吗?
  • @Rob,您使用的是哪个 Term::ReadLine 实现?在我安装 Term::ReadLine::Gnu 后它对我有用,但我认为即使使用 Stub 也应该可以工作......

标签: perl readline arrow-keys


【解决方案1】:

要回答主要问题,您可能没有安装好的 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($_);
}

【讨论】:

  • @Zagorax 您介意选择答案还是提交您自己的答案?
  • 注意:装饰品(0)对我不起作用。在 BEGIN{} 块中设置 $ENV{PERL_RL}="o=0" 确实如此。很奇怪
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 2018-09-03
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2013-08-17
  • 1970-01-01
  • 2019-06-05
相关资源
最近更新 更多