如何禁止 Term::ReadLine 的默认文件名完成?

How to inhibit Term::ReadLine's default filename completion?

如何禁用 Term::ReadLine 的默认完成,或者更确切地说,让它在某个时候停止建议文件名完成?

例如,我需要用什么替换 return() 以禁止从第二个单词开始的默认完成?

这些都不起作用:

    $attribs->{'filename_completion_function'}=undef;
    $attribs->{'rl_inhibit_completion'}=1;
use Term::ReadLine;

my $term    = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
$attribs->{attempted_completion_function} = \&sample_completion;

sub sample_completion {
    my ( $text, $line, $start, $end ) = @_;

    # If first word then username completion, else filename completion
    if ( substr( $line, 0, $start ) =~ /^\s*$/ ) {

        return $term->completion_matches( $text,
            $attribs->{'username_completion_function'} );
    }
    else {
        return ();
    }
}

while ( my $input = $term->readline( "> " ) ) {
    ...
}

定义 completion_function 而不是 attempted_completion_function:

$attribs->{completion_function} = \&completion;

然后 return undef 如果完成应该停止,return $term->completion_matches($text, $attribs->{filename_completion_function}) 如果文件名完成要接管。

在下面的示例中,第一个参数没有任何建议,但文件名是第二个参数。

use Term::ReadLine;

my $term = new Term::ReadLine 'sample';
my $attribs = $term->Attribs;
$attribs->{completion_function} = \&completion;

sub completion {

  my ( $text, $line, $start ) = @_;

  if ( substr( $line, 0, $start ) =~ /^\s*$/) {

    return 

  } else {

    return $term->completion_matches($text, $attribs->{filename_completion_function})

  }
}

while ( my $input = $term->readline ("> ") ) {
  exit 0 if $input eq "q";
}