使用 Term::Readline::readline 停止无限 while 循环的正确方法是什么? ?
这样我就无法读取单个0
#!/usr/bin/env perl
use warnings;
use strict;
use 5.010;
use Term::ReadLine;
my $term = Term::ReadLine->new( 'Text' );
my $content;
while ( 1 ) {
my $con = $term->readline( 'input: ' );
last if not $con;
$content .= "$con\n";
}
say $content;
并与
last if not defined $con;
循环永远不会结束。
最佳答案
您可以按照documentation中所示的方式进行操作:
use strict; use warnings;
use Term::ReadLine;
my $term = Term::ReadLine->new('Text');
my $content = '';
while ( defined (my $con = $term->readline('input: ')) ) {
last unless length $con;
$content .= "$con\n";
}
print "You entered:\n$content\n";
输出:
C:\Temp> t input: one input: two input:^D You entered: one two
关于perl - 使用 Term::Readline-readline 停止无限 while 循环的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2477376/