2

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

循环永远不会结束。

4

1 回答 1

3

您可以按照文档中显示的方式进行操作:

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:\温度> t

输入:一个

输入:两个

输入:^D
你进来了:
一
二
于 2010-03-19T13:13:10.533 回答