我看到问题的方式是:STDIN
通过使用脚本运行后从脚本内部运行的外部程序来过滤脚本(因此,不是使用管道)。使用IPC::Run
use warnings;
use strict;
use feature 'say';
use IPC::Run qw(start pump finish);
my $filtered_in;
FILTER_IN: {
my @cmd = qw(a_filter.pl); # add filter's options/arguments
my $h = start \@cmd, \my $in, \$filtered_in;
while (<>) {
$in = $_;
pump $h while length $in;
# Wait for filter's output -- IF WANT to process lines as received
pump $h until $filtered_in =~ /\n\z/;
chomp $filtered_in; # process/use filter's output
$filtered_in .= '|'; # as it's coming (if needed)
}
finish $h or die "Cleanup returned: $?";
};
say $filtered_in // 'no input';
这允许人们在发出过滤器的输出行时对其进行处理。如果不需要,但我们只想累积过滤器的输出以供以后使用,那么您不需要下面的代码# Wait for...
最简单的测试,a_filter.pl
例如
use warnings;
use strict;
STDOUT->autoflush(1);
my $cnt = 0;
while (<>) { print "line ", ++$cnt, ": ", $_ }
然后运行
echo "a\nfew\nlines" | script.pl
带输出
第 1 行:a|第 2 行:少数|第 3 行:行|
来自我们script.pl
上面的玩具加工。
这也将通过文件过滤输入,
script.pl < input.txt