使用Term :: Readline-readline停止无限while循环的正确方法是什么?

时间:2010-03-19 12:54:02

标签: perl terminal while-loop readline

使用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;

循环永远不会结束。

1 个答案:

答案 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