What is the right way to stop an infinite while-loop with a Term::Readline-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
精彩评论