【发布时间】:2016-05-30 14:45:31
【问题描述】:
我最近开始使用Term::Readline,但现在我意识到cat text | ./script.pl 不起作用(没有输出)。
script.pl sn-p before(工作正常):
#!/usr/bin/perl
use strict;
use warnings;
$| = 1;
while (<>) {
print $_;
}
script.pl sn-p after(仅以交互方式工作):
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadLine
$| = 1;
my $term = Term::ReadLine->new('name');
my $input;
while (defined ($input = $term->readline('')) ) {
print $input;
}
我可以做些什么来保持这种行为(打印行)?
【问题讨论】:
-
如果你想从 STDIN 读取,你需要从 STDIN 读取。您可以将行为基于
my $interactive = !@ARGV && -t STDIN; -
问题标题中无用使用 Cat 的经典示例。见en.wikipedia.org/wiki/Cat_%28Unix%29#Useless_use_of_cat
-
你需要决定你想从哪里读取:如果你使用
readline,那么它总是从/dev/tty读取。如果您使用<>或标准输入,它可能会也可能不会。