使用Term::Readline::readline停止无限循环的正确方法是什么?
这样我就无法阅读单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;
循环永远不会结束。
答案 0 :(得分:3)
您可以按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