0

我目前正在尝试使用我的程序并让它接受用户输入,通常是一个文本文件,然后调用一个外部脚本来计算单词。我正在处理的脚本本质上是一个“中间人”,我正在尝试更熟悉到外部脚本/命令的管道。它目前没有正确执行单词计数器脚本。这是代码:

我仍然收到 ./word_counter.pl 的错误消息,说“glue.pl 中没有这样的文件或目录(您在此处看到的脚本)”。

#!usr/bin/perl
use warnings;
use strict;
use IO::Handle qw();

open (PIPE_TO, "|-", "./word_counter.pl");
While(<>)
{
$PIPE_TO -> autoflush(1);
print PIPE_TO $_;

}
4

3 回答 3

1

受缓冲之苦?

use IO::Handle qw( );
PIPE_TO->autoflush(1);
于 2013-04-03T04:45:29.857 回答
0

这是你想要做的吗?

#!/usr/bin/perl
use warnings;
use strict;

open (my $PIPE_TO, "|-", "./word_counter.pl") or die $!;
while(<>) {
  print $PIPE_TO $_;
}
于 2013-04-03T05:09:56.383 回答
0

The reason it doesn't work is probably that you have syntax errors.

Otherwise: Other than introducing line-buffered semantics, you are really doing nothing here (you just pipe what you read to another program, which is in this case equivalent to just running the program)

Modulo the buffering (which you don't seem to explicitly need) an equivalent script would be:

#!/usr/bin/perl

exec ("./word_counter.pl");
于 2013-04-03T04:54:00.053 回答