使用
Term::ReadLine::Gnu时,我似乎无法获得不区分大小写的完成.以此示例脚本为例:
use strict; use warnings; use 5.010; use Term::ReadLine; my $term = Term::ReadLine->new('test'); say "Using " . $term->ReadLine; if (my $attr = $term->Attribs) { $term->ornaments(0); $attr->{basic_word_break_characters} = ". \t\n"; $attr->{completer_word_break_characters} = " \t\n"; $attr->{completion_function} = \&complete_word; } # end if attributes my @words = qw(apple approve Adam America UPPER UPPERCASE UNUSED); sub complete_word { my ($text,$line,$start) = @_; return grep(/^$text/i,@words); } # end complete_word while (1) { $_ = $term->readline(']'); last unless /\S/; # quit on empty input } # end while 1
请注意,complete_word执行不区分大小写的匹配.如果我用Term::ReadLine::Perl(通过执行PERL_RL = Perl perl script.pl)运行它,它可以正常工作.键入< TAB>< TAB>列出所有4个单词.键入u< TAB>< TAB>将你转换为U并列出3个单词.
当我使用Term::ReadLine::Gnu而不是(PERL_RL = Gnu perl script.pl或只是perl script.pl)时,它只会区分大小写完成.输入< TAB>给应用程序.键入u< TAB>< TAB>没有列出任何完成.
我甚至在我的/ etc / inputrc中设置了completion-ignore-case,但它仍然无法在这里工作. (但它在bash中工作得很好.)
有没有办法让Term::ReadLine::Gnu做不区分大小写的完成?
解决方法
问题出现在Term :: ReadLine :: Gnu :: XS :: _ trp_completion_function()(用户定义的完成函数的包装器)中.
您的匹配是从您的complete_word()函数中正确检索的,但是包装器中的以下代码段会执行自己的区分大小写的匹配:
for (; $_i <= $#_matches; $_i++) { return $_matches[$_i] if ($_matches[$_i] =~ /^\Q$text/); }
其中@_matches是你的complete_word()的结果,而$text是到目前为止已完成的文本.
所以看起来答案是否定的,没有支持的方法让Term :: ReadLine :: Gnu做不区分大小写的完成.你必须覆盖私有的Term :: ReadLine :: Gnu :: XS :: _ trp_completion_function(一个丑陋的黑客来确定) – 或直接修改XS.pm(可以说是一个更丑陋的黑客).
编辑:Term :: ReadLine ::使用的Gnu版本:1.20