【发布时间】:2013-06-27 19:34:35
【问题描述】:
我正在尝试以下方法:
我想分叉多个进程并同时使用多个管道(子 -> 父)。 我的方法是使用 IO::Pipe。
#!/usr/bin/perl
use strict;
use IO::Pipe;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my @ua_processes = (0..9);
my $url = "http://<some-sample-textfile>";
my @ua_pipe;
my @ua_process;
$ua_pipe[0] = IO::Pipe->new();
$ua_process[0] = fork();
if( $ua_process[0] == 0 ) {
my $response = $ua->get($url);
$ua_pipe[0]->writer();
print $ua_pipe[0] $response->decoded_content;
exit 0;
}
$ua_pipe[0]->reader();
while (<$ua_pipe[0]>) {
print $_;
}
以后我想在一个数组中使用多个“$ua_process”。
执行后出现以下错误:
Scalar found where operator expected at ./forked.pl line 18, near "] $response"
(Missing operator before $response?)
syntax error at ./forked.pl line 18, near "] $response"
BEGIN not safe after errors--compilation aborted at ./forked.pl line 23.
如果我不使用数组,同样的代码可以完美运行。似乎只有 $ua_pipe[0] 没有按预期工作(与数组一起)。
我真的不知道为什么。有人知道解决方案吗?非常感谢您的帮助!
【问题讨论】: