开发者

What is the right way to stop an infinite while-loop with a Term::Readline-readline?

开发者 https://www.devze.com 2022-12-23 05:29 出处:网络
What is the right way to stop an endless while-loop with a Term::Readline::开发者_JAVA百科readline?

What is the right way to stop an endless while-loop with a Term::Readline::开发者_JAVA百科readline?

This way I can not read in a single 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;

and with

last if not defined $con;

the loop does never end.


You can do it the way it is shown in the 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";

Output:

C:\Temp> t

input: one

input: two

input:^D
You entered:
one
two
0

精彩评论

暂无评论...
验证码 换一张
取 消