2

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;

ループは決して終わりません。

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:\Temp> t

入力: 1

入力: 2

入力:^D
あなたは入りました:
1
2
于 2010-03-19T13:13:10.533 に答える