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

标签 perl terminal while-loop readline

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

循环永远不会结束。

最佳答案

您可以按照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

关于perl - 使用 Term::Readline-readline 停止无限 while 循环的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2477376/

相关文章:

python - 更改二维数组元素的值会更改整个列

r - 如何使用 while 循环绑定(bind)多个数据帧?

regex - 如何让 perl 在命令行中打印所有匹配项?

perl - 在标量上下文中使用时,我可以控制 Moose 对象的值吗?

linux - Shell脚本选择多次重复或退出

macos - udp 上的 ffmpeg 原始视频

java - 如何读取没有大括号的循环?

Perl UTF8 到 UTF16 转换错误 - 宽字符

arrays - 将数组、标量和散列传递给 Perl 中的子例程

emacs - 删除截断线末端的字形